aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/singleton.h18
1 files changed, 12 insertions, 6 deletions
diff --git a/common/singleton.h b/common/singleton.h
index 89d912f69a..665ef3d72e 100644
--- a/common/singleton.h
+++ b/common/singleton.h
@@ -31,6 +31,12 @@ namespace Common {
template <class T>
class Singleton
{
+private:
+ Singleton<T>(const Singleton<T>&);
+ Singleton<T>& operator= (const Singleton<T>&);
+
+ static T* _singleton;
+
public:
static T& instance() {
// TODO: We aren't thread safe. For now we ignore it since the
@@ -40,18 +46,18 @@ public:
// semi-random. If we use multiple singletons, the destruction
// order might become an issue. There are various approaches
// to solve that problem, but for now this is sufficient
- static T singleton;
- return singleton;
+ if (!_singleton)
+ _singleton = new T;
+ return *_singleton;
}
protected:
Singleton<T>() { }
~Singleton<T>() { }
-
-private:
- Singleton<T>(const Singleton<T>&);
- Singleton<T>& operator= (const Singleton<T>&);
};
+template <class T>
+T* Singleton<T>::_singleton=0;
+
} // End of namespace Common
#endif