-rw-r--r-- | noncore/multimedia/opieplayer2/singleton.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/noncore/multimedia/opieplayer2/singleton.h b/noncore/multimedia/opieplayer2/singleton.h index 83e228f..825344e 100644 --- a/noncore/multimedia/opieplayer2/singleton.h +++ b/noncore/multimedia/opieplayer2/singleton.h @@ -7,62 +7,71 @@ version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef SINGLETON_H #define SINGLETON_H +#include "threadutil.h" + template <class Product> struct DefaultSingletonCreator { static Product *create() { return new Product; } }; template <class Product> struct NullSingletonCreator { static Product *create() { return 0; } }; template < class T, template <class> class Creator = DefaultSingletonCreator > class Singleton { public: static T &self() { + if ( !s_self ) { + ThreadUtil::AutoLock lock( s_guard ); if ( !s_self ) s_self = Creator<T>::create(); + } return *s_self; } protected: Singleton() { s_self = static_cast<T *>( this ); } ~Singleton() { s_self = 0; } private: Singleton( const Singleton<T, Creator> &rhs ); Singleton<T, Creator> &operator=( const Singleton<T, Creator> &rhs ); static T *s_self; + static ThreadUtil::Mutex s_guard; }; template <class T, template <class> class Creator> T *Singleton<T, Creator>::s_self = 0; +template <class T, template <class> class Creator> +ThreadUtil::Mutex Singleton<T, Creator>::s_guard; + #endif // SINGLETON_H /* vim: et sw=4 ts=4 */ |