3 #include "ref_counted.h"
5 #include <cgv/defines/assert.h>
6 #include <cgv/type/cond/is_base_of.h>
7 #include <cgv/type/cond/has_virtual_destructor.h>
10 #include "lib_begin.h"
18 template <
class T,
bool is_ref_counted>
44 template <
typename T,
bool is_ref_counted = false>
48 friend class ref_ptr_impl<const T, is_ref_counted>;
62 if (--counter->count == 0) {
71 T* ref ()
const {
return counter ? counter->ptr : 0; }
73 explicit ref_ptr_impl(T* p = 0) : counter(0) {
75 counter =
new counter_type(p, 1);
78 ref_ptr_impl(
const ref_ptr_impl<T,false>& r) : counter(r.counter)
85 ref_ptr_impl(
const ref_ptr_impl<S,false>& s)
88 CGV_DEFINES_ASSERT(type::cond::is_base_of<T,S>::value);
90 CGV_DEFINES_ASSERT(type::cond::has_virtual_destructor<T>::value);
92 counter =
reinterpret_cast<counter_type*
>(s.counter);
99 int get_count()
const {
return counter ? counter->count : 0; }
102 template <
typename T>
103 class ref_ptr_impl<T,true> :
public ref_ptr_tag
110 if (dec_ref_count(ptr)) {
122 explicit ref_ptr_impl(T* p) : ptr(p) {
127 ref_ptr_impl(
const ref_ptr_impl<T,true>& r) : ptr(r.ref())
133 template <
typename S>
134 ref_ptr_impl(
const ref_ptr_impl<S,true>& s)
137 CGV_DEFINES_ASSERT(type::cond::is_base_of<T,S>::value);
139 CGV_DEFINES_ASSERT(type::cond::has_virtual_destructor<T>::value);
141 ptr =
static_cast<const ref_ptr<S,true>&
>(s).operator->();
149 int get_count()
const {
150 return ptr ? ptr->get_ref_count() : 0;
158 template <class T, bool is_ref_counted = type::cond::is_base_of<ref_counted,T>::value>
159 class ref_ptr :
public ref_ptr_impl<T,is_ref_counted>
176 template <
typename S>
179 template <
typename S>
199 template <
typename S>
215 return this->ref() == r.ref();
219 return this->ref() < r.ref();
223 return this->ref() != r.ref();
227 return this->get_count() <= 1;
231 return this->get_count() == 0;
234 operator bool ()
const {
246 #include <cgv/config/lib_end.h>
bool operator<(const this_type &r) const
compare by pointer
Definition: ref_ptr.h:218
ref_ptr_impl< T, is_ref_counted > base_type
type of base class that implements the reference count specific methods
Definition: ref_ptr.h:165
this_type & operator=(const this_type &r)
assignment to pointer of same type
Definition: ref_ptr.h:191
struct to store the pointer with a count
Definition: ref_ptr.h:51
T & operator*() const
access to element
Definition: ref_ptr.h:206
void clear()
set to null pointer
Definition: ref_ptr.h:238
T * operator->() const
access to element pointer
Definition: ref_ptr.h:210
bool operator==(const this_type &r) const
compare by pointer
Definition: ref_ptr.h:214
ref_ptr< S, is_ref_counted > up_cast() const
use static cast to convert from T to S if T is a base class of S and has a virtual destructor
Definition: ref_ptr.h:180
ref_ptr(T *p=0)
construction from pointer or empty if pointer is not given
Definition: ref_ptr.h:168
ref_ptr(const ref_ptr< S, is_ref_counted > &s)
allow to copy ref_ptr to a ref_ptr<T> if T is a base class of S and if T has a virtual destructor
Definition: ref_ptr.h:177
bool empty() const
check if pointer is not yet set
Definition: ref_ptr.h:230
ref_ptr(const this_type &r)
copy constructor for reference pointers of the same type
Definition: ref_ptr.h:170
struct used to make ref pointers to ref_counted friends of ref_counted
Definition: ref_ptr.h:23
void inc_ref_count(const ref_counted *ptr) const
increment the count of a ref counted object
Definition: ref_ptr.h:26
Definition: ref_counted.h:11
template condition returning, whether the passed type has a virtual destructor
Definition: has_virtual_destructor.h:14
~ref_ptr()
destruct reference counted pointer
Definition: ref_ptr.h:172
int get_ref_count() const
read access to current count
Definition: ref_counted.h:23
ref_ptr< T, is_ref_counted > this_type
type of the reference counted pointer
Definition: ref_ptr.h:163
void set_ref_count(int c) const
write access is also const to allow ref counted pointers to const instances
Definition: ref_counted.h:20
the cgv namespace
Definition: vr_calib.cxx:9
bool operator!=(const this_type &r) const
compare by pointer
Definition: ref_ptr.h:222
bool dec_ref_count(const ref_counted *ptr) const
decrement the count of a ref counted object and return whether to delete the object
Definition: ref_ptr.h:31
bool unique() const
check if this is the only reference
Definition: ref_ptr.h:226
template condition returning, whether the first argument is a base class of the second argument
Definition: is_base_of.h:15