25#ifndef PXR_BASE_TF_DELEGATED_COUNT_PTR_H
26#define PXR_BASE_TF_DELEGATED_COUNT_PTR_H
30#include "pxr/base/tf/api.h"
38PXR_NAMESPACE_OPEN_SCOPE
45 TfDelegatedCountIncrementTag{};
51 TfDelegatedCountDoNotIncrementTag{};
79template <
typename ValueType>
82 using RawPtrType = std::add_pointer_t<ValueType>;
83 using ReferenceType = std::add_lvalue_reference_t<ValueType>;
84 using element_type = ValueType;
89 decltype(TfDelegatedCountIncrement(std::declval<RawPtrType>()))>);
93 decltype(TfDelegatedCountDecrement(std::declval<RawPtrType>()))>);
95 using IncrementIsNoExcept =
96 std::integral_constant<
98 noexcept(TfDelegatedCountIncrement(std::declval<RawPtrType>()))>;
99 using DecrementIsNoExcept =
100 std::integral_constant<
102 noexcept(TfDelegatedCountDecrement(std::declval<RawPtrType>()))>;
103 using IncrementAndDecrementAreNoExcept =
104 std::integral_constant<
105 bool, IncrementIsNoExcept() && DecrementIsNoExcept()>;
106 using DereferenceIsNoExcept =
107 std::integral_constant<bool, noexcept(*std::declval<RawPtrType>())>;
110 template <
typename ConvertibleType>
111 using _IsPtrConvertible = std::is_convertible<
112 std::add_pointer_t<ConvertibleType>, RawPtrType>;
122 RawPtrType rawPointer) noexcept :
123 _pointer{rawPointer} {
130 RawPtrType rawPointer)
131 noexcept(IncrementIsNoExcept()) :
132 _pointer{rawPointer} {
139 noexcept(IncrementIsNoExcept()) :
140 _pointer{ptr.get()} {
147 template <
typename OtherType>
150 std::enable_if_t<_IsPtrConvertible<OtherType>::value,
int> = 0)
151 noexcept(IncrementIsNoExcept()) :
152 _pointer(ptr.
get()) {
160 _pointer(ptr.get()) {
161 ptr._pointer =
nullptr;
169 noexcept(IncrementAndDecrementAreNoExcept()) {
177 template <
typename OtherType>
180 noexcept(IncrementAndDecrementAreNoExcept()) {
181 static_assert(_IsPtrConvertible<OtherType>::value);
190 noexcept(DecrementIsNoExcept()) {
192 _pointer = ptr.get();
193 ptr._pointer =
nullptr;
199 noexcept(DecrementIsNoExcept()) {
213 ReferenceType
operator*() const noexcept(DereferenceIsNoExcept()) {
223 explicit operator bool() const noexcept {
return get(); }
226 template <
typename OtherType>
229 return get() == other.get();
233 template <
typename OtherType>
236 return get() != other.get();
240 template <
typename OtherType>
243 return get() < other.get();
247 RawPtrType
get() const noexcept {
return _pointer; }
251 void reset() noexcept(DecrementIsNoExcept()) {
258 std::swap(other._pointer, _pointer);
262 void _IncrementIfValid() noexcept(IncrementIsNoExcept()) {
264 TfDelegatedCountIncrement(_pointer);
268 void _DecrementIfValid() noexcept(DecrementIsNoExcept()) {
270 TfDelegatedCountDecrement(_pointer);
274 ValueType* _pointer{
nullptr};
280template <
typename ValueType,
typename... Args>
282TfMakeDelegatedCountPtr(Args&&... args) {
284 TfDelegatedCountIncrementTag,
285 new ValueType(std::forward<Args>(args)...)
289PXR_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.