Last active
December 10, 2015 17:58
-
-
Save asukharev/4471146 to your computer and use it in GitHub Desktop.
c++ singleton pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Singleton.h | |
// | |
// Created by Alexander Sukharev on 05.01.13. | |
// Distributed under the MIT software license, see the accompanying | |
// file COPYING or http://www.opensource.org/licenses/mit-license.php | |
// | |
#ifndef Singleton_h | |
#define Singleton_h | |
template<class Class> | |
class Singleton { | |
public: | |
Singleton(void) { | |
if (instance_) | |
throw; // or what have you... | |
instance_ = (Class*) this; | |
} | |
static inline Class& instance(void) { | |
return *instance_; | |
} | |
static inline bool exists(void) { | |
return instance_ != 0; | |
} | |
virtual ~Singleton(void) { | |
instance_ = 0; | |
} | |
protected: | |
static Class* instance_; | |
}; | |
template<typename Class> | |
Class* Singleton<Class>::instance_ = 0; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment