Loading...
Searching...
No Matches
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
28
29#include "pxr/pxr.h"
30
31#include "pxr/base/tf/pyUtils.h"
32#include <boost/noncopyable.hpp>
33#include <boost/optional.hpp>
34#include <boost/python/converter/from_python.hpp>
35#include <boost/python/extract.hpp>
36#include <boost/python/to_python_converter.hpp>
37#include <boost/python/to_python_value.hpp>
38
39#include <optional>
40
41PXR_NAMESPACE_OPEN_SCOPE
42
43// Adapted from original at:
44// http://mail.python.org/pipermail/cplusplus-sig/2007-May/012003.html
45
46namespace TfPyOptional {
47
48template <typename T, typename TfromPy>
49struct object_from_python
50{
51 object_from_python() {
52 boost::python::converter::registry::push_back
53 (&TfromPy::convertible, &TfromPy::construct,
54 boost::python::type_id<T>());
55 }
56};
57
58template <typename T, typename TtoPy, typename TfromPy>
59struct register_python_conversion
60{
61 register_python_conversion() {
62 boost::python::to_python_converter<T, TtoPy>();
63 object_from_python<T, TfromPy>();
64 }
65};
66
67template <typename T>
68struct python_optional : public boost::noncopyable
69{
70 template <typename Optional>
71 struct optional_to_python
72 {
73 static PyObject * convert(const Optional& value)
74 {
75 if (value) {
76 boost::python::object obj = TfPyObject(*value);
77 Py_INCREF(obj.ptr());
78 return obj.ptr();
79 }
80 return boost::python::detail::none();
81 }
82 };
83
84 template <typename Optional>
85 struct optional_from_python
86 {
87 static void * convertible(PyObject * source)
88 {
89 using namespace boost::python::converter;
90
91 if ((source == Py_None) || boost::python::extract<T>(source).check())
92 return source;
93
94 return NULL;
95 }
96
97 static void construct(PyObject * source,
98 boost::python::converter::rvalue_from_python_stage1_data * data)
99 {
100 using namespace boost::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(boost::python::extract<T>(source));
109 }
110
111 data->convertible = storage;
112 }
113 };
114
115 explicit python_optional() {
116 register_python_conversion<
117 std::optional<T>,
118 optional_to_python<std::optional<T>>,
119 optional_from_python<std::optional<T>>>();
120 register_python_conversion<
121 boost::optional<T>,
122 optional_to_python<boost::optional<T>>,
123 optional_from_python<boost::optional<T>>>();
124 }
125};
126
127} // namespace TfPyOptional
128
129PXR_NAMESPACE_CLOSE_SCOPE
130
131#endif // PXR_BASE_TF_PY_OPTIONAL_H
Miscellaneous Utilities for dealing with script.
boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Return a python object for the given C++ object, loading the appropriate wrapper code if necessary.
Definition: pyUtils.h:144