Loading...
Searching...
No Matches
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"
31
32#ifdef PXR_PYTHON_SUPPORT_ENABLED
33// Include this header first to pick up additional mitigations
34// for build issues when including Python.h
36
37#include <boost/functional/hash.hpp>
38#include <boost/python/object_fwd.hpp>
39#include <boost/python/object_operators.hpp>
40
41#include <iosfwd>
42#include <memory>
43
44#else
45
46#include <type_traits>
47
48#endif
49
50PXR_NAMESPACE_OPEN_SCOPE
51
52// We define the empty stub for ABI compatibility even if Python support is
53// enabled so we can make sure size and alignment is the same.
54class TfPyObjWrapperStub
55{
56public:
57 static constexpr std::size_t Size = 16;
58 static constexpr std::size_t Align = 8;
59
60private:
61 ARCH_PRAGMA_PUSH
62 ARCH_PRAGMA_UNUSED_PRIVATE_FIELD
63 std::aligned_storage<Size, Align>::type _stub;
64 ARCH_PRAGMA_POP
65};
66
67
94#ifdef PXR_PYTHON_SUPPORT_ENABLED
96 : public boost::python::api::object_operators<TfPyObjWrapper>
97{
98 typedef boost::python::object object;
99
100public:
101
105
109 TF_API TfPyObjWrapper(object obj);
110
116 object const &Get() const {
117 return *_objectPtr;
118 }
119
127 TF_API PyObject *ptr() const;
128
133 friend inline size_t hash_value(TfPyObjWrapper const &o) {
134 return (size_t) o.ptr();
135 }
136
139 TF_API bool operator==(TfPyObjWrapper const &other) const;
140
143 TF_API bool operator!=(TfPyObjWrapper const &other) const;
144
145private:
146
147 // Befriend object_operators to allow it access to implicit conversion to
148 // boost::python::object.
149 friend class boost::python::api::object_operators<TfPyObjWrapper>;
150 operator object const &() const {
151 return Get();
152 }
153
154 // Store a shared_ptr to a python object.
155 std::shared_ptr<object> _objectPtr;
156};
157
158static_assert(sizeof(TfPyObjWrapper) == sizeof(TfPyObjWrapperStub),
159 "ABI break: Incompatible class sizes.");
160static_assert(alignof(TfPyObjWrapper) == alignof(TfPyObjWrapperStub),
161 "ABI break: Incompatible class alignments.");
162
163#else // PXR_PYTHON_SUPPORT_ENABLED
164
165class TfPyObjWrapper : TfPyObjWrapperStub
166{
167};
168
169#endif // PXR_PYTHON_SUPPORT_ENABLED
170
171PXR_NAMESPACE_CLOSE_SCOPE
172
173#endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
Boost Python object wrapper.
Definition: pyObjWrapper.h:97
TF_API bool operator==(TfPyObjWrapper const &other) const
Equality.
TF_API PyObject * ptr() const
Underlying PyObject* access.
friend size_t hash_value(TfPyObjWrapper const &o)
Produce a hash code for this object.
Definition: pyObjWrapper.h:133
object const & Get() const
Underlying object access.
Definition: pyObjWrapper.h:116
TF_API bool operator!=(TfPyObjWrapper const &other) const
Inequality.
TF_API TfPyObjWrapper()
Default construct a TfPyObjWrapper holding a reference to python None.
TF_API TfPyObjWrapper(object obj)
Construct a TfPyObjectWrapper wrapping obj.
Pragmas for controlling compiler-specific behaviors.
Intended to replace a direct include of Python.h, which causes several build problems with certain co...