diff options
author | Christian Gmeiner <christian.gmeiner@gmail.com> | 2009-06-02 08:30:13 +0200 |
---|---|---|
committer | Christian Gmeiner <christian.gmeiner@gmail.com> | 2009-06-02 08:30:13 +0200 |
commit | e90ad159371e1f83e02ae10db326c2b2fbffdfbd (patch) | |
tree | e5ce9db35093e0ed29172312d72de3c8c3160fb7 /dxr3singleton.h | |
parent | 9261c474ce41b2e72fdb1341af465849e25cecea (diff) | |
download | vdr-plugin-dxr3-e90ad159371e1f83e02ae10db326c2b2fbffdfbd.tar.gz vdr-plugin-dxr3-e90ad159371e1f83e02ae10db326c2b2fbffdfbd.tar.bz2 |
rework singleton class
Diffstat (limited to 'dxr3singleton.h')
-rw-r--r-- | dxr3singleton.h | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/dxr3singleton.h b/dxr3singleton.h index ebfbe42..b53ac79 100644 --- a/dxr3singleton.h +++ b/dxr3singleton.h @@ -1,7 +1,7 @@ /* * dxr3singleton.h * - * Copyright (C) 2004 Christian Gmeiner + * Copyright (C) 2004-2009 Christian Gmeiner * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -22,30 +22,43 @@ #ifndef _DXR3_SINGLETON_H_ #define _DXR3_SINGLETON_H_ -// ================================== -//! A singleton template. -/*! - Is a nice solution to use only - one instance of a class. -*/ +#include <memory> +#include <vdr/thread.h> + template <class T> -class Singleton { +class Singleton +{ public: - virtual ~Singleton() {} - static T& Instance(); + static T *instance() { + + // use double-checked looking + // see http://en.wikipedia.org/wiki/Double-checked_locking + if (inst.get() == 0) { + m.Lock(); + if (inst.get() == 0) { + inst = std::auto_ptr<T>(new T); + } + m.Unlock(); + } + + return inst.get(); + } + + virtual ~Singleton() { }; protected: - Singleton() {} + Singleton() { } private: - Singleton(const Singleton&); + static std::auto_ptr<T> inst; + static cMutex m; }; -template <class T> -T& Singleton<T>::Instance() { - static T instance; - return instance; -} +template<class T> +std::auto_ptr<T> Singleton<T>::inst(0); + +template<class T> +cMutex Singleton<T>::m; #endif /*_DXR3_SINGLETON_H_*/ |