24 #ifndef PXR_BASE_TF_PY_OPTIONAL_H
25 #define PXR_BASE_TF_PY_OPTIONAL_H
32 #include <boost/noncopyable.hpp>
33 #include <boost/optional.hpp>
34 #include <boost/python/converter/from_python.hpp>
35 #include <boost/python/extract.hpp>
36 #include <boost/python/to_python_converter.hpp>
37 #include <boost/python/to_python_value.hpp>
39 PXR_NAMESPACE_OPEN_SCOPE
44 namespace TfPyOptional {
46 template <
typename T,
typename TfromPy>
47 struct object_from_python
49 object_from_python() {
50 boost::python::converter::registry::push_back
51 (&TfromPy::convertible, &TfromPy::construct,
52 boost::python::type_id<T>());
56 template <
typename T,
typename TtoPy,
typename TfromPy>
57 struct register_python_conversion
59 register_python_conversion() {
60 boost::python::to_python_converter<T, TtoPy>();
61 object_from_python<T, TfromPy>();
66 struct python_optional :
public boost::noncopyable
68 struct optional_to_python
70 static PyObject * convert(
const boost::optional<T>& value)
73 boost::python::object obj =
TfPyObject(*value);
77 return boost::python::detail::none();
81 struct optional_from_python
83 static void * convertible(PyObject * source)
85 using namespace boost::python::converter;
87 if ((source == Py_None) || boost::python::extract<T>(source).check())
93 static void construct(PyObject * source,
94 boost::python::converter::rvalue_from_python_stage1_data * data)
96 using namespace boost::python::converter;
98 void *
const storage =
99 ((rvalue_from_python_storage<T> *)data)->storage.bytes;
101 if (data->convertible == Py_None) {
102 new (storage) boost::optional<T>();
104 new (storage) boost::optional<T>(boost::python::extract<T>(source));
107 data->convertible = storage;
111 explicit python_optional() {
112 register_python_conversion<boost::optional<T>,
113 optional_to_python, optional_from_python>();
119 PXR_NAMESPACE_CLOSE_SCOPE
121 #endif // PXR_BASE_TF_PY_OPTIONAL_H
boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Return a python object for the given C++ object, loading the appropriate wrapper code if necessary...
Miscellaneous Utilities for dealing with script.