Loading...
Searching...
No Matches
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 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 <boost/python/class.hpp>
33#include <boost/python/operators.hpp>
34#include <boost/python/return_by_value.hpp>
35
36#include <string>
37
38PXR_NAMESPACE_OPEN_SCOPE
39
40template <class Annotation>
41struct TfPyAnnotatedBoolResult
42{
43 TfPyAnnotatedBoolResult() {}
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
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 boost::python::class_<Derived>
80 Wrap(char const *name, char const *annotationName) {
81 typedef TfPyAnnotatedBoolResult<Annotation> This;
82 using namespace boost::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 boost 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
113private:
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 boost::python::object _GetItem(const Derived& x, int i)
123 {
124 if (i == 0) {
125 return boost::python::object(x._val);
126 }
127 if (i == 1) {
128 return boost::python::object(x._annotation);
129 }
130
131 PyErr_SetString(PyExc_IndexError, "Index must be 0 or 1.");
132 boost::python::throw_error_already_set();
133
134 return boost::python::object();
135 }
136
137private:
138 bool _val;
139 Annotation _annotation;
140
141};
142
144template <class Annotation>
145bool operator==(bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
146{
147 return rhs == lhs;
148}
149
151template <class Annotation>
152bool operator!=(bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
153{
154 return rhs != lhs;
155}
156
157PXR_NAMESPACE_CLOSE_SCOPE
158
159#endif // PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
Miscellaneous Utilities for dealing with script.
std::string TfPyRepr(T const &t)
Return repr(t).
Definition: pyUtils.h:180
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:122