anyWeakPtr.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_ANY_WEAK_PTR_H
25 #define PXR_BASE_TF_ANY_WEAK_PTR_H
26 
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/tf/api.h"
33 #include "pxr/base/tf/cxxCast.h"
34 #include "pxr/base/tf/type.h"
35 #include "pxr/base/tf/weakPtr.h"
36 
37 #ifdef PXR_PYTHON_SUPPORT_ENABLED
38 #include "pxr/base/tf/pyUtils.h"
39 #endif // PXR_PYTHON_SUPPORT_ENABLED
40 
41 #include "pxr/base/tf/pyObjWrapper.h"
42 #include <boost/operators.hpp>
43 
44 #include <cstddef>
45 #include <type_traits>
46 #include <utility>
47 
48 PXR_NAMESPACE_OPEN_SCOPE
49 
54 class TfAnyWeakPtr : boost::totally_ordered<TfAnyWeakPtr>
55 {
56  struct _Data {
57  void* space[4];
58  };
59 
60 public:
61  typedef TfAnyWeakPtr This;
62 
64  template <class Ptr, class = typename
65  std::enable_if<Tf_SupportsWeakPtr<
66  typename Ptr::DataType>::value>::type>
67  TfAnyWeakPtr(Ptr const &ptr) {
68  static_assert(sizeof(_PointerHolder<Ptr>) <= sizeof(_Data),
69  "Ptr is too big to fit in a TfAnyWeakPtr");
70  new (&_ptrStorage) _PointerHolder<Ptr>(ptr);
71  }
72 
75  static_assert(sizeof(_EmptyHolder) <= sizeof(_Data),
76  "Ptr is too big to fit in a TfAnyWeakPtr");
77  new (&_ptrStorage) _EmptyHolder;
78  }
79 
81  TfAnyWeakPtr(TfNullPtrType) : TfAnyWeakPtr() {}
82 
84  TfAnyWeakPtr(std::nullptr_t) : TfAnyWeakPtr() {}
85 
86  TfAnyWeakPtr(TfAnyWeakPtr const &other) {
87  other._Get()->Clone(&_ptrStorage);
88  }
89 
90  TfAnyWeakPtr &operator=(TfAnyWeakPtr const &other) {
91  if (this != &other) {
92  _Get()->~_PointerHolderBase();
93  other._Get()->Clone(&_ptrStorage);
94  }
95  return *this;
96  }
97 
98  ~TfAnyWeakPtr() {
99  _Get()->~_PointerHolderBase();
100  }
101 
104  TF_API bool IsInvalid() const;
105 
107  TF_API void const *GetUniqueIdentifier() const;
108 
110  TF_API TfWeakBase const *GetWeakBase() const;
111 
113  TF_API operator bool() const;
114 
116  TF_API bool operator !() const;
117 
119  TF_API bool operator ==(const TfAnyWeakPtr &rhs) const;
120 
122  TF_API bool operator <(const TfAnyWeakPtr &rhs) const;
123 
125  TF_API const std::type_info & GetTypeInfo() const;
126 
128  TF_API TfType const& GetType() const;
129 
131  size_t GetHash() const {
132  return reinterpret_cast<uintptr_t>(GetUniqueIdentifier()) >> 3;
133  }
134 
135  private:
136 #ifdef PXR_PYTHON_SUPPORT_ENABLED
137  // This grants friend access to a function in the wrapper file for this
138  // class. This lets the wrapper reach down into an AnyWeakPtr to get a
139  // boost::python wrapped object corresponding to the held type. This
140  // facility is necessary to get the python API we want.
141  friend boost::python::api::object
142  Tf_GetPythonObjectFromAnyWeakPtr(This const &self);
143 
144  TF_API
145  boost::python::api::object _GetPythonObject() const;
146 #endif // PXR_PYTHON_SUPPORT_ENABLED
147 
148  template <class WeakPtr>
149  friend WeakPtr TfAnyWeakPtrDynamicCast(const TfAnyWeakPtr &anyWeak, WeakPtr*);
150 
151  // This is using the standard type-erasure pattern.
152  struct _PointerHolderBase {
153  TF_API virtual ~_PointerHolderBase();
154  virtual void Clone(_Data *target) const = 0;
155  virtual bool IsInvalid() const = 0;
156  virtual void const * GetUniqueIdentifier() const = 0;
157  virtual TfWeakBase const *GetWeakBase() const = 0;
158  virtual operator bool() const = 0;
159  virtual bool _IsConst() const = 0;
160  virtual TfPyObjWrapper GetPythonObject() const = 0;
161  virtual const std::type_info & GetTypeInfo() const = 0;
162  virtual TfType const& GetType() const = 0;
163  virtual const void* _GetMostDerivedPtr() const = 0;
164  virtual bool _IsPolymorphic() const = 0;
165  };
166 
167  struct _EmptyHolder : _PointerHolderBase {
168  TF_API virtual ~_EmptyHolder();
169  TF_API virtual void Clone(_Data *target) const;
170  TF_API virtual bool IsInvalid() const;
171  TF_API virtual void const * GetUniqueIdentifier() const;
172  TF_API virtual TfWeakBase const *GetWeakBase() const;
173  TF_API virtual operator bool() const;
174  TF_API virtual bool _IsConst() const;
175  TF_API virtual TfPyObjWrapper GetPythonObject() const;
176  TF_API virtual const std::type_info & GetTypeInfo() const;
177  TF_API virtual TfType const& GetType() const;
178  TF_API virtual const void* _GetMostDerivedPtr() const;
179  TF_API virtual bool _IsPolymorphic() const;
180  };
181 
182  template <typename Ptr>
183  struct _PointerHolder : _PointerHolderBase {
184  _PointerHolder(Ptr const &ptr) : _ptr(ptr) {
185  }
186 
187  virtual ~_PointerHolder();
188  virtual void Clone(_Data *target) const;
189  virtual bool IsInvalid() const;
190  virtual void const *GetUniqueIdentifier() const;
191  virtual TfWeakBase const *GetWeakBase() const;
192  virtual operator bool() const;
193  virtual bool _IsConst() const;
194  virtual TfPyObjWrapper GetPythonObject() const;
195  virtual const std::type_info & GetTypeInfo() const;
196  virtual TfType const& GetType() const;
197  virtual const void* _GetMostDerivedPtr() const;
198  virtual bool _IsPolymorphic() const;
199  private:
200  Ptr _ptr;
201  };
202 
203  _PointerHolderBase* _Get() const {
204  return (_PointerHolderBase*)(&_ptrStorage);
205  }
206 
207  _Data _ptrStorage;
208 };
209 
210 // TfHash support. We don't want to choose the TfAnyWeakPtr overload unless the
211 // passed argument is exactly TfAnyWeakPtr. By making this a function template
212 // that's only enabled for TfAnyWeakPtr, C++ will not perform implicit
213 // conversions since T is deduced.
214 template <class HashState,
215  class T, class = typename std::enable_if<
216  std::is_same<T, TfAnyWeakPtr>::value>::type>
217 inline void
218 TfHashAppend(HashState &h, const T& ptr)
219 {
220  h.Append(ptr.GetUniqueIdentifier());
221 }
222 
223 template <class Ptr>
224 TfAnyWeakPtr::_PointerHolder<Ptr>::~_PointerHolder() {}
225 
226 template <class Ptr>
227 void
228 TfAnyWeakPtr::_PointerHolder<Ptr>::Clone(_Data *target) const
229 {
230  new (target) _PointerHolder<Ptr>(_ptr);
231 }
232 
233 template <class Ptr>
234 bool
235 TfAnyWeakPtr::_PointerHolder<Ptr>::IsInvalid() const
236 {
237  return _ptr.IsInvalid();
238 }
239 
240 template <class Ptr>
241 void const *
242 TfAnyWeakPtr::_PointerHolder<Ptr>::GetUniqueIdentifier() const
243 {
244  return _ptr.GetUniqueIdentifier();
245 }
246 
247 template <class Ptr>
248 TfWeakBase const *
249 TfAnyWeakPtr::_PointerHolder<Ptr>::GetWeakBase() const
250 {
251  return &(_ptr->__GetTfWeakBase__());
252 }
253 
254 template <class Ptr>
255 TfAnyWeakPtr::_PointerHolder<Ptr>::operator bool() const
256 {
257  return bool(_ptr);
258 }
259 
260 template <class Ptr>
262 TfAnyWeakPtr::_PointerHolder<Ptr>::GetPythonObject() const
263 {
264 #ifdef PXR_PYTHON_SUPPORT_ENABLED
265  return TfPyObject(_ptr);
266 #else
267  return {};
268 #endif // PXR_PYTHON_SUPPORT_ENABLED
269 }
270 template <class Ptr>
271 const std::type_info &
272 TfAnyWeakPtr::_PointerHolder<Ptr>::GetTypeInfo() const
273 {
274  return TfTypeid(_ptr);
275 }
276 
277 template <class Ptr>
278 TfType const&
279 TfAnyWeakPtr::_PointerHolder<Ptr>::GetType() const
280 {
281  return TfType::Find(_ptr);
282 }
283 
284 template <class Ptr>
285 const void *
286 TfAnyWeakPtr::_PointerHolder<Ptr>::_GetMostDerivedPtr() const
287 {
288  if (!_ptr) {
289  return 0;
290  }
291 
292  typename Ptr::DataType const *rawPtr = get_pointer(_ptr);
293  return TfCastToMostDerivedType(rawPtr);
294 }
295 
296 template <class Ptr>
297 bool
298 TfAnyWeakPtr::_PointerHolder<Ptr>::_IsPolymorphic() const
299 {
300  return std::is_polymorphic<typename Ptr::DataType>::value;
301 }
302 
303 template <class Ptr>
304 bool
305 TfAnyWeakPtr::_PointerHolder<Ptr>::_IsConst() const
306 {
307  return std::is_const<typename Ptr::DataType>::value;
308 }
309 
310 PXR_NAMESPACE_CLOSE_SCOPE
311 
312 #endif
TF_API bool operator==(const TfAnyWeakPtr &rhs) const
equality operator
TF_API bool IsInvalid() const
Return true only if this expiry checker is watching a weak pointer which has expired.
TF_API TfWeakBase const * GetWeakBase() const
Return the TfWeakBase object of the WeakPtr we are holding.
boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Return a python object for the given C++ object, loading the appropriate wrapper code if necessary.
Definition: pyUtils.h:144
Miscellaneous Utilities for dealing with script.
static TfType const & Find()
Retrieve the TfType corresponding to type T.
Definition: type.h:153
Pointer storage with deletion detection.
Provides the ability to hold an arbitrary TfWeakPtr in a non-type-specific manner in order to observe...
Definition: anyWeakPtr.h:54
TfAnyWeakPtr(Ptr const &ptr)
Construct an AnyWeakPtr watching ptr.
Definition: anyWeakPtr.h:67
TF_API bool operator !() const
operator !
TfAnyWeakPtr(TfNullPtrType)
Construct and implicitly convert from TfNullPtr.
Definition: anyWeakPtr.h:81
TF_API const std::type_info & GetTypeInfo() const
returns the type_info of the underlying WeakPtr
std::enable_if< std::is_polymorphic< T >::value, Tf_CopyCV< T, void > * >::type TfCastToMostDerivedType(T *ptr)
Return a pointer to the most-derived object.
Definition: cxxCast.h:67
C++ Cast Utilities.
Boost Python object wrapper.
Definition: pyObjWrapper.h:91
TF_API bool operator<(const TfAnyWeakPtr &rhs) const
comparison operator
TfAnyWeakPtr()
Construct an AnyWeakPtr not watching any ptr.
Definition: anyWeakPtr.h:74
TF_API TfType const & GetType() const
Returns the TfType of the underlying WeakPtr.
TfAnyWeakPtr(std::nullptr_t)
Construct and implicitly convert from std::nullptr_t.
Definition: anyWeakPtr.h:84
TfType represents a dynamic runtime type.
Definition: type.h:64
TF_API void const * GetUniqueIdentifier() const
Return the unique identifier of the WeakPtr this AnyWeakPtr contains.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141
size_t GetHash() const
Return a hash value for this instance.
Definition: anyWeakPtr.h:131