HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyAnnotatedBoolResult.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
25 #define PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/tf/pyLock.h"
30 #include "pxr/base/tf/pyUtils.h"
31 
32 #include <hboost/operators.hpp>
33 #include <hboost/python/class.hpp>
34 #include <hboost/python/operators.hpp>
35 #include <hboost/python/return_by_value.hpp>
36 
37 #include <string>
38 
40 
41 template <class Annotation>
43  hboost::equality_comparable<TfPyAnnotatedBoolResult<Annotation>, bool>
44 {
46 
47  TfPyAnnotatedBoolResult(bool val, Annotation const &annotation) :
48  _val(val), _annotation(annotation) {}
49 
50  bool GetValue() const {
51  return _val;
52  }
53 
54  Annotation const &GetAnnotation() const {
55  return _annotation;
56  }
57 
58  std::string GetRepr() const {
59  return GetValue() ? "True" :
60  "(False, " + TfPyRepr(GetAnnotation()) + ")";
61  }
62 
63  /// Returns \c true if the result is the same as \p rhs.
64  bool operator==(bool rhs) const {
65  return _val == rhs;
66  }
67 
68  template <class Derived>
69  static hboost::python::class_<Derived>
70  Wrap(char const *name, char const *annotationName) {
72  using namespace hboost::python;
73  TfPyLock lock;
74  return class_<Derived>(name, init<bool, Annotation>())
75  .def("__bool__", &Derived::GetValue)
76  .def("__repr__", &Derived::GetRepr)
77  .def(self == bool())
78  .def(self != bool())
79  .def(bool() == self)
80  .def(bool() != self)
81  // Use a helper function. We'd like to def_readonly the
82  // _annotation member but there are two problems with that.
83  // First, we can't control the return_value_policy and if the
84  // Annotation type has a custom to-Python converter then the
85  // def_readonly return_value_policy of return_internal_reference
86  // won't work since the object needs conversion. Second, if we
87  // try to use GetAnnotation() with add_property then we'll get
88  // a failure at runtime because Python has a Derived but
89  // GetAnnotation takes a TfPyAnnotatedBoolResult<Annotation>
90  // and hboost python doesn't know the former is-a latter because
91  // TfPyAnnotatedBoolResult<Annotation> is not wrapped.
92  //
93  // So we provide a templated static method that takes a Derived
94  // and returns Annotation by value. We can add_property that
95  // with no problem.
96  .add_property(annotationName, &This::_GetAnnotation<Derived>)
97  .def("__getitem__", &This::_GetItem<Derived>)
98  ;
99  }
100 
101  using AnnotationType = Annotation;
102 
103 private:
104  // Helper function for wrapper.
105  template <class Derived>
106  static Annotation _GetAnnotation(const Derived& x)
107  {
108  return x.GetAnnotation();
109  }
110 
111  template <class Derived>
112  static hboost::python::object _GetItem(const Derived& x, int i)
113  {
114  if (i == 0) {
115  return hboost::python::object(x._val);
116  }
117  if (i == 1) {
118  return hboost::python::object(x._annotation);
119  }
120 
121  PyErr_SetString(PyExc_IndexError, "Index must be 0 or 1.");
122  hboost::python::throw_error_already_set();
123 
124  return hboost::python::object();
125  }
126 
127 private:
128  bool _val;
129  Annotation _annotation;
130 
131 };
132 
133 /// Returns \c true if the result of \p lhs is the same as \p rhs.
134 template <class Annotation>
136 {
137  return rhs == lhs;
138 }
139 
140 /// Returns \c false if the result of \p lhs is the same as \p rhs.
141 template <class Annotation>
143 {
144  return rhs != lhs;
145 }
146 
148 
149 #endif // PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
Annotation const & GetAnnotation() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
std::string TfPyRepr(T const &t)
Definition: pyUtils.h:180
TfPyAnnotatedBoolResult(bool val, Annotation const &annotation)
bool operator!=(bool lhs, TfPyAnnotatedBoolResult< Annotation > &rhs)
Returns false if the result of lhs is the same as rhs.
GLuint const GLchar * name
Definition: glcorearb.h:786
static hboost::python::class_< Derived > Wrap(char const *name, char const *annotationName)
GLint GLenum GLint x
Definition: glcorearb.h:409
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool operator==(bool rhs) const
Returns true if the result is the same as rhs.
std::string GetRepr() const