1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 NOTE: The following extension would be intelligent:
20 ---------------------------------------------------
21 - add 'preInit', 'preServiceUpdate', 'preTickUpdate' and 'preRelease' methods
22 - add 'postInit', 'postServiceUpdate', 'postTickUpdate' and 'postRelease' methods
23 - call all 'preXXX' methods followed by all XXX methods followed by all postXXX methods
24 => This allows one to open log files etc in pre-init, start counters in pre-update, stop counters in post-update, etc...
28 #ifndef SINGLETON_REGISTRY_H
29 #define SINGLETON_REGISTRY_H
32 //-------------------------------------------------------------------------------------------------
34 //-------------------------------------------------------------------------------------------------
38 //-------------------------------------------------------------------------------------------------
39 // class IServiceSingleton
40 //-------------------------------------------------------------------------------------------------
42 class IServiceSingleton
45 // overloadable method called at service initialisation
46 virtual void init() {}
48 // overloadable method called in the service update
49 virtual void serviceUpdate() {}
51 // overloadable method called in the tick update
52 virtual void tickUpdate() {}
54 // overloadable method called at service release
55 virtual void release() {}
58 // protect from untrolled instantiation
59 // this method registers the singleton with the singleton registry
61 virtual ~IServiceSingleton() {}
65 IServiceSingleton(const IServiceSingleton
&);
69 //-------------------------------------------------------------------------------------------------
70 // class CSingletonRegistry
71 //-------------------------------------------------------------------------------------------------
73 class CSingletonRegistry
76 // public interface for getting hold of the singleton instance
77 static CSingletonRegistry
* getInstance();
79 // registration of an IServiceSingleton object with the singleton
80 void registerSingleton(IServiceSingleton
*);
82 // methods called from the service loop
89 // prohibit uncontrolled instantiation
90 CSingletonRegistry() {}
91 CSingletonRegistry(const CSingletonRegistry
&);
93 typedef std::set
<IServiceSingleton
*> TSingletons
;
94 TSingletons _Singletons
;
98 //-------------------------------------------------------------------------------------------------
99 // inlines IServiceSingleton
100 //-------------------------------------------------------------------------------------------------
102 inline IServiceSingleton::IServiceSingleton()
104 CSingletonRegistry::getInstance()->registerSingleton(this);
108 //-------------------------------------------------------------------------------------------------
109 // inlines CSingletonRegistry
110 //-------------------------------------------------------------------------------------------------
112 inline CSingletonRegistry
* CSingletonRegistry::getInstance()
114 static CSingletonRegistry
* instance
= NULL
;
117 instance
=new CSingletonRegistry
;
122 inline void CSingletonRegistry::registerSingleton(IServiceSingleton
* singleton
)
124 _Singletons
.insert(singleton
);
127 inline void CSingletonRegistry::init()
129 for (TSingletons::iterator it
=_Singletons
.begin(); it
!=_Singletons
.end();++it
)
133 inline void CSingletonRegistry::tickUpdate()
135 for (TSingletons::iterator it
=_Singletons
.begin(); it
!=_Singletons
.end();++it
)
139 inline void CSingletonRegistry::serviceUpdate()
141 for (TSingletons::iterator it
=_Singletons
.begin(); it
!=_Singletons
.end();++it
)
142 (*it
)->serviceUpdate();
145 inline void CSingletonRegistry::release()
147 for (TSingletons::iterator it
=_Singletons
.begin(); it
!=_Singletons
.end();++it
)
152 //-------------------------------------------------------------------------------------------------