7#ifndef PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
8#define PXR_BASE_TF_PY_ANNOTATED_BOOL_RESULT_H
12#include "pxr/base/tf/pyLock.h"
15#include "pxr/external/boost/python/class.hpp"
16#include "pxr/external/boost/python/operators.hpp"
17#include "pxr/external/boost/python/return_by_value.hpp"
21PXR_NAMESPACE_OPEN_SCOPE
23template <
class Annotation>
24struct TfPyAnnotatedBoolResult
26 TfPyAnnotatedBoolResult() {}
28 TfPyAnnotatedBoolResult(
bool val, Annotation
const &annotation) :
29 _val(val), _annotation(annotation) {}
31 bool GetValue()
const {
35 Annotation
const &GetAnnotation()
const {
39 std::string GetRepr()
const {
40 return GetValue() ?
"True" :
41 "(False, " +
TfPyRepr(GetAnnotation()) +
")";
45 bool operator==(
bool rhs)
const {
49 friend bool operator==(
bool lhs,
const TfPyAnnotatedBoolResult& rhs) {
53 friend bool operator!=(
const TfPyAnnotatedBoolResult& lhs,
bool rhs) {
57 friend bool operator!=(
bool lhs,
const TfPyAnnotatedBoolResult& rhs) {
61 template <
class Derived>
62 static pxr_boost::python::class_<Derived>
63 Wrap(
char const *name,
char const *annotationName) {
64 typedef TfPyAnnotatedBoolResult<Annotation> This;
65 using namespace pxr_boost::python;
67 return class_<Derived>(name, init<bool, Annotation>())
68 .def(
"__bool__", &Derived::GetValue)
69 .def(
"__repr__", &Derived::GetRepr)
89 .add_property(annotationName, &This::_GetAnnotation<Derived>)
90 .def(
"__getitem__", &This::_GetItem<Derived>)
94 using AnnotationType = Annotation;
98 template <
class Derived>
99 static Annotation _GetAnnotation(
const Derived& x)
101 return x.GetAnnotation();
104 template <
class Derived>
105 static pxr_boost::python::object _GetItem(
const Derived& x,
int i)
108 return pxr_boost::python::object(x._val);
111 return pxr_boost::python::object(x._annotation);
114 PyErr_SetString(PyExc_IndexError,
"Index must be 0 or 1.");
115 pxr_boost::python::throw_error_already_set();
117 return pxr_boost::python::object();
122 Annotation _annotation;
127template <
class Annotation>
128bool operator==(
bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
134template <
class Annotation>
135bool operator!=(
bool lhs, TfPyAnnotatedBoolResult<Annotation>& rhs)
140PXR_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.