Files CCore/inc/InstanceLock.h CCore/src/InstanceLock.cpp
This class can be used to prevent a creation of multiple objects of the given class. The class is thread-safe.
template <class T,ulen Count=1>
class InstanceLock : NoCopy
{
ulen index;
public:
explicit InstanceLock(StrLen name,ulen index=0); // index<Count
~InstanceLock();
ulen getInstanceIndex() const { return index; }
};
The class constructor will throw an exception if an object with the same index has already being created. So up to the Count objects of this type can exist simultaneously each with a unique instance index. The argument name is used in the exception message. It should name the class.
getInstanceIndex() returns the instance index.
Here is a typical usage example:
class SomeClass : InstanceLock<SomeClass> { .... };
This will prevent the creation of multiple objects of the class SomeClass.