Loading...
Searching...
No Matches
pyObjWrapper.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_PY_OBJ_WRAPPER_H
8#define PXR_BASE_TF_PY_OBJ_WRAPPER_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/base/tf/api.h"
14
15#ifdef PXR_PYTHON_SUPPORT_ENABLED
16// Include this header first to pick up additional mitigations
17// for build issues when including Python.h
19
20#include <boost/functional/hash.hpp>
21#include <boost/python/object_fwd.hpp>
22#include <boost/python/object_operators.hpp>
23
24#include <iosfwd>
25#include <memory>
26
27#else
28
29#include <type_traits>
30
31#endif
32
33PXR_NAMESPACE_OPEN_SCOPE
34
35// We define the empty stub for ABI compatibility even if Python support is
36// enabled so we can make sure size and alignment is the same.
37class TfPyObjWrapperStub
38{
39public:
40 static constexpr std::size_t Size = 16;
41 static constexpr std::size_t Align = 8;
42
43private:
44 ARCH_PRAGMA_PUSH
45 ARCH_PRAGMA_UNUSED_PRIVATE_FIELD
46 std::aligned_storage<Size, Align>::type _stub;
47 ARCH_PRAGMA_POP
48};
49
50
77#ifdef PXR_PYTHON_SUPPORT_ENABLED
79 : public boost::python::api::object_operators<TfPyObjWrapper>
80{
81 typedef boost::python::object object;
82
83public:
84
88
92 TF_API TfPyObjWrapper(object obj);
93
99 object const &Get() const {
100 return *_objectPtr;
101 }
102
110 TF_API PyObject *ptr() const;
111
116 friend inline size_t hash_value(TfPyObjWrapper const &o) {
117 return (size_t) o.ptr();
118 }
119
122 TF_API bool operator==(TfPyObjWrapper const &other) const;
123
126 TF_API bool operator!=(TfPyObjWrapper const &other) const;
127
128private:
129
130 // Befriend object_operators to allow it access to implicit conversion to
131 // boost::python::object.
132 friend class boost::python::api::object_operators<TfPyObjWrapper>;
133 operator object const &() const {
134 return Get();
135 }
136
137 // Store a shared_ptr to a python object.
138 std::shared_ptr<object> _objectPtr;
139};
140
141static_assert(sizeof(TfPyObjWrapper) == sizeof(TfPyObjWrapperStub),
142 "ABI break: Incompatible class sizes.");
143static_assert(alignof(TfPyObjWrapper) == alignof(TfPyObjWrapperStub),
144 "ABI break: Incompatible class alignments.");
145
146#else // PXR_PYTHON_SUPPORT_ENABLED
147
148class TfPyObjWrapper : TfPyObjWrapperStub
149{
150};
151
152#endif // PXR_PYTHON_SUPPORT_ENABLED
153
154PXR_NAMESPACE_CLOSE_SCOPE
155
156#endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
Boost Python object wrapper.
Definition: pyObjWrapper.h:80
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:116
object const & Get() const
Underlying object access.
Definition: pyObjWrapper.h:99
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...