24 #ifndef PXR_BASE_VT_VALUE_FROM_PYTHON_H
25 #define PXR_BASE_VT_VALUE_FROM_PYTHON_H
30 #include "pxr/base/vt/api.h"
31 #include "pxr/base/vt/value.h"
34 #include "pxr/base/tf/hashmap.h"
42 PXR_NAMESPACE_OPEN_SCOPE
46 class Vt_ValueFromPythonRegistry {
49 static bool HasConversions() {
50 return !_GetInstance()._lvalueExtractors.empty() &&
51 !_GetInstance()._rvalueExtractors.empty();
54 VT_API
static VtValue Invoke(PyObject *obj);
57 static void Register(
bool registerRvalue) {
59 TF_FATAL_ERROR(
"Tried to register a VtValue from python conversion "
60 "but python is not initialized!");
62 _GetInstance()._RegisterLValue(_Extractor::MakeLValue<T>());
64 _GetInstance()._RegisterRValue(_Extractor::MakeRValue<T>());
67 Vt_ValueFromPythonRegistry(Vt_ValueFromPythonRegistry
const&) =
delete;
68 Vt_ValueFromPythonRegistry& operator=(
69 Vt_ValueFromPythonRegistry
const&) =
delete;
71 Vt_ValueFromPythonRegistry(Vt_ValueFromPythonRegistry &&) =
delete;
72 Vt_ValueFromPythonRegistry& operator=(
73 Vt_ValueFromPythonRegistry &&) =
delete;
76 Vt_ValueFromPythonRegistry() {}
77 VT_API ~Vt_ValueFromPythonRegistry();
79 friend class TfSingleton<Vt_ValueFromPythonRegistry>;
83 using _ExtractFunc =
VtValue (*)(PyObject *);
89 static VtValue _ExtractLValue(PyObject *);
95 static VtValue _ExtractRValue(PyObject *);
100 static _Extractor MakeLValue() {
101 return _Extractor(&_ExtractLValue<T>);
105 static _Extractor MakeRValue() {
106 return _Extractor(&_ExtractRValue<T>);
109 VtValue Invoke(PyObject *obj)
const {
110 return _extract(obj);
114 explicit _Extractor(_ExtractFunc extract) : _extract(extract) {}
116 _ExtractFunc _extract;
119 VT_API
static Vt_ValueFromPythonRegistry &_GetInstance() {
123 VT_API
void _RegisterLValue(_Extractor
const &e);
124 VT_API
void _RegisterRValue(_Extractor
const &e);
126 std::vector<_Extractor> _lvalueExtractors;
127 std::vector<_Extractor> _rvalueExtractors;
129 typedef TfHashMap<PyObject *, _Extractor, TfHash> _LValueExtractorCache;
130 _LValueExtractorCache _lvalueExtractorCache;
137 VtValue Vt_ValueFromPythonRegistry::
138 _Extractor::_ExtractLValue(PyObject *obj) {
139 boost::python::extract<T &> x(obj);
146 VtValue Vt_ValueFromPythonRegistry::
147 _Extractor::_ExtractRValue(PyObject *obj) {
148 boost::python::extract<T> x(obj);
155 void VtValueFromPython() {
156 Vt_ValueFromPythonRegistry::Register<T>(
true);
160 void VtValueFromPythonLValue() {
161 Vt_ValueFromPythonRegistry::Register<T>(
false);
164 PXR_NAMESPACE_CLOSE_SCOPE
166 #endif // PXR_BASE_VT_VALUE_FROM_PYTHON_H
TF_API bool TfPyIsInitialized()
Returns true if python is initialized.
Manage a single instance of an object.
Manage a single instance of an object (see.
Miscellaneous Utilities for dealing with script.
Intended to replace a direct include of Python.h, which causes several build problems with certain co...
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Provides a container which may hold any type, and provides introspection and iteration over array typ...