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"
26 #include "debugging/debugging.h"
29 #define RADIANT_DLLEXPORT __stdcall
31 #define RADIANT_DLLEXPORT
38 virtual void capture() = 0;
39 virtual void release() = 0;
40 virtual void* getTable() = 0;
43 inline void* Module_getTable(Module& module)
45 return module.getTable();
48 class TextOutputStream;
49 class DebugMessageHandler;
57 virtual void visit(const char* name, Module& module) const = 0;
60 virtual void setError(bool error) = 0;
61 virtual bool getError() const = 0;
63 virtual TextOutputStream& getOutputStream() = 0;
64 virtual TextOutputStream& getErrorStream() = 0;
65 virtual DebugMessageHandler& getDebugMessageHandler() = 0;
67 virtual void registerModule(const char* type, int version, const char* name, Module& module) = 0;
68 virtual Module* findModule(const char* type, int version, const char* name) const = 0;
69 virtual void foreachModule(const char* type, int version, const Visitor& visitor) = 0;
72 class ModuleServerHolder
74 ModuleServer* m_server;
80 void set(ModuleServer& server)
90 typedef Static<ModuleServerHolder> GlobalModuleServer;
92 inline ModuleServer& globalModuleServer()
94 return GlobalModuleServer::instance().get();
98 inline void initialiseModule(ModuleServer& server)
100 GlobalErrorStream::instance().setOutputStream(server.getErrorStream());
101 GlobalOutputStream::instance().setOutputStream(server.getOutputStream());
102 GlobalDebugMessageHandler::instance().setHandler(server.getDebugMessageHandler());
103 GlobalModuleServer::instance().set(server);
108 template<typename Type>
115 virtual void visit(const char* name, const Type& table) const = 0;
118 virtual Type* findModule(const char* name) = 0;
119 virtual void foreachModule(const Visitor& visitor) = 0;
122 #include "debugging/debugging.h"
124 template<typename Type>
130 ModuleRef(const char* name) : m_table(0)
132 if(!globalModuleServer().getError())
134 m_module = globalModuleServer().findModule(typename Type::Name(), typename Type::Version(), name);
137 globalModuleServer().setError(true);
138 globalErrorStream() << "ModuleRef::initialise: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " name=" << makeQuoted(name) << " - not found\n";
143 if(!globalModuleServer().getError())
145 m_table = static_cast<Type*>(m_module->getTable());
160 ASSERT_MESSAGE(m_table != 0, "ModuleRef::getTable: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " - module-reference used without being initialised");
166 template<typename Type>
167 class SingletonModuleRef
174 : m_module(0), m_table(0)
178 bool initialised() const
180 return m_module != 0;
183 void initialise(const char* name)
185 m_module = globalModuleServer().findModule(typename Type::Name(), typename Type::Version(), name);
188 globalModuleServer().setError(true);
189 globalErrorStream() << "SingletonModuleRef::initialise: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " name=" << makeQuoted(name) << " - not found\n";
196 ASSERT_MESSAGE(m_table != 0, "SingletonModuleRef::getTable: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " - module-reference used without being initialised");
205 m_table = static_cast<Type*>(m_module->getTable());
217 template<typename Type>
220 static SingletonModuleRef<Type> m_instance;
222 static SingletonModuleRef<Type>& instance()
226 static Type& getTable()
228 return *m_instance.getTable();
233 SingletonModuleRef<Type> GlobalModule<Type>::m_instance;
236 template<typename Type>
237 class GlobalModuleRef
240 GlobalModuleRef(const char* name = "*")
242 if(!globalModuleServer().getError())
244 GlobalModule<Type>::instance().initialise(name);
246 GlobalModule<Type>::instance().capture();
250 GlobalModule<Type>::instance().release();
254 return GlobalModule<Type>::getTable();