libassa  3.5.1
Singleton.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // Singleton.h
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1997-2002,2005 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 // Created: 02/22/99
13 //------------------------------------------------------------------------------
14 #ifndef _Singleton_h
15 #define _Singleton_h
16 
17 #include "Destroyer.h"
18 
19 namespace ASSA {
20 
41 template <class T>
42 class Singleton
43 {
44 public:
46 
47  static T* get_instance () {
48  if (m_instance == 0) {
49  m_instance = new T;
50  m_destroyer.setGuard (m_instance);
51  }
52  return m_instance;
53  }
54 
55 protected:
57  Singleton() {}
58 
59  friend class Destroyer<T>;
60 
62  virtual ~Singleton () {}
63 
64 private:
66  static T* m_instance;
67 
70 };
71 
72 } // end namespace ASSA
73 
74 
82 #define ASSA_DECL_SINGLETON(K) \
83 template <> K* ASSA::Singleton<K>::m_instance = NULL; \
84 template <class T> ASSA::Destroyer<T> ASSA::Singleton<T>::m_destroyer; \
85 template ASSA::Destroyer<K> ASSA::Singleton<K>::m_destroyer;
86 
87 #endif
Destroyer is the helper class for class Singleton.
static Destroyer< T > m_destroyer
Destroyer that owns object T.
Definition: Singleton.h:69
virtual ~Singleton()
Virtual Destructor.
Definition: Singleton.h:62
Singleton()
Protected Constructor.
Definition: Singleton.h:57
static T * get_instance()
Return an instance of templated class T.
Definition: Singleton.h:47
static T * m_instance
Pointer to the object T instance.
Definition: Singleton.h:66
Definition: Acceptor.h:40