All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 "pxr/external/boost/python/object_fwd.hpp"
21#include "pxr/external/boost/python/object_operators.hpp"
22
23#include <iosfwd>
24#include <memory>
25
26#else
27
28#include <type_traits>
29
30#endif
31
32PXR_NAMESPACE_OPEN_SCOPE
33
34// We define the empty stub for ABI compatibility even if Python support is
35// enabled so we can make sure size and alignment is the same.
36class TfPyObjWrapperStub
37{
38public:
39 static constexpr std::size_t Size = 16;
40 static constexpr std::size_t Align = 8;
41
42private:
43 ARCH_PRAGMA_PUSH
44 ARCH_PRAGMA_UNUSED_PRIVATE_FIELD
45 std::aligned_storage<Size, Align>::type _stub;
46 ARCH_PRAGMA_POP
47};
48
49
76#ifdef PXR_PYTHON_SUPPORT_ENABLED
78 : public pxr_boost::python::api::object_operators<TfPyObjWrapper>
79{
80 typedef pxr_boost::python::object object;
81
82public:
83
87
91 TF_API TfPyObjWrapper(object obj);
92
98 object const &Get() const {
99 return *_objectPtr;
100 }
101
109 TF_API PyObject *ptr() const;
110
115 friend inline size_t hash_value(TfPyObjWrapper const &o) {
116 return (size_t) o.ptr();
117 }
118
121 TF_API bool operator==(TfPyObjWrapper const &other) const;
122
125 TF_API bool operator!=(TfPyObjWrapper const &other) const;
126
127private:
128
129 // Befriend object_operators to allow it access to implicit conversion to
130 // pxr_boost::python::object.
131 friend class pxr_boost::python::api::object_operators<TfPyObjWrapper>;
132 operator object const &() const {
133 return Get();
134 }
135
136 // Store a shared_ptr to a python object.
137 std::shared_ptr<object> _objectPtr;
138};
139
140static_assert(sizeof(TfPyObjWrapper) == sizeof(TfPyObjWrapperStub),
141 "ABI break: Incompatible class sizes.");
142static_assert(alignof(TfPyObjWrapper) == alignof(TfPyObjWrapperStub),
143 "ABI break: Incompatible class alignments.");
144
145#else // PXR_PYTHON_SUPPORT_ENABLED
146
147class TfPyObjWrapper : TfPyObjWrapperStub
148{
149};
150
151#endif // PXR_PYTHON_SUPPORT_ENABLED
152
153PXR_NAMESPACE_CLOSE_SCOPE
154
155#endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
Boost Python object wrapper.
Definition: pyObjWrapper.h:79
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:115
object const & Get() const
Underlying object access.
Definition: pyObjWrapper.h:98
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...