pyObjWrapper.h
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_OBJ_WRAPPER_H
25 #define PXR_BASE_TF_PY_OBJ_WRAPPER_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/tf/api.h"
30 
31 #ifdef PXR_PYTHON_SUPPORT_ENABLED
32 // Include this header first to pick up additional mitigations
33 // for build issues when including Python.h
35 
36 #include <boost/functional/hash.hpp>
37 #include <boost/python/object_fwd.hpp>
38 #include <boost/python/object_operators.hpp>
39 
40 #include <iosfwd>
41 #include <memory>
42 
43 #else
44 
45 #include <type_traits>
46 
47 #endif
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 // We define the empty stub for ABI compatibility even if Python support is
52 // enabled so we can make sure size and alignment is the same.
53 class TfPyObjWrapperStub
54 {
55 public:
56  static constexpr std::size_t Size = 16;
57  static constexpr std::size_t Align = 8;
58 
59 private:
60  std::aligned_storage<Size, Align>::type _stub;
61 };
62 
63 
90 #ifdef PXR_PYTHON_SUPPORT_ENABLED
91 class TfPyObjWrapper
92  : public boost::python::api::object_operators<TfPyObjWrapper>
93 {
94  typedef boost::python::object object;
95 
96 public:
97 
100  TF_API TfPyObjWrapper();
101 
105  TF_API TfPyObjWrapper(object obj);
106 
112  object const &Get() const {
113  return *_objectPtr;
114  }
115 
123  TF_API PyObject *ptr() const;
124 
129  friend inline size_t hash_value(TfPyObjWrapper const &o) {
130  return (size_t) o.ptr();
131  }
132 
135  TF_API bool operator==(TfPyObjWrapper const &other) const;
136 
139  TF_API bool operator!=(TfPyObjWrapper const &other) const;
140 
141 private:
142 
143  // Befriend object_operators to allow it access to implicit conversion to
144  // boost::python::object.
145  friend class boost::python::api::object_operators<TfPyObjWrapper>;
146  operator object const &() const {
147  return Get();
148  }
149 
150  // Store a shared_ptr to a python object.
151  std::shared_ptr<object> _objectPtr;
152 };
153 
154 static_assert(sizeof(TfPyObjWrapper) == sizeof(TfPyObjWrapperStub),
155  "ABI break: Incompatible class sizes.");
156 static_assert(alignof(TfPyObjWrapper) == alignof(TfPyObjWrapperStub),
157  "ABI break: Incompatible class alignments.");
158 
159 #else // PXR_PYTHON_SUPPORT_ENABLED
160 
161 class TfPyObjWrapper : TfPyObjWrapperStub
162 {
163 };
164 
165 #endif // PXR_PYTHON_SUPPORT_ENABLED
166 
167 PXR_NAMESPACE_CLOSE_SCOPE
168 
169 #endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
Intended to replace a direct include of Python.h, which causes several build problems with certain co...
Boost Python object wrapper.
Definition: pyObjWrapper.h:161
std::enable_if< std::is_same< Half, half >::value, size_t >::type hash_value(const Half &h)
Overload hash_value for half.
Definition: half.h:50
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.