]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - include/modulesystem.h
Centralise compile checks
[xonotic/netradiant.git] / include / modulesystem.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #if !defined( INCLUDED_MODULESYSTEM_H )
23 #define INCLUDED_MODULESYSTEM_H
24
25 #include "globaldefs.h"
26 #include "generic/static.h"
27 #include "debugging/debugging.h"
28
29 #if GDEF_OS_WINDOWS
30 #define RADIANT_DLLEXPORT __declspec(dllexport)
31 #define RADIANT_DLLIMPORT __declspec(dllimport)
32 #else
33 #define RADIANT_DLLEXPORT __attribute__((visibility("default")))
34 #define RADIANT_DLLIMPORT
35 #endif
36
37
38 class Module
39 {
40 public:
41 virtual void capture() = 0;
42 virtual void release() = 0;
43 virtual void* getTable() = 0;
44 };
45
46 inline void* Module_getTable( Module& module ){
47         return module.getTable();
48 }
49
50 class TextOutputStream;
51 class DebugMessageHandler;
52
53 class ModuleServer
54 {
55 public:
56 class Visitor
57 {
58 public:
59 virtual void visit( const char* name, Module& module ) const = 0;
60 };
61
62 virtual void setError( bool error ) = 0;
63 virtual bool getError() const = 0;
64
65 virtual TextOutputStream& getOutputStream() = 0;
66 virtual TextOutputStream& getErrorStream() = 0;
67 virtual DebugMessageHandler& getDebugMessageHandler() = 0;
68
69 virtual void registerModule( const char* type, int version, const char* name, Module& module ) = 0;
70 virtual Module* findModule( const char* type, int version, const char* name ) const = 0;
71 virtual void foreachModule( const char* type, int version, const Visitor& visitor ) = 0;
72 };
73
74 class ModuleServerHolder
75 {
76 ModuleServer* m_server;
77 public:
78 ModuleServerHolder()
79         : m_server( 0 ){
80 }
81 void set( ModuleServer& server ){
82         m_server = &server;
83 }
84 ModuleServer& get(){
85         return *m_server;
86 }
87 };
88
89 typedef Static<ModuleServerHolder> GlobalModuleServer;
90
91 inline ModuleServer& globalModuleServer(){
92         return GlobalModuleServer::instance().get();
93 }
94
95
96 inline void initialiseModule( ModuleServer& server ){
97         GlobalErrorStream::instance().setOutputStream( server.getErrorStream() );
98         GlobalOutputStream::instance().setOutputStream( server.getOutputStream() );
99         GlobalDebugMessageHandler::instance().setHandler( server.getDebugMessageHandler() );
100         GlobalModuleServer::instance().set( server );
101 }
102
103
104
105 template<typename Type>
106 class Modules
107 {
108 public:
109 class Visitor
110 {
111 public:
112 virtual void visit( const char* name, const Type& table ) const = 0;
113 };
114
115 virtual Type* findModule( const char* name ) = 0;
116 virtual void foreachModule( const Visitor& visitor ) = 0;
117 };
118
119 #include "debugging/debugging.h"
120
121 template<typename Type>
122 class ModuleRef
123 {
124 Module* m_module;
125 Type* m_table;
126 public:
127 ModuleRef( const char* name ) : m_table( 0 ){
128         if ( !globalModuleServer().getError() ) {
129                 m_module = globalModuleServer().findModule( typename Type::Name(), typename Type::Version(), name );
130                 if ( m_module == 0 ) {
131                         globalModuleServer().setError( true );
132                         globalErrorStream() << "ModuleRef::initialise: type=" << makeQuoted( typename Type::Name() ) << " version=" << makeQuoted( typename Type::Version() ) << " name=" << makeQuoted( name ) << " - not found\n";
133                 }
134                 else
135                 {
136                         m_module->capture();
137                         if ( !globalModuleServer().getError() ) {
138                                 m_table = static_cast<Type*>( m_module->getTable() );
139                         }
140                 }
141         }
142 }
143 ~ModuleRef(){
144         if ( m_module != 0 ) {
145                 m_module->release();
146         }
147 }
148 Type* getTable(){
149 #if GDEF_DEBUG
150         ASSERT_MESSAGE( m_table != 0, "ModuleRef::getTable: type=" << makeQuoted( typename Type::Name() ) << " version=" << makeQuoted( typename Type::Version() ) << " - module-reference used without being initialised" );
151 #endif
152         return m_table;
153 }
154 };
155
156 template<typename Type>
157 class SingletonModuleRef
158 {
159 Module* m_module;
160 Type* m_table;
161 public:
162
163 SingletonModuleRef()
164         : m_module( 0 ), m_table( 0 ){
165 }
166
167 bool initialised() const {
168         return m_module != 0;
169 }
170
171 void initialise( const char* name ){
172         m_module = globalModuleServer().findModule( typename Type::Name(), typename Type::Version(), name );
173         if ( m_module == 0 ) {
174                 globalModuleServer().setError( true );
175                 globalErrorStream() << "SingletonModuleRef::initialise: type=" << makeQuoted( typename Type::Name() ) << " version=" << makeQuoted( typename Type::Version() ) << " name=" << makeQuoted( name ) << " - not found\n";
176         }
177 }
178
179 Type* getTable(){
180 #if GDEF_DEBUG
181         ASSERT_MESSAGE( m_table != 0, "SingletonModuleRef::getTable: type=" << makeQuoted( typename Type::Name() ) << " version=" << makeQuoted( typename Type::Version() ) << " - module-reference used without being initialised" );
182 #endif
183         return m_table;
184 }
185 void capture(){
186         if ( initialised() ) {
187                 m_module->capture();
188                 m_table = static_cast<Type*>( m_module->getTable() );
189         }
190 }
191 void release(){
192         if ( initialised() ) {
193                 m_module->release();
194         }
195 }
196 };
197
198 template<typename Type>
199 class GlobalModule
200 {
201 static SingletonModuleRef<Type> m_instance;
202 public:
203 static SingletonModuleRef<Type>& instance(){
204         return m_instance;
205 }
206 static Type& getTable(){
207         return *m_instance.getTable();
208 }
209 };
210
211 template<class Type>
212 SingletonModuleRef<Type> GlobalModule<Type>::m_instance;
213
214
215 template<typename Type>
216 class GlobalModuleRef
217 {
218 public:
219 GlobalModuleRef( const char* name = "*" ){
220         if ( !globalModuleServer().getError() ) {
221                 GlobalModule<Type>::instance().initialise( name );
222         }
223         GlobalModule<Type>::instance().capture();
224 }
225 ~GlobalModuleRef(){
226         GlobalModule<Type>::instance().release();
227 }
228 Type& getTable(){
229         return GlobalModule<Type>::getTable();
230 }
231 };
232
233 #endif