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_H)
23 #define INCLUDED_MODULESYSTEM_H
25 #include "generic/static.h"
28 #define RADIANT_DLLEXPORT __stdcall
30 #define RADIANT_DLLEXPORT
37 virtual void capture() = 0;
38 virtual void release() = 0;
39 virtual void* getTable() = 0;
42 inline void* Module_getTable(Module& module)
44 return module.getTable();
47 class TextOutputStream;
48 class DebugMessageHandler;
56 virtual void visit(const char* name, Module& module) = 0;
59 virtual void setError(bool error) = 0;
60 virtual bool getError() const = 0;
62 virtual TextOutputStream& getOutputStream() = 0;
63 virtual TextOutputStream& getErrorStream() = 0;
64 virtual DebugMessageHandler& getDebugMessageHandler() = 0;
66 virtual void registerModule(const char* type, int version, const char* name, Module& module) = 0;
67 virtual Module* findModule(const char* type, int version, const char* name) const = 0;
68 virtual void foreachModule(const char* type, int version, Visitor& visitor) = 0;
71 class ModuleServerHolder
73 ModuleServer* m_server;
79 void set(ModuleServer& server)
89 typedef Static<ModuleServerHolder> GlobalModuleServer;
91 inline ModuleServer& globalModuleServer()
93 return GlobalModuleServer::instance().get();
97 template<typename Type>
104 virtual void visit(const char* name, const Type& table) = 0;
107 virtual Type* findModule(const char* name) = 0;
108 virtual void foreachModule(Visitor& visitor) = 0;
111 #include "debugging/debugging.h"
113 template<typename Type>
119 ModuleRef(const char* name) : m_table(0)
121 if(!globalModuleServer().getError())
123 m_module = globalModuleServer().findModule(typename Type::Name(), typename Type::Version(), name);
126 globalModuleServer().setError(true);
127 globalErrorStream() << "ModuleRef::initialise: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " name=" << makeQuoted(name) << " - not found\n";
132 if(!globalModuleServer().getError())
134 m_table = static_cast<Type*>(m_module->getTable());
149 ASSERT_MESSAGE(m_table != 0, "ModuleRef::getTable: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " - module-reference used without being initialised");
155 template<typename Type>
156 class SingletonModuleRef
163 : m_module(0), m_table(0)
167 bool initialised() const
169 return m_module != 0;
172 void initialise(const char* name)
174 m_module = globalModuleServer().findModule(typename Type::Name(), typename Type::Version(), name);
177 globalModuleServer().setError(true);
178 globalErrorStream() << "SingletonModuleRef::initialise: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " name=" << makeQuoted(name) << " - not found\n";
185 ASSERT_MESSAGE(m_table != 0, "SingletonModuleRef::getTable: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " - module-reference used without being initialised");
194 m_table = static_cast<Type*>(m_module->getTable());
206 template<typename Type>
209 static SingletonModuleRef<Type> m_instance;
211 static SingletonModuleRef<Type>& instance()
215 static Type& getTable()
217 return *m_instance.getTable();
222 SingletonModuleRef<Type> GlobalModule<Type>::m_instance;
225 template<typename Type>
226 class GlobalModuleRef
229 GlobalModuleRef(const char* name = "*")
231 if(!globalModuleServer().getError())
233 GlobalModule<Type>::instance().initialise(name);
235 GlobalModule<Type>::instance().capture();
239 GlobalModule<Type>::instance().release();
243 return GlobalModule<Type>::getTable();