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 #include <hboost/functional/hash.hpp>
32 #include <hboost/python/object_fwd.hpp>
33 #include <hboost/python/object_operators.hpp>
34 
35 #include <iosfwd>
36 #include <memory>
37 
39 
40 /// \class TfPyObjWrapper
41 ///
42 /// Boost Python object wrapper.
43 ///
44 /// Provides a wrapper around hboost::python::object that works correctly for the
45 /// following basic operations regardless of the GIL state: default construction,
46 /// copy construction, assignment, (in)equality comparison, hash_value(), and
47 /// destruction.
48 ///
49 /// None of those work correctly in the presence of an unlocked GIL for
50 /// hboost::python::object. This class only actually acquires the GIL for default
51 /// construction, destruction and for some (in)equality comparisons. The other
52 /// operations do not require taking the GIL.
53 ///
54 /// This is primarily useful in cases where a hboost::python::object might be
55 /// destroyed without a locked GIL by a client blind to that fact. This occurs
56 /// when a registry, for example, holds type-erased objects. If one
57 /// of the type-erased objects in the registry happens to hold a
58 /// hboost::python::object, that type-erased object must be destroyed while the
59 /// GIL is held but it's unreasonable to require that the registry know that.
60 /// This class helps solve that problem.
61 ///
62 /// This class also provides many of the operators that hboost::python::object
63 /// provides, by virtue of deriving from hboost::python::api::object_operators<T>.
64 /// However it is important to note that callers must ensure the GIL is held
65 /// before using these operators!
67  : public hboost::python::api::object_operators<TfPyObjWrapper>
68 {
69  typedef hboost::python::object object;
70 
71 public:
72 
73  /// Default construct a TfPyObjWrapper holding a reference to python None.
74  /// The GIL need not be held by the caller.
76 
77  /// Construct a TfPyObjectWrapper wrapping \a obj.
78  /// The GIL must be held by the caller. Note, allowing the implicit
79  /// conversion is intended here.
80  TF_API TfPyObjWrapper(object obj);
81 
82  /// Underlying object access.
83  /// This method returns a reference, so technically, the GIL need not be
84  /// held to call this. However, the caller is strongly advised to ensure
85  /// the GIL is held, since assigning this object to another or otherwise
86  /// operating on the returned object requires it.
87  object const &Get() const {
88  return *_objectPtr;
89  }
90 
91  /// Underlying PyObject* access.
92  /// This method returns a pointer, so technically, the GIL need not be
93  /// held to call this. However, the caller is strongly advised to ensure
94  /// the GIL is held, since assigning this object to another or otherwise
95  /// operating on the returned object requires it. The returned PyObject *
96  /// is a "borrowed reference", meaning that the underlying object's
97  /// reference count has not been incremented on behalf of the caller.
98  TF_API PyObject *ptr() const;
99 
100  /// Produce a hash code for this object.
101  /// Note that this does not attempt to hash the underlying python object,
102  /// it returns a hash code that's suitable for hash-table lookup of
103  /// TfPyObjWrapper instances, and does not require taking the python lock.
104  friend inline size_t hash_value(TfPyObjWrapper const &o) {
105  return (size_t) o.ptr();
106  }
107 
108  /// Equality.
109  /// Returns true if \a other refers to the same python object.
110  TF_API bool operator==(TfPyObjWrapper const &other) const;
111 
112  /// Inequality.
113  /// Returns false if \a other refers to the same python object.
114  TF_API bool operator!=(TfPyObjWrapper const &other) const;
115 
116 private:
117 
118  // Befriend object_operators to allow it access to implicit conversion to
119  // hboost::python::object.
120  friend class hboost::python::api::object_operators<TfPyObjWrapper>;
121  operator object const &() const {
122  return Get();
123  }
124 
125  // Store a shared_ptr to a python object.
126  std::shared_ptr<object> _objectPtr;
127 };
128 
130 
131 #endif // PXR_BASE_TF_PY_OBJ_WRAPPER_H
#define TF_API
Definition: api.h:40
GLhandleARB obj
Definition: glew.h:6236
TF_API bool operator==(TfPyObjWrapper const &other) const
TF_API PyObject * ptr() const
GLuint object
Definition: glew.h:8986
TF_API TfPyObjWrapper()
friend size_t hash_value(TfPyObjWrapper const &o)
Definition: pyObjWrapper.h:104
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
object const & Get() const
Definition: pyObjWrapper.h:87
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
TF_API bool operator!=(TfPyObjWrapper const &other) const