7#ifndef PXR_BASE_TF_WEAK_PTR_H
8#define PXR_BASE_TF_WEAK_PTR_H
16#include "pxr/base/tf/nullPtr.h"
20#include "pxr/base/tf/weakPtrFacade.h"
25PXR_NAMESPACE_OPEN_SCOPE
132 template <
class U>
friend class TfWeakPtr;
134 template <
class U>
struct Rebind {
151 : _rawPtr(p._rawPtr), _remnant(std::move(p._remnant)) {
159 typename std::enable_if<
160 std::is_convertible<U*, T*>::value
161 >::type *dummy = 0) : _rawPtr(get_pointer(p))
164 if (ARCH_LIKELY(_rawPtr))
165 _remnant = Tf_WeakBaseAccess::
166 GetRemnant(_rawPtr->__GetTfWeakBase__());
172 std::is_convertible<U*, T*>::value>::type *dummy =
nullptr) : _rawPtr(p)
175 if (ARCH_LIKELY(_rawPtr))
176 _remnant = Tf_WeakBaseAccess::
177 GetRemnant(_rawPtr->__GetTfWeakBase__());
182 typename std::enable_if<
183 std::is_convertible<U*, T*>::value
184 >::type *dummy = 0) : _rawPtr(p._rawPtr), _remnant(p._remnant)
194 _remnant = std::move(p._remnant);
199 bool IsExpired()
const {
200 return this->IsInvalid();
205 T *_FetchPointer()
const {
206 if (ARCH_LIKELY(_remnant && _remnant->_IsAlive()))
211 bool _IsInvalid()
const {
212 return _remnant && !_remnant->_IsAlive();
215 void const *_GetUniqueIdentifier()
const {
216 return _remnant ? _remnant->_GetUniqueIdentifier() : 0;
219 void _EnableExtraNotification()
const {
220 _remnant->EnableNotification();
261 typedef typename TfRefPtr<T>::_Counter Counter;
262 if (T *rawPtr = get_pointer(p)) {
264 if (Counter::AddRefIfNonzero(rawPtr)) {
268 return TfCreateRefPtr(rawPtr);
290 : _remnant(wp._remnant) {
293 template <
template <
class>
class PtrTemplate,
class Type>
294 TfWeakPtr(TfWeakPtrFacade<PtrTemplate, Type>
const& wpf)
295 : _remnant(_GetRemnant(wpf)) {
301 _remnant = wp._remnant;
305 template <
template <
class>
class PtrTemplate,
class Type>
307 operator= (TfWeakPtrFacade<PtrTemplate, Type>
const& wpf) {
308 _remnant = _GetRemnant(wpf);
314 return wp._remnant == _remnant;
317 template <
template <
class>
class PtrTemplate,
class Type>
318 bool operator== (TfWeakPtrFacade<PtrTemplate, Type>
const& wpf)
const {
319 return _GetRemnant(wpf) == _remnant;
324 return wp._remnant != _remnant;
327 template <
template <
class>
class PtrTemplate,
class Type>
328 bool operator!= (TfWeakPtrFacade<PtrTemplate, Type>
const& wpf)
const {
329 return _GetRemnant(wpf) != _remnant;
334 return wp._remnant < _remnant;
337 template <
template <
class>
class PtrTemplate,
class Type>
338 bool operator< (TfWeakPtrFacade<PtrTemplate, Type>
const& wpf) {
339 return _GetRemnant(wpf) < _remnant;
344 operator UnspecifiedBoolType()
const {
345 return (_remnant && _remnant->_IsAlive())
346 ? &TfWeakPtr::_remnant :
nullptr;
349 bool operator !()
const {
353 bool IsExpired()
const {
354 return _remnant && !_remnant->_IsAlive();
358 template <
template <
class>
class PtrTemplate,
class Type>
360 _GetRemnant(TfWeakPtrFacade<PtrTemplate, Type>
const& wpf) {
361 TfWeakBase const *weakBase = wpf.GetWeakBase();
362 if (ARCH_LIKELY(weakBase)) {
363 return Tf_WeakBaseAccess::GetRemnant(*weakBase);
389struct Tf_HasGetWeakBase
396 using _SignatureOf__GetTfWeakBase__ =
TfWeakBase const & (U::*)()
const;
399 static std::true_type
400 _HasSig(_SignatureOf__GetTfWeakBase__<U>);
403 static decltype(_HasSig(&U::__GetTfWeakBase__))
406 static std::false_type
410 using type =
decltype(_Deduce(
static_cast<T*
>(
nullptr)));
411 using value_type = bool;
412 static const bool value = type::value;
416struct Tf_SupportsWeakPtr
418 static const bool value =
419 std::is_base_of<TfWeakBase, T>::value || Tf_HasGetWeakBase<T>::value;
422#define TF_SUPPORTS_WEAKPTR(T) (Tf_SupportsWeakPtr<T>::value)
423#define TF_TRULY_SUPPORTS_WEAKPTR(T) std::is_base_of<TfWeakBase, T >::value
425#define TF_DECLARE_WEAK_POINTABLE_INTERFACE \
426 virtual TfWeakBase const &__GetTfWeakBase__() const = 0
428#define TF_IMPLEMENT_WEAK_POINTABLE_INTERFACE \
429 virtual TfWeakBase const &__GetTfWeakBase__() const { \
433PXR_NAMESPACE_CLOSE_SCOPE
A user-extensible hashing mechanism for use with runtime hash tables.
Reference-counted smart pointer utility class.
Enable a concrete base class for use with TfWeakPtr.
This access class is befriended by TfWeakPtrFacade -derived classes to grant TfWeakPtrFacade access t...
Pointer storage with deletion detection.
TfWeakPtr(std::nullptr_t)
Construction, implicit conversion from nullptr.
TfWeakPtr(TfRefPtr< U > const &p, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *dummy=0)
Conversion from RefPtr where U* is convertible to T* (this pointer type).
TfWeakPtr(TfWeakPtr &&p) noexcept
Move construction.
TfWeakPtr & operator=(TfWeakPtr const &p)=default
Copy assignment.
TfWeakPtr & operator=(TfWeakPtr &&p) noexcept
Move assignment.
TfWeakPtr(TfWeakPtr const &p)=default
Copy construction.
TfWeakPtr(TfNullPtrType)
Construction, implicit conversion from TfNullPtr.
TfWeakPtr(U *p, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *dummy=nullptr)
Explicitly construct from a raw pointer p.
#define TF_UNUSED(x)
Stops compiler from producing unused argument or variable warnings.
A file containing basic constants and definitions.
TfRefPtr< T > TfCreateRefPtrFromProtectedWeakPtr(TfWeakPtr< T > const &p)
Thread-safe creation of a Tf ref pointer from a Tf weak pointer.