|
PSCF v1.4.0
|
Reference to a shared resource. More...
#include <CountedReference.h>
Public Member Functions | |
| CountedReference () | |
| Constructor. | |
| bool | isAssociated () const |
| Is this associated with a ReferenceCounter? | |
| void | associate (ReferenceCounter &counter) |
| Create an association with a ReferenceCounter. | |
| void | dissociate () |
| Destroy an association with a ReferenceCounter. | |
Reference to a shared resource.
A class that makes use of a resource that it does not own, known as a resource user, may have a private CountedReference member variable to keep track of the association with the class that owns the resource, known as the resource owner. In this pattern, the resource owner class should have a private ReferenceCounter member variable that maintains an integer counter of the number of associated resource users. A CountedReference may only be associated with a single ReferenceCounter, but a ReferenceCounter may be associated with any number of instances of CountedReference.
A CountedReference has a private pointer member that can store the address of an associated ReferenceCounter. A ReferenceCounter has an integer member variable that keeps track of the number of associated CountedReference objects. The CountedReference class is a friend of ReferenceCounter, which allows it to increment and decrement the counter maintained by an associated ReferenceCounter whenever an association is created or destroyed.
Each resource user class should provide public functions, typically named associated and dissociate, that can create and destroy an association with a single instance of a related resource owner class. The associate member function of the parent resource user class should retain a handle to the shared resource and also call the associate member function of its CountedReference member variable. The dissociate member function of the parent resource user class should nullify the handle to the resource and call the dissociate function of the CountedReference member variable.
Definition at line 49 of file CountedReference.h.
| Util::CountedReference::CountedReference | ( | ) |
Constructor.
Definition at line 18 of file CountedReference.cpp.
|
inline |
Is this associated with a ReferenceCounter?
Is this reference associated with a counter?
Definition at line 101 of file CountedReference.h.
Referenced by associate(), and dissociate().
| void Util::CountedReference::associate | ( | ReferenceCounter & | counter | ) |
Create an association with a ReferenceCounter.
On entry, isAssociated() must return false. After return, isAssociated() will return true.
This function retains a pointer to the associated ReferenceCounter that is passed as a parameter. It also increments the integer reference counter maintained by that object, which is returned by the function ReferenceCounter::nRef().
| counter | associated ReferenceCounter |
Definition at line 25 of file CountedReference.cpp.
References isAssociated(), and UTIL_CHECK.
Referenced by Pscf::DeviceArray< Data >::addReference(), and Pscf::DeviceMemory::addReference().
| void Util::CountedReference::dissociate | ( | ) |
Destroy an association with a ReferenceCounter.
On entry, isAssociated() must true true. After return, isAssociated() will return false.
This function decrements the integer counter maintained by an associated ReferenceCounter, and then nullifies the pointer to to that object.
Definition at line 35 of file CountedReference.cpp.
References isAssociated(), and UTIL_CHECK.