All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyAnnotatedBoolResult.h
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 TF_PYANNOTATEDBOOLRESULT_H
25 #define TF_PYANNOTATEDBOOLRESULT_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 <boost/operators.hpp>
33 #include <boost/python/class.hpp>
34 #include <boost/python/operators.hpp>
35 #include <boost/python/return_by_value.hpp>
36 
37 #include <string>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 template <class Annotation>
42 struct TfPyAnnotatedBoolResult :
43  boost::equality_comparable<TfPyAnnotatedBoolResult<Annotation>, bool>
44 {
45  TfPyAnnotatedBoolResult() {}
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 
64  bool operator==(bool rhs) const {
65  return _val == rhs;
66  }
67 
68  template <class Derived>
69  static boost::python::class_<Derived>
70  Wrap(char const *name, char const *annotationName) {
71  typedef TfPyAnnotatedBoolResult<Annotation> This;
72  using namespace boost::python;
73  TfPyLock lock;
74  return class_<Derived>(name, no_init)
75  .def("__nonzero__", &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 boost 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 boost::python::object _GetItem(const Derived& x, int i)
113  {
114  if (i == 0) {
115  return boost::python::object(x._val);
116  }
117  if (i == 1) {
118  return boost::python::object(x._annotation);
119  }
120 
121  PyErr_SetString(PyExc_IndexError, "Index must be 0 or 1.");
122  boost::python::throw_error_already_set();
123 
124  return boost::python::object();
125  }
126 
127 private:
128  bool _val;
129  Annotation _annotation;
130 
131 };
132 
134 template <class Annotation>
135 bool operator==(bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
136 {
137  return rhs == lhs;
138 }
139 
141 template <class Annotation>
142 bool operator!=(bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
143 {
144  return rhs != lhs;
145 }
146 
147 PXR_NAMESPACE_CLOSE_SCOPE
148 
149 #endif // TF_PYANNOTATEDBOOLRESULT_H
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
std::string TfPyRepr(T const &t)
Return repr(t).
Definition: pyUtils.h:136