OregonCore  revision 3611e8a-git
Your Favourite TBC server
Singleton.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef OREGON_SINGLETON_H
19 #define OREGON_SINGLETON_H
20 
21 #include "CreationPolicy.h"
22 #include "ThreadingModel.h"
23 #include "ObjectLifeTime.h"
24 
25 namespace Oregon
26 {
27 template
28 <
29  typename T,
30  class ThreadingModel = Oregon::SingleThreaded<T>,
31  class CreatePolicy = Oregon::OperatorNew<T>,
32  class LifeTimePolicy = Oregon::ObjectLifeTime<T>
33  >
34 class Singleton
35 {
36  public:
37  static T& Instance();
38 
39  protected:
40  Singleton() {};
41 
42  private:
43 
44  // Prohibited actions...this does not prevent hijacking.
45  Singleton(const Singleton&);
46  Singleton& operator=(const Singleton&);
47 
48  // Singleton Helpers
49  static void DestroySingleton();
50 
51  // data structure
52  typedef typename ThreadingModel::Lock Guard;
53  static T* si_instance;
54  static bool si_destroyed;
55 };
56 
57 template<typename T, class ThreadingModel, class CreatePolicy, class LifeTimePolicy>
59 
60 template<typename T, class ThreadingModel, class CreatePolicy, class LifeTimePolicy>
62 
63 template
64 <
65  typename T,
66  class ThreadingModel,
67  class CreatePolicy,
68  class LifeTimePolicy
69  >
70 T&
72 {
73  if (!si_instance)
74  {
75  // double-checked Locking pattern
76  Guard();
77  if (!si_instance)
78  {
79  if (si_destroyed)
80  {
81  si_destroyed = false;
82  LifeTimePolicy::OnDeadReference();
83  }
84  si_instance = CreatePolicy::Create();
85  LifeTimePolicy::ScheduleCall(&DestroySingleton);
86  }
87  }
88 
89  return *si_instance;
90 }
91 
92 template
93 <
94  typename T,
95  class ThreadingModel,
96  class CreatePolicy,
97  class LifeTimePolicy
98  >
99 void
101 {
102  CreatePolicy::Destroy(si_instance);
103  si_instance = NULL;
104  si_destroyed = true;
105 }
106 
107 #define INSTANTIATE_SINGLETON_1(TYPE) \
108  namespace Oregon \
109  { \
110  template class Singleton<TYPE, Oregon::SingleThreaded<TYPE>, Oregon::OperatorNew<TYPE>, Oregon::ObjectLifeTime<TYPE> >; \
111  }
112 
113 #define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \
114  namespace Oregon \
115  { \
116  template class Singleton<TYPE, THREADINGMODEL, Oregon::OperatorNew<TYPE>, Oregon::ObjectLifeTime<TYPE> >; \
117  }
118 
119 #define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \
120  namespace Oregon \
121  { \
122  template class Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Oregon::ObjectLifeTime<TYPE> >; \
123  }
124 
125 #define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \
126  namespace Oregon \
127  { \
128  template class Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >; \
129  }
130 
131 }
132 #endif
133 
static T & Instance()
Definition: Singleton.h:71
static void DestroySingleton()
Definition: Singleton.h:100
ThreadingModel::Lock Guard
Definition: Singleton.h:52
static T * si_instance
Definition: Singleton.h:53
Singleton & operator=(const Singleton &)
static bool si_destroyed
Definition: Singleton.h:54