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/python/class.hpp>
33 #include <hboost/python/operators.hpp>
34 #include <hboost/python/return_by_value.hpp>
35 
36 #include <string>
37 
39 
40 template <class Annotation>
42 {
44 
45  TfPyAnnotatedBoolResult(bool val, Annotation const &annotation) :
46  _val(val), _annotation(annotation) {}
47 
48  bool GetValue() const {
49  return _val;
50  }
51 
52  Annotation const &GetAnnotation() const {
53  return _annotation;
54  }
55 
56  std::string GetRepr() const {
57  return GetValue() ? "True" :
58  "(False, " + TfPyRepr(GetAnnotation()) + ")";
59  }
60 
61  /// Returns \c true if the result is the same as \p rhs.
62  bool operator==(bool rhs) const {
63  return _val == rhs;
64  }
65 
66  friend bool operator==(bool lhs, const TfPyAnnotatedBoolResult& rhs) {
67  return rhs == lhs;
68  }
69 
70  friend bool operator!=(const TfPyAnnotatedBoolResult& lhs, bool rhs) {
71  return !(lhs == rhs);
72  }
73 
74  friend bool operator!=(bool lhs, const TfPyAnnotatedBoolResult& rhs) {
75  return !(lhs == rhs);
76  }
77 
78  template <class Derived>
79  static hboost::python::class_<Derived>
80  Wrap(char const *name, char const *annotationName) {
82  using namespace hboost::python;
83  TfPyLock lock;
84  return class_<Derived>(name, init<bool, Annotation>())
85  .def("__bool__", &Derived::GetValue)
86  .def("__repr__", &Derived::GetRepr)
87  .def(self == bool())
88  .def(self != bool())
89  .def(bool() == self)
90  .def(bool() != self)
91  // Use a helper function. We'd like to def_readonly the
92  // _annotation member but there are two problems with that.
93  // First, we can't control the return_value_policy and if the
94  // Annotation type has a custom to-Python converter then the
95  // def_readonly return_value_policy of return_internal_reference
96  // won't work since the object needs conversion. Second, if we
97  // try to use GetAnnotation() with add_property then we'll get
98  // a failure at runtime because Python has a Derived but
99  // GetAnnotation takes a TfPyAnnotatedBoolResult<Annotation>
100  // and hboost python doesn't know the former is-a latter because
101  // TfPyAnnotatedBoolResult<Annotation> is not wrapped.
102  //
103  // So we provide a templated static method that takes a Derived
104  // and returns Annotation by value. We can add_property that
105  // with no problem.
106  .add_property(annotationName, &This::_GetAnnotation<Derived>)
107  .def("__getitem__", &This::_GetItem<Derived>)
108  ;
109  }
110 
111  using AnnotationType = Annotation;
112 
113 private:
114  // Helper function for wrapper.
115  template <class Derived>
116  static Annotation _GetAnnotation(const Derived& x)
117  {
118  return x.GetAnnotation();
119  }
120 
121  template <class Derived>
122  static hboost::python::object _GetItem(const Derived& x, int i)
123  {
124  if (i == 0) {
125  return hboost::python::object(x._val);
126  }
127  if (i == 1) {
128  return hboost::python::object(x._annotation);
129  }
130 
131  PyErr_SetString(PyExc_IndexError, "Index must be 0 or 1.");
132  hboost::python::throw_error_already_set();
133 
134  return hboost::python::object();
135  }
136 
137 private:
138  bool _val;
139  Annotation _annotation;
140 
141 };
142 
143 /// Returns \c true if the result of \p lhs is the same as \p rhs.
144 template <class Annotation>
146 {
147  return rhs == lhs;
148 }
149 
150 /// Returns \c false if the result of \p lhs is the same as \p rhs.
151 template <class Annotation>
153 {
154  return rhs != lhs;
155 }
156 
158 
159 #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)
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
friend bool operator!=(bool lhs, const TfPyAnnotatedBoolResult &rhs)
friend bool operator!=(const TfPyAnnotatedBoolResult &lhs, bool rhs)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
friend bool operator==(bool lhs, const TfPyAnnotatedBoolResult &rhs)
bool operator==(bool rhs) const
Returns true if the result is the same as rhs.
std::string GetRepr() const