Files CCore/inc/SingleHost.h CCore/src/SingleHost.cpp
SingleHost is working as a global variable, it points to some object, required as a global resource. SingleHost object must be a global object. It may have an attached object of the type T. To attach and detach this object classes SingleMaster and SingleMaster_delete are used. To access this object the class SingleHook is used.
template <class T>
class SingleHost : NoCopy
{
....
public:
explicit SingleHost(TextLabel name);
~SingleHost();
};
The name is used to name the internal mutex.
SingleHook is used to "hook" an object from a SingleHost. Been hooked, the object is counted as used. The object life-time is extended until it gets out of use.
template <class T>
class SingleHook : NoCopy
{
....
public:
//
// assign/move/swap are not provided to prevent cyclic dependencies
//
// constructors
explicit SingleHook(SingleHost<T> &host);
~SingleHook();
// object ptr
T * operator + () const;
bool operator ! () const;
T * getPtr() const;
T & operator * () const;
T * operator -> () const;
// copy objects
SingleHook(const SingleHook &hook);
};
Constructor does hook, destructor releases the object. If the host does not have an attached object, the hook is null.
The object is accessed through the Object Pointer Interface.
Copy constructor can be used to duplicate the hook. The resulting hook points to the same object.
SingleMaster controls a SingleHost object. In constructor it attaches the object to the host. In destructor it detaches the object. Destructor is waiting until the object gets out of use. The object must be constructed before the constructor call and be destructed after the SingleMaster destructor is finished.
template <class T>
class SingleMaster : NoCopy
{
....
public:
SingleMaster(SingleHost<T> &host,TextLabel name,T &obj);
~SingleMaster();
void guard();
};
Constructor tries to attach the given object to the host. The name is used to name the internal AntiSem. This AntiSem is used to control the life-time of the object. If the host has already an attached object, the constructor and then destructor does nothing. You may use the method guard() to throw an exception, if this happens.
SingleMaster_delete is working similar to the SingleMaster. The difference is: the object is provided using a pointer, not a reference. And the destructor deletes the object. So the object must be created using the operator new.
template <class T>
class SingleMaster_delete : NoCopy
{
....
public:
SingleMaster_delete(SingleHost<T> &host,TextLabel name,T *obj);
~SingleMaster_delete();
void guard();
};