This repository has been archived on 2019-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
dungeon-gunner/source/util/Singleton.h

42 lines
810 B
C
Raw Normal View History

/*
* Singleton.h
*
* Created on: 04.07.2012
* Author: Felix
*/
#ifndef DG_SINGLETON_H_
#define DG_SINGLETON_H_
#include <SFML/System.hpp>
/**
* Template class for inheriting singleton behaviour.
*
2012-09-16 18:45:12 +00:00
* This uses lazy initialization and should be thread safe in C++11 (untested).
* To use, just make a subclass with only a private default constructor and Singleton<T>
* as friend class.
2012-09-16 18:45:12 +00:00
*
* @code
* class MyClass : public Singleton<MyClass> {
* private:
* friend class Singleton<MyClass>;
* MyClass() = default;
* };
* @endcode
*/
template <class T>
class Singleton : public sf::NonCopyable {
2012-09-16 18:45:12 +00:00
// Public functions.
public:
/**
2012-09-16 18:45:12 +00:00
* Returns the instance of T.
*/
static T& i() {
static T s;
return s;
2012-09-16 18:45:12 +00:00
};
};
#endif /* DG_SINGLETON_H_ */