Go to the documentation of this file.
11 #ifndef TLX_COUNTING_PTR_HEADER
12 #define TLX_COUNTING_PTR_HEADER
18 #include <type_traits>
24 class CountingPtrDefaultDeleter
27 template <
typename Type>
37 template <
typename Type>
64 template <
typename Type,
typename Deleter = CountingPtrDefaultDeleter>
77 {
if (o) o->inc_reference(); }
87 template <
typename Other,
typename OtherDeleter>
112 template <
typename Subclass,
113 typename =
typename std::enable_if<
114 std::is_convertible<Subclass*, Type*>::value,
void>::type>
122 { other.ptr_ =
nullptr; }
125 template <
typename Subclass,
126 typename =
typename std::enable_if<
127 std::is_convertible<Subclass*, Type*>::value,
void>::type>
130 { other.ptr_ =
nullptr; }
135 if (
ptr_ == other.ptr_)
145 template <
typename Subclass,
146 typename =
typename std::enable_if<
147 std::is_convertible<Subclass*, Type*>::value,
void>::type>
150 if (
ptr_ == other.ptr_)
160 if (
ptr_ == other.ptr_)
164 other.ptr_ =
nullptr;
169 template <
typename Subclass,
170 typename =
typename std::enable_if<
171 std::is_convertible<Subclass*, Type*>::value,
void>::type>
173 if (
ptr_ == other.ptr_)
177 other.ptr_ =
nullptr;
202 Type *
get() const noexcept {
return ptr_; }
205 bool valid() const noexcept
206 {
return (
ptr_ !=
nullptr); }
209 operator bool () const noexcept
214 {
return (
ptr_ ==
nullptr); }
223 {
return ptr_->reference_count(); }
254 {
return ptr_ == other.ptr_; }
258 {
return ptr_ != other.ptr_; }
262 {
return ptr_ == other; }
266 {
return ptr_ != other; }
270 {
return ptr_ < other.ptr_; }
274 {
return ptr_ <= other.ptr_; }
278 {
return ptr_ > other.ptr_; }
282 {
return ptr_ >= other.ptr_; }
286 {
return ptr_ < other; }
290 {
return ptr_ <= other; }
294 {
return ptr_ > other; }
298 {
return ptr_ >= other; }
304 template <
typename Type>
308 template <
typename Type>
312 template <
typename Type,
typename... Args>
319 template <
typename A,
typename D>
325 template <
typename A,
typename D>
326 std::ostream&
operator << (std::ostream& os,
const CountingPtr<A, D>& c) {
327 return os << c.get();
380 bool unique() const noexcept
438 #endif // !TLX_COUNTING_PTR_HEADER
Type & operator*() const noexcept
return the enclosed object as reference.
Provides reference counting abilities for use with CountingPtr.
void operator()(Type *ptr) const noexcept
~CountingPtr()
destructor: decrements reference count in ptr.
void unify()
make and refer a copy if the original object was shared.
void swap(CountingPtr &b) noexcept
swap enclosed object with another counting pointer (no reference counts need change)
Type * ptr_
the pointer to the currently referenced object.
bool unique() const noexcept
Test if the ReferenceCounter is referenced by only one CountingPtr.
dummy deleter for CountingPtr
ReferenceCounter & operator=(const ReferenceCounter &) noexcept
assignment operator, leaves pointers unchanged
std::ostream & operator<<(std::ostream &os, const CountingPtr< A, D > &c)
print pointer
CountingPtr(const CountingPtr &other) noexcept
copy-constructor: also initializes new reference to ptr.
bool operator==(const CountingPtr &other) const noexcept
test equality of only the pointer values.
bool operator!=(const CountingPtr &other) const noexcept
test inequality of only the pointer values.
bool valid() const noexcept
test for a non-nullptr pointer
bool dec_reference() const noexcept
Call whenever resetting (i.e.
size_t reference_count() const noexcept
Return the number of references to this object (for debugging)
Type element_type
contained type.
bool operator>(const CountingPtr &other) const noexcept
compare the pointer values.
void reset()
release contained pointer, frees object if this is the last reference.
bool unique() const noexcept
if the object is referred by this CountingPtr only
std::atomic< size_t > reference_count_
the reference count is kept mutable for CountingPtr<const Type> to change the reference count.
size_t use_count() const noexcept
Returns the number of different shared_ptr instances managing the current object.
void operator()(Type *) const noexcept
CountingPtr(CountingPtr &&other) noexcept
move-constructor: just moves pointer, does not change reference counts.
ReferenceCounter() noexcept
new objects have zero reference count
CountingPtr() noexcept
default constructor: contains a nullptr pointer.
bool operator<=(const CountingPtr &other) const noexcept
compare the pointer values.
bool operator>=(const CountingPtr &other) const noexcept
compare the pointer values.
Type * operator->() const noexcept
return the enclosed pointer.
bool empty() const noexcept
test for a nullptr pointer
void inc_reference(Type *o) noexcept
increment reference count of object.
void swap(CountingPtr< A, D > &a1, CountingPtr< A, D > &a2) noexcept
swap enclosed object with another counting pointer (no reference counts need change)
CountingPtr< Type > make_counting(Args &&... args)
method analogous to std::make_shared and std::make_unique.
friend class CountingPtr
all CountingPtr are friends such that they may steal pointers.
bool operator<(const CountingPtr &other) const noexcept
compare the pointer values.
CountingPtr & operator=(const CountingPtr &other) noexcept
copy-assignment operator: acquire reference on new one and dereference current object.
void inc_reference() const noexcept
Call whenever setting a pointer to the object.
Type * get() const noexcept
return the enclosed pointer.
void dec_reference() noexcept
decrement reference count of current object and maybe delete it.
High-performance smart pointer used as a wrapping reference counting pointer.