Files CCore/inc/gadget/Init.h CCore/src/gadget/Init.cpp
InitExitObject is a class for the manual object initialization/termination. It is a POD type, containing inside the storage room for the object and the object pointer.
template <class T>
struct InitExitObject
{
// private data
InitStorage<sizeof (T)> storage;
T *obj;
You should not touch data members directly.
Method init() creates the object, exit() destroys it. Using these methods you can manually control the life-time of the object and recreate it if necessary. To use InitExitObject the prior value initialization is required. You can do it using the default initialization or using the method clean().
// public void clean() { obj=0; } template <class ... SS> void init(SS && ... ss); void exit();
For example, you can create a global variable:
InitExitObject<C> theObject{};
and then initialize it using the method init().
InitExitObject implements the Object-Pointer Interface to check the state of the object and access it:
T * operator + () const { return obj; } bool operator ! () const { return !obj; } T * getPtr_unsafe() const { return obj; } T * getPtr() const { if( !obj ) NoObjectAbort(); return obj; } T & operator * () const { return *getPtr(); } T * operator -> () const { return getPtr(); } };
InitStorage is a simple structure, providing an aligned storage to create objects inside:
template <ulen Len>
struct InitStorage
{
.... storage;
void * getMem() { return &storage; }
Place<void> getPlace() { return PlaceAt(getMem()); }
const void * getMem() const { return &storage; }
Place<const void> getPlace() const { return PlaceAt(getMem()); }
};
getMem() returns the raw memory pointer to the storage.
getPlace() returns the Place of the storage.
NoObjectAbort() is an Abort function. It is used to abort the execution in case of access to an absent object. You can use it in similar situations. It calls Abort() with a proper diagnostic message.
void NoObjectAbort();