24 #ifndef PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
25 #define PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
30 #include "pxr/base/tf/pyLock.h"
33 #include <boost/operators.hpp>
34 #include <boost/python/class.hpp>
35 #include <boost/python/operators.hpp>
36 #include <boost/python/return_by_value.hpp>
40 PXR_NAMESPACE_OPEN_SCOPE
42 template <
class Annotation>
43 struct TfPyAnnotatedBoolResult :
44 boost::equality_comparable<TfPyAnnotatedBoolResult<Annotation>, bool>
46 TfPyAnnotatedBoolResult() {}
48 TfPyAnnotatedBoolResult(
bool val, Annotation
const &annotation) :
49 _val(val), _annotation(annotation) {}
51 bool GetValue()
const {
55 Annotation
const &GetAnnotation()
const {
59 std::string GetRepr()
const {
60 return GetValue() ?
"True" :
61 "(False, " +
TfPyRepr(GetAnnotation()) +
")";
69 template <
class Derived>
70 static boost::python::class_<Derived>
71 Wrap(
char const *name,
char const *annotationName) {
72 typedef TfPyAnnotatedBoolResult<Annotation> This;
73 using namespace boost::python;
75 return class_<Derived>(name, no_init)
76 .def(TfPyBoolBuiltinFuncName, &Derived::GetValue)
77 .def(
"__repr__", &Derived::GetRepr)
97 .add_property(annotationName, &This::_GetAnnotation<Derived>)
98 .def(
"__getitem__", &This::_GetItem<Derived>)
102 using AnnotationType = Annotation;
106 template <
class Derived>
107 static Annotation _GetAnnotation(
const Derived& x)
109 return x.GetAnnotation();
112 template <
class Derived>
113 static boost::python::object _GetItem(
const Derived& x,
int i)
116 return boost::python::object(x._val);
119 return boost::python::object(x._annotation);
122 PyErr_SetString(PyExc_IndexError,
"Index must be 0 or 1.");
123 boost::python::throw_error_already_set();
125 return boost::python::object();
130 Annotation _annotation;
135 template <
class Annotation>
136 bool operator==(
bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
142 template <
class Annotation>
143 bool operator!=(
bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
148 PXR_NAMESPACE_CLOSE_SCOPE
150 #endif // PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
Compatibility code for supporting python 2 and 3.
Miscellaneous Utilities for dealing with script.
std::string TfPyRepr(T const &t)
Return repr(t).
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.