1 #ifndef s11n_net_refcount_REFCOUNT_HPP_INCLUDED 2 #define s11n_net_refcount_REFCOUNT_HPP_INCLUDED 1 81 static type
deref( T *t ) {
return *t; }
92 static type
deref( type xx ) {
return xx; }
215 template <
typename HandleT,
231 mutable pointer_type m_ptr;
232 typedef int counter_type;
233 typedef std::map<pointer_type,counter_type> map_type;
239 static map_type & map()
257 static counter_type decrement( pointer_type & ptr )
259 if( ! ptr )
return false;
260 typename map_type::iterator it = map().find(ptr);
261 if( map().end() == it )
return false;
262 if( 0 == (*it).second )
return 0;
263 counter_type rc = --(*it).second;
267 finalizer_type()( ptr );
278 static counter_type increment( pointer_type & ptr )
280 if( ! ptr )
return -1;
281 return ++(map()[ptr]);
299 explicit rcptr( pointer_type h ) : m_ptr(h)
301 this->increment( this->m_ptr );
309 this->increment( this->m_ptr );
321 if( rhs.m_ptr == this->m_ptr )
return *
this;
322 this->decrement( this->m_ptr );
323 this->m_ptr = rhs.m_ptr;
324 this->increment( this->m_ptr );
343 if( this->m_ptr != rhs )
345 pointer_type x = this->m_ptr;
346 this->m_ptr = rhs.m_ptr;
357 this->decrement( this->m_ptr );
363 return this->m_ptr == rhs.m_ptr;
368 return this->m_ptr != rhs.m_ptr;
377 return this->m_ptr < rhs.m_ptr;
385 pointer_type
get()
const {
return this->m_ptr; }
396 if( p == this->m_ptr )
return;
397 this->decrement( this->m_ptr );
398 this->increment( this->m_ptr = p );
415 pointer_type t = this->m_ptr;
416 this->map().erase( this->m_ptr );
467 if( ! this->m_ptr )
return 0;
468 typename map_type::iterator it = map().find(this->m_ptr);
469 return ( map().end() == it ) ? 0 : (*it).second;
475 bool empty()
const {
return 0 == this->m_ptr; }
521 #endif // s11n_net_refcount_REFCOUNT_HPP_INCLUDED The refcount namespace encapsulates code for a reference-counted smart pointer.
rcptr(rcptr const &rhs)
rhs and this object will both manage the same underlying pointer.
bool operator==(rcptr const &rhs) const
Returns (this->m_ptr == rhs.m_ptr).
type * pointer_type
The basic pointer type.
void *& type
Same as (void*&).
bool operator<(rcptr const &rhs) const
Returns this->get() < rhs.get().
void operator()(T *&t)
Assigs t to 0 without deleting t.
rcptr & operator=(rcptr const &rhs)
First disowns any connected pointer (using take(0)), then rhs and this object will both manage the sa...
void operator()(T *&t)
Calls delete t and assigns t to 0.
reference_type operator*() const
The same as *(this->get()).
rcptr()
An empty shared pointer, useful only as a target of assigment or take().
void take(pointer_type p)
Gives ownership of p to this object (or a collection of like-types rcptr objects).
pointer_type take()
Transfers ownership of this->get() to the caller.
The default destructor/cleanup functor for use with rcptr<>.
bool operator!=(rcptr const &rhs) const
Returns (this->m_ptr != rhs.m_ptr).
A no-op "destructor" for use with rcptr.
static type deref(type xx)
Returns xx.
void swap(rcptr &rhs)
Efficiently swaps this object and rhs, such that they swap ownership of their underlying pointers...
Detail::ref_type< type >::type reference_type
reference_type is the same as (T&) unless T is void, in which case it is the same as (void*&) because...
HandleT type
The basic type of object pointed to.
FinalizerT finalizer_type
The type of functor used to clean up pointer_type objects.
size_t ref_count() const
Returns the number of references to this object's pointer, or zero if no pointer is bound...
static type deref(T *t)
Returns *t.
~rcptr()
See decrement();.
Internal detail for dereferencing pointers.
A bare-bones non-intrusive reference-counted pointer type with the ability for the client to specify ...
rcptr(pointer_type h)
Transfers ownership of h, or allows h to participate in ownership with other rcptr objects pointing a...
bool empty() const
Returns the same as (!this->get()).
pointer_type operator->() const
The same as this->get().