1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef TRINITY_SINGLETONIMPL_H |
---|
22 | #define TRINITY_SINGLETONIMPL_H |
---|
23 | |
---|
24 | #include "Singleton.h" |
---|
25 | |
---|
26 | // avoid the using namespace here cuz |
---|
27 | // its a .h file afterall |
---|
28 | |
---|
29 | template |
---|
30 | < |
---|
31 | typename T, |
---|
32 | class ThreadingModel, |
---|
33 | class CreatePolicy, |
---|
34 | class LifeTimePolicy |
---|
35 | > |
---|
36 | T& |
---|
37 | Trinity::Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy >::Instance() |
---|
38 | { |
---|
39 | if( !si_instance ) |
---|
40 | { |
---|
41 | // double-checked Locking pattern |
---|
42 | Guard(); |
---|
43 | if( !si_instance ) |
---|
44 | { |
---|
45 | if( si_destroyed ) |
---|
46 | { |
---|
47 | si_destroyed = false; |
---|
48 | LifeTimePolicy::OnDeadReference(); |
---|
49 | } |
---|
50 | si_instance = CreatePolicy::Create(); |
---|
51 | LifeTimePolicy::ScheduleCall(&DestroySingleton); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | return *si_instance; |
---|
56 | } |
---|
57 | |
---|
58 | template |
---|
59 | < |
---|
60 | typename T, |
---|
61 | class ThreadingModel, |
---|
62 | class CreatePolicy, |
---|
63 | class LifeTimePolicy |
---|
64 | > |
---|
65 | void |
---|
66 | Trinity::Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::DestroySingleton() |
---|
67 | { |
---|
68 | CreatePolicy::Destroy(si_instance); |
---|
69 | si_instance = NULL; |
---|
70 | si_destroyed = true; |
---|
71 | } |
---|
72 | |
---|
73 | #define INSTANTIATE_SINGLETON_1(TYPE) \ |
---|
74 | template class TRINITY_DLL_DECL Trinity::Singleton<TYPE, Trinity::SingleThreaded<TYPE>, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >; \ |
---|
75 | template<> TYPE* Trinity::Singleton<TYPE, Trinity::SingleThreaded<TYPE>, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_instance = 0; \ |
---|
76 | template<> bool Trinity::Singleton<TYPE, Trinity::SingleThreaded<TYPE>, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_destroyed = false |
---|
77 | |
---|
78 | #define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \ |
---|
79 | template class TRINITY_DLL_DECL Trinity::Singleton<TYPE, THREADINGMODEL, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >; \ |
---|
80 | template<> TYPE* Trinity::Singleton<TYPE, THREADINGMODEL, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_instance = 0; \ |
---|
81 | template<> bool Trinity::Singleton<TYPE, THREADINGMODEL, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_destroyed = false |
---|
82 | |
---|
83 | #define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \ |
---|
84 | template class TRINITY_DLL_DECL Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Trinity::ObjectLifeTime<TYPE> >; \ |
---|
85 | template<> TYPE* Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Trinity::ObjectLifeTime<TYPE> >::si_instance = 0; \ |
---|
86 | template<> bool Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Trinity::ObjectLifeType<TYPE> >::si_destroyed = false |
---|
87 | |
---|
88 | #define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \ |
---|
89 | template class TRINITY_DLL_DECL Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >; \ |
---|
90 | template<> TYPE* Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >::si_instance = 0; \ |
---|
91 | template<> bool Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >::si_destroyed = false |
---|
92 | #endif |
---|