#include <MboRefCntPtr.h>
Public Types | |
typedef _THREAD_MODEL | ThreadModel |
typedef _THREAD_MODEL_PTR | ThreadModelPtr |
typedef _Ty | Type |
typedef RefCntRep< _Ty, ThreadModel > | RepType |
typedef RepType::refcount_type | refcount_type |
typedef NullPointer< _Ty > | NullPtr |
typedef ThreadModel::Lock | ExtLock |
typedef ThreadModel::CriticalSection | CriticalSection |
typedef RefCntConstPtr< Type, ThreadModel, ThreadModelPtr > | ConstPtr |
typedef const RepType *const RefCntPtr::* | unspecified_bool_type |
Public Member Functions | |
RefCntPtr (_Ty *p=0) | |
template<class _Tx> | |
RefCntPtr (const _Tx &x) | |
RefCntPtr (const RefCntPtr &oth) | |
~RefCntPtr () | |
RefCntPtr & | operator= (_Ty *p) |
RefCntPtr & | __assign (const _Ty &ref) |
_Ty & | __clone (_Ty &ref) const |
RefCntPtr & | operator= (const RefCntPtr &oth) |
operator unspecified_bool_type () const | |
refcount_type | __referenceCount () const |
ExtLock | __getLock () const |
RepType * | __getRep () |
const RepType * | __getRep () const |
_Ty & | operator * () |
const _Ty & | operator * () const |
_Ty * | operator-> () |
const _Ty * | operator-> () const |
Protected Member Functions | |
void | __release () |
Protected Attributes | |
RepType * | pRep |
CriticalSection | mx |
Related Functions | |
(Note that these are not member functions.) | |
template<class _Ty, class _THM, class _THM_PTR> | |
bool | operator< (const RefCntPtr< _Ty, _THM, _THM_PTR > &lhs, const RefCntPtr< _Ty, _THM, _THM_PTR > &rhs) |
LT: value base comparison of two reference count pointers. | |
template<class _Ty, class _THM, class _THM_PTR> | |
bool | operator== (const RefCntPtr< _Ty, _THM, _THM_PTR > &lhs, const RefCntPtr< _Ty, _THM, _THM_PTR > &rhs) |
EQ: value base comparison of two reference count pointers. | |
template<class _Ty, class _THM, class _THM_PTR> | |
bool | operator!= (const RefCntPtr< _Ty, _THM, _THM_PTR > &lhs, const RefCntPtr< _Ty, _THM, _THM_PTR > &rhs) |
NE: negated value base comparison of two reference count pointers. |
The class RefCntPtr<_Ty> describes an object that stores a pointer to an allocated instance of type _Ty, and keeps that object's reference count. Each time RefCntPtr<_Ty> instance is copied (through copy constructor or assignment operator), it's reference count is incremented. The stored pointer to object of type _Ty * is passed to constructor of RefCntPtr<_Ty> and must either be null or designate an object allocated by a new expression. The destructor for RefCntPtr<_Ty> deletes the allocated object if reference count falls to zero. Hence, an object of class RefCntPtr<_Ty> ensures that a pointer to an instance of type _Ty is automatically deleted. Valid values for the templates _THREAD_MODEL are SingleThreadedModel (for single threaded model) and MultiThreadedModel (for multi threaded model).
_Ty | type to use for target pointer | |
_THREAD_MODEL | either SingleThreadedModel, MultiThreadedModel or the automatically selected ThreadModel. | |
_THREAD_MODEL_PTR | same as _THREAD_MODEL but for the instance itself. This defaults to SingleThreadedModel regardless of the current ThreadModel default. The only thinkable need for MultiThreadedModel would be a situation where more than one thread accesses the same RefCntPtr. |
{ RefCntPtr<int> p; // equivalent of NULL pointer (no target) RefCntPtr<int> p1(5); // reference count of p1 is 1 (target RefCntPtr<int> p2(7); // reference count of p2 is 1 printf("p1=%d", *p2); // this will print "p1=5". printf("p2=%d", *p2); // this will print "p2=7". p2 = p1; // reference count of p2 falls to zero, deallocation // occurs, and then reference count of p1 will be // incremented to 2 because p2 now uses the same // target. Therefore also reference count of p2 is 2. printf("p2=%d", *p2); // this will print "p2=5". } // deallocation is performed now becouse destructor of p1 decrements // reference count to 1, and then destructor of p2 decrements reference // count to 0.
The is a derived class RefCntConstPtr that stores a const pointer.
The size of this class is > 4 Bytes so use call by reference where approriate. The class itself will handle reference counting in assignments. But you may not store instances passed by refernce by memory operations of any kind since that would ignore reference counting.
The shortcut ConstPtr denotes a const version of this type that does not allow to modify the inner pointer, however it can free the inner pointer.
For debugging purpose under Microsoft Visual Studio you might want to edit the debug helper file 'autoexp.dat' and add the following lines:
This class is distributed under the following license GNU Lesser General Public License, version 2.1.
Definition at line 305 of file MboRefCntPtr.h.
typedef _THREAD_MODEL mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::ThreadModel |
thread model for Rep member access
Definition at line 308 of file MboRefCntPtr.h.
typedef _THREAD_MODEL_PTR mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::ThreadModelPtr |
thread model for accessing this
Definition at line 309 of file MboRefCntPtr.h.
typedef _Ty mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::Type |
type abbrevation for target pointer type
Definition at line 310 of file MboRefCntPtr.h.
typedef RefCntRep<_Ty, ThreadModel> mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::RepType |
type abbrevation for target pointer representation
Definition at line 311 of file MboRefCntPtr.h.
typedef RepType::refcount_type mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::refcount_type |
type used for refcounting
Definition at line 312 of file MboRefCntPtr.h.
typedef NullPointer<_Ty> mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::NullPtr |
type abbrevation for a null pointer that throws nullptr_access upon access
Definition at line 313 of file MboRefCntPtr.h.
typedef ThreadModel::Lock mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::ExtLock |
type abbreviation for thread model dependent Lock
Definition at line 314 of file MboRefCntPtr.h.
typedef ThreadModel::CriticalSection mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::CriticalSection |
type abbreviation for thread model dependent ctitical section
Definition at line 315 of file MboRefCntPtr.h.
typedef RefCntConstPtr<Type, ThreadModel, ThreadModelPtr> mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::ConstPtr |
const pointer shortcut
Definition at line 316 of file MboRefCntPtr.h.
typedef const RepType* const RefCntPtr::* mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::unspecified_bool_type |
Definition at line 493 of file MboRefCntPtr.h.
mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::RefCntPtr | ( | _Ty * | p = 0 |
) | [inline] |
Default constructor
The standard constructor takes a pointer of the member pointer type.
p | target pointer to be stored in the RefCntPtr. |
Definition at line 346 of file MboRefCntPtr.h.
00347 : pRep(p ? RepType::Alloc(p) : 0) 00348 { 00349 }
mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::RefCntPtr | ( | const _Tx & | x | ) | [inline] |
Clone constructor with automatic type conversion
This copy constructor is very handy when two types are involved in the creation of a RefCntPtr instance. The first type is the type of the internally stored pointer which may be an abstract class. The second type may be any derived type. This way you can create a copy from a derived reference and store the resulting pointer with the abstract type in the RefCntPrt.
Class _Tx is the type of param x
which must be equal or derived from class type _Ty
.
x | An instance to clone (by calling its copy constructor) |
Definition at line 366 of file MboRefCntPtr.h.
00367 : pRep(RepType::Alloc(new _Tx(x))) 00368 { 00369 }
mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::RefCntPtr | ( | const RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR > & | oth | ) | [inline] |
Copy constructor that takes a reference
oth | RefCntPtr to copy from |
Definition at line 375 of file MboRefCntPtr.h.
References mbo::RefCntRep< _Ty, _THREAD_MODEL >::AddRef(), and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00376 : pRep(0) 00377 { 00378 FastLock lock(&oth.mx); 00379 00380 pRep = oth.pRep; 00381 if (pRep) 00382 { 00383 pRep->AddRef(); 00384 } 00385 }
mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::~RefCntPtr | ( | ) | [inline] |
destructor
The destructor releases the resources. That means it decrements the reference count to the stored pointer and frees is if the result is zero.
Definition at line 395 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__release(), and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx.
void mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__release | ( | ) | [inline, protected] |
Free/release the internal pointer
Definition at line 329 of file MboRefCntPtr.h.
References mbo::RefCntRep< _Ty, _THREAD_MODEL >::Dealloc(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep, and mbo::RefCntRep< _Ty, _THREAD_MODEL >::Release().
Referenced by mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator=(), and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::~RefCntPtr().
00330 { 00331 if (pRep && pRep->Release()) 00332 { 00333 RepType::Dealloc(pRep); 00334 } 00335 }
RefCntPtr& mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator= | ( | _Ty * | p | ) | [inline] |
Assignment operator
p | new target pointer |
Definition at line 407 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__release(), mbo::RefCntRep< _Ty, _THREAD_MODEL >::Alloc(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx, mbo::RefCntRep< _Ty, _THREAD_MODEL >::pData, and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00408 { 00409 FastLock lock(&mx); 00410 00411 if (!pRep || pRep->pData != p) 00412 { 00413 __release(); 00414 pRep = p ? RepType::Alloc(p) : 0; 00415 } 00416 return *this; 00417 }
RefCntPtr& mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__assign | ( | const _Ty & | ref | ) | [inline] |
Assignment by reference
Unlike other assignment solutions this one is completely captured by the lock and opens/closes the lock only once.
Using *ptr = ref; would require the pointer to be valid before and the assignment happens outside the lock.
Using ptr = ref; would open/lock twice.
ref | reference to target |
Definition at line 432 of file MboRefCntPtr.h.
References mbo::RefCntRep< _Ty, _THREAD_MODEL >::Alloc(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx, mbo::RefCntRep< _Ty, _THREAD_MODEL >::pData, and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00433 { 00434 FastLock lock(&mx); 00435 00436 if (pRep) 00437 { 00438 if (pRep->pData) 00439 { 00440 *pRep->pData = ref; 00441 } 00442 else 00443 { 00444 pRep->pData = new _Ty(ref); 00445 } 00446 } 00447 else 00448 { 00449 pRep = RepType::Alloc(new _Ty(ref)); 00450 } 00451 return *this; 00452 }
_Ty& mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__clone | ( | _Ty & | ref | ) | const [inline] |
Derefence cloning
Unlike other data methods this one is completly captured by the lock. Hence it ensures the data is completley cloned while protected.
ref | Reference to data which will hold the copy (work as a clone) |
Definition at line 462 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx, mbo::RefCntRep< _Ty, _THREAD_MODEL >::pData, and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00463 { 00464 FastLock lock(&mx); 00465 00466 ref = pRep && pRep->pData ? *pRep->pData : *NullPtr(); 00467 00468 return ref; 00469 }
RefCntPtr& mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator= | ( | const RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR > & | oth | ) | [inline] |
Assignment operator
oth | RefCntPtr to copy from |
Definition at line 476 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__release(), mbo::RefCntRep< _Ty, _THREAD_MODEL >::AddRef(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx, and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00477 { 00478 FastLock lock(&mx); 00479 00480 if (pRep != oth.pRep) 00481 { 00482 __release(); 00483 pRep = oth.pRep; 00484 if (pRep) 00485 { 00486 pRep->AddRef(); 00487 } 00488 } 00489 return *this; 00490 }
mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator unspecified_bool_type | ( | ) | const [inline] |
Check if assigned/set
Reimplemented in mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >.
Definition at line 503 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx, mbo::RefCntRep< _Ty, _THREAD_MODEL >::pData, and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00504 { 00505 FastLock lock(&mx); 00506 00507 return (pRep != 0 && pRep->pData != 0) ? &RefCntPtr::pRep : 0; 00508 }
refcount_type mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__referenceCount | ( | ) | const [inline] |
ExtLock mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__getLock | ( | ) | const [inline] |
RepType* mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__getRep | ( | ) | [inline] |
Definition at line 535 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00537 { 00538 return pRep;
const RepType* mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__getRep | ( | ) | const [inline] |
Reimplemented in mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >.
Definition at line 540 of file MboRefCntPtr.h.
References mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep.
00542 { 00543 return pRep;
_Ty& mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator * | ( | ) | [inline] |
const _Ty& mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator * | ( | ) | const [inline] |
Dereference operator (const)
Reimplemented in mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >.
Definition at line 566 of file MboRefCntPtr.h.
00568 { 00569 FastLock lock(&mx); 00570 00571 return pRep && pRep->pData ? *pRep->pData : *NullPtr();
_Ty* mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator-> | ( | ) | [inline] |
const _Ty* mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator-> | ( | ) | const [inline] |
Dereference operator (const)
Reimplemented in mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >.
Definition at line 588 of file MboRefCntPtr.h.
bool operator< | ( | const RefCntPtr< _Ty, _THM, _THM_PTR > & | lhs, | |
const RefCntPtr< _Ty, _THM, _THM_PTR > & | rhs | |||
) | [related] |
LT: value base comparison of two reference count pointers.
lhs | left hand side operand | |
rhs | right hand side operand |
Definition at line 820 of file MboRefCntPtr.h.
bool operator== | ( | const RefCntPtr< _Ty, _THM, _THM_PTR > & | lhs, | |
const RefCntPtr< _Ty, _THM, _THM_PTR > & | rhs | |||
) | [related] |
EQ: value base comparison of two reference count pointers.
lhs | left hand side operand | |
rhs | right hand side operand |
Definition at line 841 of file MboRefCntPtr.h.
bool operator!= | ( | const RefCntPtr< _Ty, _THM, _THM_PTR > & | lhs, | |
const RefCntPtr< _Ty, _THM, _THM_PTR > & | rhs | |||
) | [related] |
NE: negated value base comparison of two reference count pointers.
lhs | left hand side operand | |
rhs | right hand side operand |
Definition at line 862 of file MboRefCntPtr.h.
RepType* mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::pRep [protected] |
connection to RefCntRep that holds the target pointer
Definition at line 321 of file MboRefCntPtr.h.
Referenced by mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__assign(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__clone(), mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__getRep(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__getRep(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__release(), mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator unspecified_bool_type(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator unspecified_bool_type(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator=(), and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::RefCntPtr().
CriticalSection mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::mx [mutable, protected] |
internal access mutex
Definition at line 325 of file MboRefCntPtr.h.
Referenced by mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__assign(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::__clone(), mbo::RefCntConstPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator unspecified_bool_type(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator unspecified_bool_type(), mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::operator=(), and mbo::RefCntPtr< _Ty, _THREAD_MODEL, _THREAD_MODEL_PTR >::~RefCntPtr().
Hosted on code.google.com | © Marcus Börger | Generated on Fri Jan 18 21:21:12 2008 for MBO-lib by ![]() |