IAP GITLAB

Skip to content
Snippets Groups Projects
Commit c4a5458d authored by Maximilian Reininghaus's avatar Maximilian Reininghaus :vulcan:
Browse files

modernized singleton

parent d9dfbd27
No related branches found
No related tags found
2 merge requests!91Resolve "define further classes of processes (MaintenanceProcess?)",!76Resolve "Handling of boundary crossings in geometry tree"
Pipeline #338 passed
......@@ -15,9 +15,6 @@
//#define OFFLINE_USE_GAMMA_SINGLETON
namespace corsika::utl {
#ifndef OFFLINE_USE_GAMMA_SINGLETON
/**
* \class Singleton Singleton.h utl/Singleton.h
*
......@@ -49,101 +46,18 @@ namespace corsika::utl {
class Singleton {
public:
static T& GetInstance()
#ifdef __MAKECINT__
;
#else
{
static T instance;
return instance;
}
#endif
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
protected:
// derived class can call ctor and dtor
Singleton() {}
~Singleton() {}
private:
// no one should do copies
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
};
#else
/// classical Gamma singleton
template <typename T>
class Singleton {
public:
static T& GetInstance() {
if (!fgInstance) fgInstance = new T;
return *fgInstance;
}
protected:
// derived class can call ctor and dtor
Singleton() {}
~Singleton() {}
private:
// no one should do copies
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
static T* fgInstance = 0;
};
#endif
/**
* \class LeakingSingleton Singleton.h utl/Singleton.h
*
* \brief CRTP for leaking singleton
*
* This type of creation (Gamma singleton) leaks the object at
* the end of the run, i.e. class T destructor does not get called
* in at_exit().
*
* This singleton can be implemented as follows
* \code
* #include <utl/Singleton.h>
*
* class SomeClass : public utl::LeakingSingleton<SomeClass> {
* ...
* private:
* // prevent creation, destruction
* SomeClass() { }
* ~SomeClass() { }
*
* friend class utl::LeakingSingleton<SomeClass>;
* };
* \endcode
* LeakingSingleton automatically prevents copying of the derived
* class.
*
* \author Darko Veberic
* \date 9 Aug 2006
* \version $Id: Singleton.h 25091 2014-01-30 09:49:57Z darko $
* \ingroup stl
*/
template <class T>
class LeakingSingleton {
public:
static T& GetInstance() {
static T* const instance = new T;
return *instance;
}
protected:
// derived class can call ctor and dtor
LeakingSingleton() {}
~LeakingSingleton() {}
private:
// no one should do copies
LeakingSingleton(const LeakingSingleton&);
LeakingSingleton& operator=(const LeakingSingleton&);
};
} // namespace corsika::utl
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment