HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyOptional.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_TF_PY_OPTIONAL_H
8 #define PXR_BASE_TF_PY_OPTIONAL_H
9 
10 /// \file tf/pyOptional.h
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/base/tf/pyUtils.h"
15 #include "pxr/external/boost/python/converter/from_python.hpp"
16 #include "pxr/external/boost/python/extract.hpp"
17 #include "pxr/external/boost/python/to_python_converter.hpp"
18 #include "pxr/external/boost/python/to_python_value.hpp"
19 
20 #include <optional>
21 
23 
24 // Adapted from original at:
25 // http://mail.python.org/pipermail/cplusplus-sig/2007-May/012003.html
26 
27 namespace TfPyOptional {
28 
29 template <typename T, typename TfromPy>
31 {
33  pxr_boost::python::converter::registry::push_back
34  (&TfromPy::convertible, &TfromPy::construct,
35  pxr_boost::python::type_id<T>());
36  }
37 };
38 
39 template <typename T, typename TtoPy, typename TfromPy>
41 {
43  pxr_boost::python::to_python_converter<T, TtoPy>();
45  }
46 };
47 
48 template <typename T>
50 {
51  python_optional(const python_optional&) = delete;
52  python_optional& operator=(const python_optional&) = delete;
53  template <typename Optional>
55  {
56  static PyObject * convert(const Optional& value)
57  {
58  if (value) {
60  Py_INCREF(obj.ptr());
61  return obj.ptr();
62  }
64  }
65  };
66 
67  template <typename Optional>
69  {
70  static void * convertible(PyObject * source)
71  {
72  using namespace pxr_boost::python::converter;
73 
74  if ((source == Py_None) || pxr_boost::python::extract<T>(source).check())
75  return source;
76 
77  return NULL;
78  }
79 
80  static void construct(PyObject * source,
81  pxr_boost::python::converter::rvalue_from_python_stage1_data * data)
82  {
83  using namespace pxr_boost::python::converter;
84 
85  void * const storage =
86  ((rvalue_from_python_storage<T> *)data)->storage.bytes;
87 
88  if (data->convertible == Py_None) {
89  new (storage) Optional(); // An uninitialized optional
90  } else {
91  new (storage) Optional(pxr_boost::python::extract<T>(source));
92  }
93 
94  data->convertible = storage;
95  }
96  };
97 
98  explicit python_optional() {
100  std::optional<T>,
103  }
104 };
105 
106 } // namespace TfPyOptional
107 
109 
110 #endif // PXR_BASE_TF_PY_OPTIONAL_H
bool none(const vbool4 &v)
Definition: simd.h:3601
pxr_boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Definition: pyUtils.h:127
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
static PyObject * convert(const Optional &value)
Definition: pyOptional.h:56
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static void construct(PyObject *source, pxr_boost::python::converter::rvalue_from_python_stage1_data *data)
Definition: pyOptional.h:80
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: format.h:1821
python_optional & operator=(const python_optional &)=delete
static void * convertible(PyObject *source)
Definition: pyOptional.h:70