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>
63class TfDelegatedCountPtr {
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>;
99 TfDelegatedCountPtr() noexcept = default;
105 RawPtrType rawPointer) noexcept :
106 _pointer{rawPointer} {
113 RawPtrType rawPointer)
114 noexcept(IncrementIsNoExcept()) :
115 _pointer{rawPointer} {
121 TfDelegatedCountPtr(
const TfDelegatedCountPtr& ptr)
122 noexcept(IncrementIsNoExcept()) :
123 _pointer{ptr.get()} {
130 template <
typename OtherType>
131 explicit TfDelegatedCountPtr(
132 const TfDelegatedCountPtr<OtherType>& ptr,
133 std::enable_if_t<_IsPtrConvertible<OtherType>::value,
int> = 0)
134 noexcept(IncrementIsNoExcept()) :
135 _pointer(ptr.get()) {
142 TfDelegatedCountPtr(TfDelegatedCountPtr&& ptr) noexcept :
143 _pointer(ptr.get()) {
144 ptr._pointer =
nullptr;
151 TfDelegatedCountPtr& operator=(
const TfDelegatedCountPtr& ptr)
152 noexcept(IncrementAndDecrementAreNoExcept()) {
154 return (*
this = TfDelegatedCountPtr{ptr});
160 template <
typename OtherType>
161 TfDelegatedCountPtr& operator=(
162 const TfDelegatedCountPtr<OtherType>& ptr)
163 noexcept(IncrementAndDecrementAreNoExcept()) {
164 static_assert(_IsPtrConvertible<OtherType>::value);
166 return (*
this = TfDelegatedCountPtr{ptr});
172 TfDelegatedCountPtr& operator=(TfDelegatedCountPtr&& ptr)
173 noexcept(DecrementIsNoExcept()) {
175 _pointer = ptr.get();
176 ptr._pointer =
nullptr;
181 TfDelegatedCountPtr& operator=(std::nullptr_t)
182 noexcept(DecrementIsNoExcept()) {
191 ~TfDelegatedCountPtr() noexcept(DecrementIsNoExcept::value) {
196 ReferenceType operator*() const noexcept(DereferenceIsNoExcept()) {
201 RawPtrType operator->() const noexcept {
206 explicit operator bool() const noexcept {
return get(); }
209 template <
typename OtherType>
211 const TfDelegatedCountPtr<OtherType>& other)
const noexcept {
212 return get() == other.get();
216 template <
typename OtherType>
218 const TfDelegatedCountPtr<OtherType>& other)
const noexcept {
219 return get() != other.get();
223 template <
typename OtherType>
225 const TfDelegatedCountPtr<OtherType>& other)
const noexcept {
226 return get() < other.get();
230 RawPtrType get() const noexcept {
return _pointer; }
234 void reset() noexcept(DecrementIsNoExcept()) {
240 void swap(TfDelegatedCountPtr& other)
noexcept {
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>
264TfDelegatedCountPtr<ValueType>
265TfMakeDelegatedCountPtr(Args&&... args) {
266 return TfDelegatedCountPtr<ValueType>(
267 TfDelegatedCountIncrementTag,
268 new ValueType(std::forward<Args>(args)...)
272PXR_NAMESPACE_CLOSE_SCOPE
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.