HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valueFromPython.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_VT_VALUE_FROM_PYTHON_H
8 #define PXR_BASE_VT_VALUE_FROM_PYTHON_H
9 
10 /// \file vt/valueFromPython.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/base/vt/api.h"
14 #include "pxr/base/vt/value.h"
15 
16 #include "pxr/base/tf/hash.h"
17 #include "pxr/base/tf/hashmap.h"
18 #include "pxr/base/tf/pyUtils.h"
19 #include "pxr/base/tf/singleton.h"
20 
22 
23 #include <vector>
24 
26 
27 /// \class Vt_ValueFromPythonRegistry
28 ///
30 public:
31 
32  static bool HasConversions() {
33  return !_GetInstance()._lvalueExtractors.empty() &&
34  !_GetInstance()._rvalueExtractors.empty();
35  }
36 
37  VT_API static VtValue Invoke(PyObject *obj);
38 
39  template <class T>
40  static void Register(bool registerRvalue) {
41  if (!TfPyIsInitialized()) {
42  TF_FATAL_ERROR("Tried to register a VtValue from python conversion "
43  "but python is not initialized!");
44  }
45  _GetInstance()._RegisterLValue(_Extractor::MakeLValue<T>());
46  if (registerRvalue)
47  _GetInstance()._RegisterRValue(_Extractor::MakeRValue<T>());
48  }
49 
52  Vt_ValueFromPythonRegistry const&) = delete;
53 
56  Vt_ValueFromPythonRegistry &&) = delete;
57 
58 private:
61 
63 
64  class _Extractor {
65  private:
66  using _ExtractFunc = VtValue (*)(PyObject *);
67 
68  // _ExtractLValue will attempt to obtain an l-value T from the python
69  // object it's passed. This effectively disallows type conversions
70  // (other than things like derived-to-base type conversions).
71  template <class T>
72  static VtValue _ExtractLValue(PyObject *);
73 
74  // _ExtractRValue will attempt to obtain an r-value T from the python
75  // object it's passed. This allows boost.python to invoke type
76  // conversions to produce the T.
77  template <class T>
78  static VtValue _ExtractRValue(PyObject *);
79 
80  public:
81 
82  template <class T>
83  static _Extractor MakeLValue() {
84  return _Extractor(&_ExtractLValue<T>);
85  }
86 
87  template <class T>
88  static _Extractor MakeRValue() {
89  return _Extractor(&_ExtractRValue<T>);
90  }
91 
92  VtValue Invoke(PyObject *obj) const {
93  return _extract(obj);
94  }
95 
96  private:
97  explicit _Extractor(_ExtractFunc extract) : _extract(extract) {}
98 
99  _ExtractFunc _extract;
100  };
101 
102  VT_API static Vt_ValueFromPythonRegistry &_GetInstance() {
104  }
105 
106  VT_API void _RegisterLValue(_Extractor const &e);
107  VT_API void _RegisterRValue(_Extractor const &e);
108 
109  std::vector<_Extractor> _lvalueExtractors;
110  std::vector<_Extractor> _rvalueExtractors;
111 
112  typedef TfHashMap<PyObject *, _Extractor, TfHash> _LValueExtractorCache;
113  _LValueExtractorCache _lvalueExtractorCache;
114 
115 };
116 
118 
119 template <class T>
120 VtValue Vt_ValueFromPythonRegistry::
121 _Extractor::_ExtractLValue(PyObject *obj) {
122  pxr_boost::python::extract<T &> x(obj);
123  if (x.check())
124  return VtValue(x());
125  return VtValue();
126 }
127 
128 template <class T>
129 VtValue Vt_ValueFromPythonRegistry::
130 _Extractor::_ExtractRValue(PyObject *obj) {
131  pxr_boost::python::extract<T> x(obj);
132  if (x.check())
133  return VtValue(x());
134  return VtValue();
135 }
136 
137 template <class T>
139  Vt_ValueFromPythonRegistry::Register<T>(/* registerRvalue = */ true);
140 }
141 
142 template <class T>
144  Vt_ValueFromPythonRegistry::Register<T>(/* registerRvalue = */ false);
145 }
146 
148 
149 #endif // PXR_BASE_VT_VALUE_FROM_PYTHON_H
TF_API bool TfPyIsInitialized()
Returns true if python is initialized.
static T & GetInstance()
Definition: singleton.h:120
#define VT_API
Definition: api.h:23
static VT_API VtValue Invoke(PyObject *obj)
OIIO_FORCEINLINE bool extract(const vbool4 &a)
Definition: simd.h:3542
static void Register(bool registerRvalue)
#define TF_FATAL_ERROR
Vt_ValueFromPythonRegistry & operator=(Vt_ValueFromPythonRegistry const &)=delete
VT_API_TEMPLATE_CLASS(TfSingleton< Vt_ValueFromPythonRegistry >)
GLint GLenum GLint x
Definition: glcorearb.h:409
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
void VtValueFromPythonLValue()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void VtValueFromPython()
Definition: value.h:146