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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_PY_OPTIONAL_H
25 #define PXR_BASE_TF_PY_OPTIONAL_H
26 
27 /// \file tf/pyOptional.h
28 
29 #include "pxr/pxr.h"
30 
31 #include "pxr/base/tf/pyUtils.h"
32 #include <hboost/noncopyable.hpp>
33 #include <hboost/optional.hpp>
34 #include <hboost/python/converter/from_python.hpp>
35 #include <hboost/python/extract.hpp>
36 #include <hboost/python/to_python_converter.hpp>
37 #include <hboost/python/to_python_value.hpp>
38 
39 #include <optional>
40 
42 
43 // Adapted from original at:
44 // http://mail.python.org/pipermail/cplusplus-sig/2007-May/012003.html
45 
46 namespace TfPyOptional {
47 
48 template <typename T, typename TfromPy>
50 {
52  hboost::python::converter::registry::push_back
53  (&TfromPy::convertible, &TfromPy::construct,
54  hboost::python::type_id<T>());
55  }
56 };
57 
58 template <typename T, typename TtoPy, typename TfromPy>
60 {
62  hboost::python::to_python_converter<T, TtoPy>();
64  }
65 };
66 
67 template <typename T>
68 struct python_optional : public hboost::noncopyable
69 {
70  template <typename Optional>
72  {
73  static PyObject * convert(const Optional& value)
74  {
75  if (value) {
76  hboost::python::object obj = TfPyObject(*value);
77  Py_INCREF(obj.ptr());
78  return obj.ptr();
79  }
80  return hboost::python::detail::none();
81  }
82  };
83 
84  template <typename Optional>
86  {
87  static void * convertible(PyObject * source)
88  {
89  using namespace hboost::python::converter;
90 
91  if ((source == Py_None) || hboost::python::extract<T>(source).check())
92  return source;
93 
94  return NULL;
95  }
96 
97  static void construct(PyObject * source,
98  hboost::python::converter::rvalue_from_python_stage1_data * data)
99  {
100  using namespace hboost::python::converter;
101 
102  void * const storage =
103  ((rvalue_from_python_storage<T> *)data)->storage.bytes;
104 
105  if (data->convertible == Py_None) {
106  new (storage) Optional(); // An uninitialized optional
107  } else {
108  new (storage) Optional(hboost::python::extract<T>(source));
109  }
110 
111  data->convertible = storage;
112  }
113  };
114 
115  explicit python_optional() {
117  std::optional<T>,
121  hboost::optional<T>,
124  }
125 };
126 
127 } // namespace TfPyOptional
128 
130 
131 #endif // PXR_BASE_TF_PY_OPTIONAL_H
getFileOption("OpenEXR:storage") storage
Definition: HDK_Image.dox:276
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
static PyObject * convert(const Optional &value)
Definition: pyOptional.h:73
hboost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Definition: pyUtils.h:144
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
static void construct(PyObject *source, hboost::python::converter::rvalue_from_python_stage1_data *data)
Definition: pyOptional.h:97
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: core.h:1131
Definition: format.h:895
static void * convertible(PyObject *source)
Definition: pyOptional.h:87