2015-09-29 19:21:02 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/** Пример:
|
2017-03-31 16:00:30 +00:00
|
|
|
|
*
|
2017-04-01 07:20:54 +00:00
|
|
|
|
* class Derived : public Singleton<Derived>
|
|
|
|
|
* {
|
|
|
|
|
* friend class Singleton<Derived>;
|
2015-09-29 19:21:02 +00:00
|
|
|
|
* ...
|
2017-04-01 07:20:54 +00:00
|
|
|
|
* protected:
|
|
|
|
|
* Derived() {};
|
|
|
|
|
* };
|
2015-09-29 19:21:02 +00:00
|
|
|
|
*
|
|
|
|
|
* Или так:
|
|
|
|
|
*
|
|
|
|
|
* class Some
|
|
|
|
|
* {
|
|
|
|
|
* ...
|
|
|
|
|
* };
|
|
|
|
|
*
|
|
|
|
|
* class SomeSingleton : public Some, public Singleton<SomeSingleton> {}
|
|
|
|
|
*/
|
|
|
|
|
template<class Subject> class Singleton
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
static Subject & instance()
|
|
|
|
|
{
|
|
|
|
|
/// Нормально при включенных thread safe statics в gcc (по-умолчанию).
|
|
|
|
|
static Subject instance;
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Singleton(){};
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
|
Singleton(const Singleton&);
|
|
|
|
|
Singleton& operator=(const Singleton&);
|
2015-09-29 19:21:02 +00:00
|
|
|
|
};
|