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 
40 
41 // Adapted from original at:
42 // http://mail.python.org/pipermail/cplusplus-sig/2007-May/012003.html
43 
44 namespace TfPyOptional {
45 
46 template <typename T, typename TfromPy>
48 {
50  hboost::python::converter::registry::push_back
51  (&TfromPy::convertible, &TfromPy::construct,
52  hboost::python::type_id<T>());
53  }
54 };
55 
56 template <typename T, typename TtoPy, typename TfromPy>
58 {
60  hboost::python::to_python_converter<T, TtoPy>();
62  }
63 };
64 
65 template <typename T>
66 struct python_optional : public hboost::noncopyable
67 {
69  {
70  static PyObject * convert(const hboost::optional<T>& value)
71  {
72  if (value) {
74  Py_INCREF(obj.ptr());
75  return obj.ptr();
76  }
78  }
79  };
80 
82  {
83  static void * convertible(PyObject * source)
84  {
85  using namespace hboost::python::converter;
86 
87  if ((source == Py_None) || hboost::python::extract<T>(source).check())
88  return source;
89 
90  return NULL;
91  }
92 
93  static void construct(PyObject * source,
94  hboost::python::converter::rvalue_from_python_stage1_data * data)
95  {
96  using namespace hboost::python::converter;
97 
98  void * const storage =
99  ((rvalue_from_python_storage<T> *)data)->storage.bytes;
100 
101  if (data->convertible == Py_None) {
102  new (storage) hboost::optional<T>(); // An uninitialized optional
103  } else {
104  new (storage) hboost::optional<T>(hboost::python::extract<T>(source));
105  }
106 
107  data->convertible = storage;
108  }
109  };
110 
111  explicit python_optional() {
114  }
115 };
116 
117 } // namespace TfPyOptional
118 
120 
121 #endif // PXR_BASE_TF_PY_OPTIONAL_H
bool none(const vbool4 &v)
Definition: simd.h:3373
static void construct(PyObject *source, hboost::python::converter::rvalue_from_python_stage1_data *data)
Definition: pyOptional.h:93
GLsizei GLsizei GLchar * source
Definition: glew.h:1832
GLhandleARB obj
Definition: glew.h:6236
static void * convertible(PyObject *source)
Definition: pyOptional.h:83
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
GLuint object
Definition: glew.h:8986
hboost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Definition: pyUtils.h:102
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
getOption("OpenEXR.storage") storage
Definition: HDK_Image.dox:276
GLsizei const GLfloat * value
Definition: glew.h:1849
static PyObject * convert(const hboost::optional< T > &value)
Definition: pyOptional.h:70