8#ifndef PXR_BASE_TF_DELEGATED_COUNT_PTR_H
9#define PXR_BASE_TF_DELEGATED_COUNT_PTR_H
13#include "pxr/base/tf/api.h"
21PXR_NAMESPACE_OPEN_SCOPE
28 TfDelegatedCountIncrementTag{};
34 TfDelegatedCountDoNotIncrementTag{};
62template <
typename ValueType>
65 using RawPtrType = std::add_pointer_t<ValueType>;
66 using ReferenceType = std::add_lvalue_reference_t<ValueType>;
67 using element_type = ValueType;
72 decltype(TfDelegatedCountIncrement(std::declval<RawPtrType>()))>);
76 decltype(TfDelegatedCountDecrement(std::declval<RawPtrType>()))>);
78 using IncrementIsNoExcept =
79 std::integral_constant<
81 noexcept(TfDelegatedCountIncrement(std::declval<RawPtrType>()))>;
82 using DecrementIsNoExcept =
83 std::integral_constant<
85 noexcept(TfDelegatedCountDecrement(std::declval<RawPtrType>()))>;
86 using IncrementAndDecrementAreNoExcept =
87 std::integral_constant<
88 bool, IncrementIsNoExcept() && DecrementIsNoExcept()>;
89 using DereferenceIsNoExcept =
90 std::integral_constant<bool, noexcept(*std::declval<RawPtrType>())>;
93 template <
typename ConvertibleType>
94 using _IsPtrConvertible = std::is_convertible<
95 std::add_pointer_t<ConvertibleType>, RawPtrType>;
105 RawPtrType rawPointer) noexcept :
106 _pointer{rawPointer} {
113 RawPtrType rawPointer)
114 noexcept(IncrementIsNoExcept()) :
115 _pointer{rawPointer} {
122 noexcept(IncrementIsNoExcept()) :
123 _pointer{ptr.get()} {
130 template <
typename OtherType>
133 std::enable_if_t<_IsPtrConvertible<OtherType>::value,
int> = 0)
134 noexcept(IncrementIsNoExcept()) :
135 _pointer(ptr.
get()) {
143 _pointer(ptr.get()) {
144 ptr._pointer =
nullptr;
152 noexcept(IncrementAndDecrementAreNoExcept()) {
160 template <
typename OtherType>
163 noexcept(IncrementAndDecrementAreNoExcept()) {
164 static_assert(_IsPtrConvertible<OtherType>::value);
173 noexcept(DecrementIsNoExcept()) {
175 _pointer = ptr.get();
176 ptr._pointer =
nullptr;
182 noexcept(DecrementIsNoExcept()) {
196 ReferenceType
operator*() const noexcept(DereferenceIsNoExcept()) {
206 explicit operator bool() const noexcept {
return get(); }
209 template <
typename OtherType>
212 return get() == other.get();
216 template <
typename OtherType>
219 return get() != other.get();
223 template <
typename OtherType>
226 return get() < other.get();
230 RawPtrType
get() const noexcept {
return _pointer; }
234 void reset() noexcept(DecrementIsNoExcept()) {
241 std::swap(other._pointer, _pointer);
245 void _IncrementIfValid() noexcept(IncrementIsNoExcept()) {
247 TfDelegatedCountIncrement(_pointer);
251 void _DecrementIfValid() noexcept(DecrementIsNoExcept()) {
253 TfDelegatedCountDecrement(_pointer);
257 ValueType* _pointer{
nullptr};
263template <
typename ValueType,
typename... Args>
265TfMakeDelegatedCountPtr(Args&&... args) {
267 TfDelegatedCountIncrementTag,
268 new ValueType(std::forward<Args>(args)...)
272PXR_NAMESPACE_CLOSE_SCOPE
Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to...
TfDelegatedCountPtr & operator=(const TfDelegatedCountPtr &ptr) noexcept(IncrementAndDecrementAreNoExcept())
Assign by copying from ptr.
TfDelegatedCountPtr(const TfDelegatedCountPtr< OtherType > &ptr, std::enable_if_t< _IsPtrConvertible< OtherType >::value, int >=0) noexcept(IncrementIsNoExcept())
Copy construct from ptr if it is convertible to this class's RawPtrType.
ReferenceType operator*() const noexcept(DereferenceIsNoExcept())
Dereference the underlying pointer.
TfDelegatedCountPtr(TfDelegatedCountIncrementTagType, RawPtrType rawPointer) noexcept(IncrementIsNoExcept())
Create a new pointer storing rawPointer and call TfDelegatedCountIncrement on it if it is not nullptr...
bool operator==(const TfDelegatedCountPtr< OtherType > &other) const noexcept
Return true if the underlying pointers are equivalent.
bool operator!=(const TfDelegatedCountPtr< OtherType > &other) const noexcept
Returns false if the underlying pointers are equivalent.
RawPtrType operator->() const noexcept
Arrow operator dispatch for the underlying pointer.
TfDelegatedCountPtr(const TfDelegatedCountPtr &ptr) noexcept(IncrementIsNoExcept())
Copy construct from ptr, calling TfDelegatedCountIncrement on the held pointer if it is not nullptr.
TfDelegatedCountPtr & operator=(TfDelegatedCountPtr &&ptr) noexcept(DecrementIsNoExcept())
Assign by moving from ptr.
void swap(TfDelegatedCountPtr &other) noexcept
Swap this object's held pointer with other's.
TfDelegatedCountPtr & operator=(std::nullptr_t) noexcept(DecrementIsNoExcept())
Reset this pointer to its default state (i.e. nullptr)
bool operator<(const TfDelegatedCountPtr< OtherType > &other) const noexcept
Orders based on the underlying pointer.
~TfDelegatedCountPtr() noexcept(DecrementIsNoExcept::value)
Call TfDelegatedCountDecrement on the held pointer if it is not nullptr`.
RawPtrType get() const noexcept
Return the underlying pointer.
TfDelegatedCountPtr() noexcept=default
Create a pointer storing nullptr
void reset() noexcept(DecrementIsNoExcept())
Reset the pointer to its default state (nullptr), calling TfDelegatedCountDecrement if the held point...
TfDelegatedCountPtr & operator=(const TfDelegatedCountPtr< OtherType > &ptr) noexcept(IncrementAndDecrementAreNoExcept())
Assign by copying from ptr if it is convertible to this class's RawPtrType.
TfDelegatedCountPtr(TfDelegatedCountPtr &&ptr) noexcept
Construct by moving from ptr.
Stripped down version of diagnostic.h that doesn't define std::string.
When constructing a TfDelegatedCountPtr from a raw pointer, use the TfDelegatedCountDoNotIncrementTag...
When constructing a TfDelegatedCountPtr from a raw pointer, use the TfDelegatedCountIncrementTag to e...
A file containing basic constants and definitions.