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_MODULESMAP_H )
23 #define INCLUDED_MODULESYSTEM_MODULESMAP_H
25 #include "modulesystem.h"
26 #include "string/string.h"
30 template<typename Type>
31 class ModulesMap : public Modules<Type>
33 typedef std::map<CopiedString, Module*> modules_t;
37 for ( modules_t::iterator i = m_modules.begin(); i != m_modules.end(); ++i )
39 ( *i ).second->release();
43 typedef modules_t::const_iterator iterator;
45 iterator begin() const {
46 return m_modules.begin();
48 iterator end() const {
49 return m_modules.end();
52 void insert( const char* name, Module& module ){
54 if ( globalModuleServer().getError() ) {
56 globalModuleServer().setError( false );
60 m_modules.insert( modules_t::value_type( name, &module ) );
64 Type* find( const char* name ){
65 modules_t::iterator i = m_modules.find( name );
66 if ( i != m_modules.end() ) {
67 return static_cast<Type*>( Module_getTable( *( *i ).second ) );
72 Type* findModule( const char* name ){
75 void foreachModule( const typename Modules<Type>::Visitor& visitor ){
76 for ( modules_t::iterator i = m_modules.begin(); i != m_modules.end(); ++i )
78 visitor.visit( ( *i ).first.c_str(), *static_cast<const Type*>( Module_getTable( *( *i ).second ) ) );
83 template<typename Type>
84 class InsertModules : public ModuleServer::Visitor
86 ModulesMap<Type>& m_modules;
88 InsertModules( ModulesMap<Type>& modules )
89 : m_modules( modules ){
91 void visit( const char* name, Module& module ) const {
92 m_modules.insert( name, module );
96 template<typename Type>
99 ModulesMap<Type> m_modules;
101 ModulesRef( const char* names ){
102 std::string type_name { typename Type::Name() };
103 if ( !globalModuleServer().getError() ) {
104 if ( string_equal( names, "*" ) ) {
105 InsertModules<Type> visitor( m_modules );
106 globalModuleServer().foreachModule( typename Type::Name(), typename Type::Version(), visitor );
110 StringTokeniser tokeniser( names );
113 const char* name = tokeniser.getToken();
114 if ( string_empty( name ) ) {
117 Module* module = globalModuleServer().findModule( typename Type::Name(), typename Type::Version(), name );
119 // do not fail on missing image or model plugin, they can be optional
120 if ( type_name.compare("image") == 0 || type_name.compare("model") == 0 ) {
121 globalOutputStream() << "ModulesRef::initialise: type=" << makeQuoted( typename Type::Name() ) << " version=" << makeQuoted( typename Type::Version() ) << " name=" << makeQuoted( name ) << " - not found\n";
124 globalModuleServer().setError( true );
125 globalErrorStream() << "ModulesRef::initialise: type=" << makeQuoted( typename Type::Name() ) << " version=" << makeQuoted( typename Type::Version() ) << " name=" << makeQuoted( name ) << " - not found\n";
131 m_modules.insert( name, *module );
137 ModulesMap<Type>& get(){