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