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"
30 #define RADIANT_DLLEXPORT __declspec(dllexport)
31 #define RADIANT_DLLIMPORT __declspec(dllimport)
33 #define RADIANT_DLLEXPORT __stdcall
34 #define RADIANT_DLLIMPORT __stdcall
37 #define RADIANT_DLLEXPORT
38 #define RADIANT_DLLIMPORT
45 virtual void capture() = 0;
46 virtual void release() = 0;
47 virtual void* getTable() = 0;
50 inline void* Module_getTable(Module& module)
52 return module.getTable();
55 class TextOutputStream;
56 class DebugMessageHandler;
64 virtual void visit(const char* name, Module& module) const = 0;
67 virtual void setError(bool error) = 0;
68 virtual bool getError() const = 0;
70 virtual TextOutputStream& getOutputStream() = 0;
71 virtual TextOutputStream& getErrorStream() = 0;
72 virtual DebugMessageHandler& getDebugMessageHandler() = 0;
74 virtual void registerModule(const char* type, int version, const char* name, Module& module) = 0;
75 virtual Module* findModule(const char* type, int version, const char* name) const = 0;
76 virtual void foreachModule(const char* type, int version, const Visitor& visitor) = 0;
79 class ModuleServerHolder
81 ModuleServer* m_server;
87 void set(ModuleServer& server)
97 typedef Static<ModuleServerHolder> GlobalModuleServer;
99 inline ModuleServer& globalModuleServer()
101 return GlobalModuleServer::instance().get();
105 inline void initialiseModule(ModuleServer& server)
107 GlobalErrorStream::instance().setOutputStream(server.getErrorStream());
108 GlobalOutputStream::instance().setOutputStream(server.getOutputStream());
109 GlobalDebugMessageHandler::instance().setHandler(server.getDebugMessageHandler());
110 GlobalModuleServer::instance().set(server);
115 template<typename Type>
122 virtual void visit(const char* name, const Type& table) const = 0;
125 virtual Type* findModule(const char* name) = 0;
126 virtual void foreachModule(const Visitor& visitor) = 0;
129 #include "debugging/debugging.h"
131 template<typename Type>
137 ModuleRef(const char* name) : m_table(0)
139 if(!globalModuleServer().getError())
141 m_module = globalModuleServer().findModule(typename Type::Name(), typename Type::Version(), name);
144 globalModuleServer().setError(true);
145 globalErrorStream() << "ModuleRef::initialise: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " name=" << makeQuoted(name) << " - not found\n";
150 if(!globalModuleServer().getError())
152 m_table = static_cast<Type*>(m_module->getTable());
167 ASSERT_MESSAGE(m_table != 0, "ModuleRef::getTable: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " - module-reference used without being initialised");
173 template<typename Type>
174 class SingletonModuleRef
181 : m_module(0), m_table(0)
185 bool initialised() const
187 return m_module != 0;
190 void initialise(const char* name)
192 m_module = globalModuleServer().findModule(typename Type::Name(), typename Type::Version(), name);
195 globalModuleServer().setError(true);
196 globalErrorStream() << "SingletonModuleRef::initialise: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " name=" << makeQuoted(name) << " - not found\n";
203 ASSERT_MESSAGE(m_table != 0, "SingletonModuleRef::getTable: type=" << makeQuoted(typename Type::Name()) << " version=" << makeQuoted(typename Type::Version()) << " - module-reference used without being initialised");
212 m_table = static_cast<Type*>(m_module->getTable());
224 template<typename Type>
227 static SingletonModuleRef<Type> m_instance;
229 static SingletonModuleRef<Type>& instance()
233 static Type& getTable()
235 return *m_instance.getTable();
240 SingletonModuleRef<Type> GlobalModule<Type>::m_instance;
243 template<typename Type>
244 class GlobalModuleRef
247 GlobalModuleRef(const char* name = "*")
249 if(!globalModuleServer().getError())
251 GlobalModule<Type>::instance().initialise(name);
253 GlobalModule<Type>::instance().capture();
257 GlobalModule<Type>::instance().release();
261 return GlobalModule<Type>::getTable();