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