template <class T> class static_ref_counter : public thread mutex static reference counter.
| | class_type Parent class type |
| | static_ref_counter (T* parent = 0, bool array = false) Make a reference counter |
| | getCount () const Get the reference count |
| | setCount (int count) Set the reference count |
| | getParent () const Get the parent class |
| | setParent (T* parent, bool array = false) Set the parent class |
| | operator++ () increase the reference count (prefix) |
| | operator++ (int) increase the reference count (postfix) |
| | operator-- () decrease the reference count (prefix); deletes parant if reaches zero |
| | operator-- (int) decrease the reference count (postfix); deletes parant if reaches zero |
| | increase () increase (returns true if counter value is positive) |
| | decrease () decrease (returns true if deleted) |
| | operator* () const dereference operator (returns parent) |
| | operator-> () const member access operator (returns parent) |
A templated reference counter. The counter maintains a count value and a pointer to a parent class. The counter value is initialized with zero and can be increased and decreased with the corresponding operators. If the reference counter reaches zero, the parant class will be deleted. A static reference counter can not be copied. A static refernce counter is MT safe in the sense that it protects its refernce counter. However, it will not protect the parent class. Since the static_ref_counter is derived from the mutex object, it can be used to protect the parent class. To avoid deadlock it must not be locked during increasing or decreasing its count value.Example:
// one_resource is a sharable resource which implements // reference counting. class one_resource { static_ref_count<one_resource> fRef; public: one_resource() : fRef (this) {++fRef; } bool refInc() {retrun fRef.Increase(); } bool refDec() {retrun fRef.Decrease(); } void modify() { thread::semlock (fRef); // use it as mutex ... } ... }; // Since one-resource deleted itself when the refercnce count reaches // zero, it has to be created with the new operator. In most cases it // is probably better to use a dynamic refernce counter.
explicit static_ref_counter(T* parent = 0, bool array = false)
int getCount() const
void setCount(int count)
T* getParent() const
void setParent(T* parent, bool array = false)
static_ref_counter& operator++()
static_ref_counter& operator++(int)
static_ref_counter& operator--()
static_ref_counter& operator--(int)
bool increase()
bool decrease()
T& operator*() const
T* operator->() const
alphabetic index hierarchy of classes
Please send questions and comments to zweizig_j@ligo.caltech.edu
generated by doc++