Miam-Player  0.8.0
A nice music player
singleton.h
Go to the documentation of this file.
1 /******************************************************************************
2  singleton.h: singleton template
3  Copyright (C) 2012-2015 Wang Bin <wbsecg1@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ******************************************************************************/
19 
20 
21 #ifndef SINGLETON_H
22 #define SINGLETON_H
23 
24 #include <cstdio>
25 #include <cstdlib> //harmattan: atexit
26 #include <cassert>
27 #define USE_EXCEPTION 0
28 #if USE_EXCEPTION
29 #include <stdexcept> // std::string breaks abi
30 #endif
31 #define DEBUG 1
32 #if DEBUG
33 #define DBG(fmt, ...) \
34  fprintf(stderr, fmt, ##__VA_ARGS__); \
35  fflush(0);
36 #else
37 #define DBG(...)
38 #endif //DEBUG
39 
40 #define DISABLE_COPY(Class) \
41  Class(const Class &); \
42  Class &operator=(const Class &);
43 
44 /*
45  * Used in library, can not be used both in library and outside. so we don't need export it
46  */
47 
48 template <typename T>
49 class Singleton
50 {
52 public:
53  typedef T ObjectType;
54  static T& Instance();
55 protected:
56  Singleton() {}
57  virtual ~Singleton() {}
58 private:
59  static void MakeInstance();
60  static void DestroySingleton();
61 
62  static T* pInstance_;
63  static bool destroyed_;
64 };
65 
66 /*if it is used as dll, template will instanced in dll and exe
67  *, and pInstance_ are 0 for both*/
68 //TODO: use static Singleton<T> inst; return inst;
69 template<typename T>
70 T* Singleton<T>::pInstance_ = 0; //Why it will be initialized twice? The order?
71 template<typename T>
72 bool Singleton<T>::destroyed_ = false;
73 
74 template<typename T>
76 {
77  //DBG("instance = %p\n", pInstance_);
78  if (!pInstance_) {
79  MakeInstance();
80  }
81  return *pInstance_;
82 }
83 
84 
85 template<typename T>
87 {
88  if (!pInstance_) {
89  if (destroyed_) {
90  destroyed_ = false;
91 #if USE_EXCEPTION
92  throw std::logic_error("Dead Reference Detected");
93 #else
94  DBG("Dead Reference Detected");
95  exit(1);
96 #endif //QT_NO_EXCEPTIONS
97  }
98  pInstance_ = new T();
99  DBG("Singleton %p created...\n", pInstance_);
100  std::atexit(&DestroySingleton);
101  }
102 }
103 
104 template<typename T>
106 {
107  DBG("DestroySingleton...\n");
108  assert(!destroyed_);
109  delete pInstance_;
110  pInstance_ = 0;
111  destroyed_ = true;
112 }
113 
114 #endif // SINGLETON_H
#define DBG(fmt,...)
Definition: singleton.h:33
Definition: singleton.h:49
T ObjectType
Definition: singleton.h:53
Singleton()
Definition: singleton.h:56
virtual ~Singleton()
Definition: singleton.h:57
#define DISABLE_COPY(Class)
Definition: singleton.h:40
static T & Instance()
Definition: singleton.h:75