HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyObjWrapper.h
Go to the documentation of this file.
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"
13 #include "pxr/base/arch/pragmas.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 
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.
37 {
38 public:
39  static constexpr std::size_t Size = 16;
40  static constexpr std::size_t Align = 8;
41 
42 private:
47 };
48 
49 
50 /// \class TfPyObjWrapper
51 ///
52 /// Boost Python object wrapper.
53 ///
54 /// Provides a wrapper around pxr_boost::python::object that works correctly for the
55 /// following basic operations regardless of the GIL state: default construction,
56 /// copy construction, assignment, (in)equality comparison, hash_value(), and
57 /// destruction.
58 ///
59 /// None of those work correctly in the presence of an unlocked GIL for
60 /// pxr_boost::python::object. This class only actually acquires the GIL for default
61 /// construction, destruction and for some (in)equality comparisons. The other
62 /// operations do not require taking the GIL.
63 ///
64 /// This is primarily useful in cases where a pxr_boost::python::object might be
65 /// destroyed without a locked GIL by a client blind to that fact. This occurs
66 /// when a registry, for example, holds type-erased objects. If one
67 /// of the type-erased objects in the registry happens to hold a
68 /// pxr_boost::python::object, that type-erased object must be destroyed while the
69 /// GIL is held but it's unreasonable to require that the registry know that.
70 /// This class helps solve that problem.
71 ///
72 /// This class also provides many of the operators that pxr_boost::python::object
73 /// provides, by virtue of deriving from pxr_boost::python::api::object_operators<T>.
74 /// However it is important to note that callers must ensure the GIL is held
75 /// before using these operators!
76 #ifdef PXR_PYTHON_SUPPORT_ENABLED
77 class TfPyObjWrapper
78  : public pxr_boost::python::api::object_operators<TfPyObjWrapper>
79 {
81 
82 public:
83 
84  /// Default construct a TfPyObjWrapper holding a reference to python None.
85  /// The GIL need not be held by the caller.
87 
88  /// Construct a TfPyObjectWrapper wrapping \a obj.
89  /// The GIL must be held by the caller. Note, allowing the implicit
90  /// conversion is intended here.
91  TF_API TfPyObjWrapper(object obj);
92 
93  /// Underlying object access.
94  /// This method returns a reference, so technically, the GIL need not be
95  /// held to call this. However, the caller is strongly advised to ensure
96  /// the GIL is held, since assigning this object to another or otherwise
97  /// operating on the returned object requires it.
98  object const &Get() const {
99  return *_objectPtr;
100  }
101 
102  /// Underlying PyObject* access.
103  /// This method returns a pointer, so technically, the GIL need not be
104  /// held to call this. However, the caller is strongly advised to ensure
105  /// the GIL is held, since assigning this object to another or otherwise
106  /// operating on the returned object requires it. The returned PyObject *
107  /// is a "borrowed reference", meaning that the underlying object's
108  /// reference count has not been incremented on behalf of the caller.
109  TF_API PyObject *ptr() const;
110 
111  /// Produce a hash code for this object.
112  /// Note that this does not attempt to hash the underlying python object,
113  /// it returns a hash code that's suitable for hash-table lookup of
114  /// TfPyObjWrapper instances, and does not require taking the python lock.
115  friend inline size_t hash_value(TfPyObjWrapper const &o) {
116  return (size_t) o.ptr();
117  }
118 
119  /// Equality.
120  /// Returns true if \a other refers to the same python object.
121  TF_API bool operator==(TfPyObjWrapper const &other) const;
122 
123  /// Inequality.
124  /// Returns false if \a other refers to the same python object.
125  TF_API bool operator!=(TfPyObjWrapper const &other) const;
126 
127 private:
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 
140 static_assert(sizeof(TfPyObjWrapper) == sizeof(TfPyObjWrapperStub),
141  "ABI break: Incompatible class sizes.");
142 static_assert(alignof(TfPyObjWrapper) == alignof(TfPyObjWrapperStub),
143  "ABI break: Incompatible class alignments.");
144 
145 #else // PXR_PYTHON_SUPPORT_ENABLED
146 
147 class TfPyObjWrapper : TfPyObjWrapperStub
148 {
149 };
150 
151 #endif // PXR_PYTHON_SUPPORT_ENABLED
152 
154 
155 #endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
type
Definition: core.h:556
#define TF_API
Definition: api.h:23
#define ARCH_PRAGMA_UNUSED_PRIVATE_FIELD
Definition: pragmas.h:195
static constexpr std::size_t Align
Definition: pyObjWrapper.h:40
#define ARCH_PRAGMA_POP
Definition: pragmas.h:159
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
#define ARCH_PRAGMA_PUSH
Definition: pragmas.h:155
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static constexpr std::size_t Size
Definition: pyObjWrapper.h:39
auto ptr(T p) -> const void *
Definition: format.h:4331
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
size_t hash_value(const CH_ChannelRef &ref)