template<typename ref_data, typename CreateDataFunctor = _Default_ModifyData<ref_data>, typename DestroyDataFunctor = _Default_ModifyData<ref_data> > class TRefCounter

A template class that can be used to implement reference counting.

Public Methods

[more] TRefCounter(CreateDataFunctor _create, DestroyDataFunctor _destroy)
Initialize a reference counter that refers to a new data object.
[more]virtual ~TRefCounter()
When a reference counter is destroyed, the data's reference count is decremented and if required, destroyed
[more]this_type& operator = (const this_type& V)
Makes this reference counter into a refer to the data refered to by "V", first removing reference to whatever object it used to refer to
[more]bool operator == (const this_type& V)
Returns true if both TRefCounter's point to the same peice of internal data
[more]bool operator != (const this_type& V)
Returns true if both TRefCounter's don't point to the same piece of internal data
[more] operator ref_data& ()
This operator allows you to perform operations on the TRefCounter as if it were the object that it wrapped
[more]TRefCounter<ref_data&> operator = (const ref_data& V)
Assigning a data object to this object will copy to the internal data wrapped by this reference counter.
[more]void Clear()
Removes reference to current data and creates a new, unique peice of data to refer to
[more]void Unhook()
Like clear but it won't delete the reference data.
[more]ref_data& GetData()
Returns the data object
[more]const ref_data& GetData() const
Returns the data object (const)
[more]int GetRefCount() const
Returns the number of objects refering to this data object
[more]void AddRef()
Add a reference to this data object
[more]bool CanDelete() const
Check if this data object is deletable
[more]void RemoveRef()
Remove a reference to this data object
[more]void SetData(const ref_data& _data)
Assign to the data object referred to by this object.


Documentation

A template class that can be used to implement reference counting.

Example:

	class TestClassData {
	public:
		int type, id;
	};
	
	class TestClass {
	public:
		TestClass(const TestClass& V) { // copy constructor
			ref = V.ref;
		}
		
		int GetType() 	{ return ref.GetData().type; }
		int GetID() 	{ return ref.GetData().id; }
	protected:
		TRefCounter<TestClassData> ref;
	};
In this example TestClass reference counts TestClassData. It is very plausible (and in fact very simple), for a class to referece count multiple data classes. Just use two or more TRefCounter members and copy them in the copy constructor.

Template arguments:

o TRefCounter(CreateDataFunctor _create, DestroyDataFunctor _destroy)
Initialize a reference counter that refers to a new data object. Use these instances of the functor objects for creation and destruction.

ovirtual ~TRefCounter()
When a reference counter is destroyed, the data's reference count is decremented and if required, destroyed

othis_type& operator = (const this_type& V)
Makes this reference counter into a refer to the data refered to by "V", first removing reference to whatever object it used to refer to

obool operator == (const this_type& V)
Returns true if both TRefCounter's point to the same peice of internal data

obool operator != (const this_type& V)
Returns true if both TRefCounter's don't point to the same piece of internal data

o operator ref_data& ()
This operator allows you to perform operations on the TRefCounter as if it were the object that it wrapped

oTRefCounter<ref_data&> operator = (const ref_data& V)
Assigning a data object to this object will copy to the internal data wrapped by this reference counter. What this means is:

RefCounterObject = RefDataObject

will actually assign to the ref data stored in the RefCounterObject.

ovoid Clear()
Removes reference to current data and creates a new, unique peice of data to refer to

ovoid Unhook()
Like clear but it won't delete the reference data. This is a very dangerous function that should only be used when you know exactly what your doing. In most cases, what you really want is Clear. This function can be useful, if the ref data is unique and you want to use it outside of the reference counter. You can then "unhook" it to prevent the reference counter from automatically destroying it.

oref_data& GetData()
Returns the data object

oconst ref_data& GetData() const
Returns the data object (const)

oint GetRefCount() const
Returns the number of objects refering to this data object

ovoid AddRef()
Add a reference to this data object

obool CanDelete() const
Check if this data object is deletable

ovoid RemoveRef()
Remove a reference to this data object

ovoid SetData(const ref_data& _data)
Assign to the data object referred to by this object. This of course affects all reference counters that refer to the same data object as this one.


This class has no child classes.

Alphabetic index HTML hierarchy of classes or Java



This page was generated with the help of DOC++.