24#ifndef PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
25#define PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
29#include "pxr/base/tf/pyLock.h"
32#include <boost/python/class.hpp>
33#include <boost/python/operators.hpp>
34#include <boost/python/return_by_value.hpp>
38PXR_NAMESPACE_OPEN_SCOPE
40template <
class Annotation>
41struct TfPyAnnotatedBoolResult
43 TfPyAnnotatedBoolResult() {}
45 TfPyAnnotatedBoolResult(
bool val, Annotation
const &annotation) :
46 _val(val), _annotation(annotation) {}
48 bool GetValue()
const {
52 Annotation
const &GetAnnotation()
const {
56 std::string GetRepr()
const {
57 return GetValue() ?
"True" :
58 "(False, " +
TfPyRepr(GetAnnotation()) +
")";
62 bool operator==(
bool rhs)
const {
66 friend bool operator==(
bool lhs,
const TfPyAnnotatedBoolResult& rhs) {
70 friend bool operator!=(
const TfPyAnnotatedBoolResult& lhs,
bool rhs) {
74 friend bool operator!=(
bool lhs,
const TfPyAnnotatedBoolResult& rhs) {
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;
84 return class_<Derived>(name, init<bool, Annotation>())
85 .def(
"__bool__", &Derived::GetValue)
86 .def(
"__repr__", &Derived::GetRepr)
106 .add_property(annotationName, &This::_GetAnnotation<Derived>)
107 .def(
"__getitem__", &This::_GetItem<Derived>)
111 using AnnotationType = Annotation;
115 template <
class Derived>
116 static Annotation _GetAnnotation(
const Derived& x)
118 return x.GetAnnotation();
121 template <
class Derived>
122 static boost::python::object _GetItem(
const Derived& x,
int i)
125 return boost::python::object(x._val);
128 return boost::python::object(x._annotation);
131 PyErr_SetString(PyExc_IndexError,
"Index must be 0 or 1.");
132 boost::python::throw_error_already_set();
134 return boost::python::object();
139 Annotation _annotation;
144template <
class Annotation>
145bool operator==(
bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
151template <
class Annotation>
152bool operator!=(
bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
157PXR_NAMESPACE_CLOSE_SCOPE
Miscellaneous Utilities for dealing with script.
std::string TfPyRepr(T const &t)
Return repr(t).
Convenience class for accessing the Python Global Interpreter Lock.