2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_MODULESYSTEM_SINGLETONMODULE_H)
23 #define INCLUDED_MODULESYSTEM_SINGLETONMODULE_H
25 #include "modulesystem.h"
27 #include "debugging/debugging.h"
28 #include "modulesystem/moduleregistry.h"
29 #include "generic/reference.h"
31 template<typename API, typename Dependencies>
32 class DefaultAPIConstructor
37 return typename API::Name();
40 API* constructAPI(Dependencies& dependencies)
44 void destroyAPI(API* api)
50 template<typename API, typename Dependencies>
51 class DependenciesAPIConstructor
56 return typename API::Name();
59 API* constructAPI(Dependencies& dependencies)
61 return new API(dependencies);
63 void destroyAPI(API* api)
69 class NullDependencies
74 template<typename API, typename Dependencies = NullDependencies, typename APIConstructor = DefaultAPIConstructor<API, Dependencies> >
75 class SingletonModule : public APIConstructor, public Module, public ModuleRegisterable
77 Dependencies* m_dependencies;
79 std::size_t m_refcount;
80 bool m_dependencyCheck;
83 typedef typename API::Type Type;
86 : m_dependencies(0), m_api(0), m_refcount(0), m_dependencyCheck(false), m_cycleCheck(false)
89 explicit SingletonModule(const APIConstructor& constructor)
90 : APIConstructor(constructor), m_dependencies(0), m_api(0), m_refcount(0), m_dependencyCheck(false), m_cycleCheck(false)
95 ASSERT_MESSAGE(m_refcount == 0, "module still referenced at shutdown");
100 globalModuleServer().registerModule(typename Type::Name(), typename Type::Version(), APIConstructor::getName(), *this);
103 Dependencies& getDependencies()
105 return *m_dependencies;
111 return m_api->getTable();
117 if(++m_refcount == 1)
119 globalOutputStream() << "Module Initialising: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
120 m_dependencies = new Dependencies();
121 m_dependencyCheck = !globalModuleServer().getError();
122 if(m_dependencyCheck)
124 m_api = APIConstructor::constructAPI(*m_dependencies);
125 globalOutputStream() << "Module Ready: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
129 globalOutputStream() << "Module Dependencies Failed: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
134 ASSERT_MESSAGE(m_cycleCheck, "cyclic dependency detected");
138 if(--m_refcount == 0)
140 if(m_dependencyCheck)
142 APIConstructor::destroyAPI(m_api);
144 delete m_dependencies;