modulesmap.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_MODULESYSTEM_MODULESMAP_H)
00023 #define INCLUDED_MODULESYSTEM_MODULESMAP_H
00024 
00025 #include "modulesystem.h"
00026 #include "string/string.h"
00027 #include <map>
00028 #include <set>
00029 #include <iostream>
00030 
00031 template<typename Type>
00032 class ModulesMap: public Modules<Type>
00033 {
00034         typedef std::map<std::string, Module*> modules_t;
00035         modules_t m_modules;
00036     public:
00037         ~ModulesMap ()
00038         {
00039             for (modules_t::iterator i = m_modules.begin(); i != m_modules.end(); ++i) {
00040                 (*i).second->release();
00041             }
00042         }
00043 
00044         typedef modules_t::const_iterator iterator;
00045 
00046         iterator begin () const
00047         {
00048             return m_modules.begin();
00049         }
00050         iterator end () const
00051         {
00052             return m_modules.end();
00053         }
00054 
00055         void insert (const std::string& name, Module& module)
00056         {
00057             module.capture();
00058             if (globalModuleServer().getError()) {
00059                 module.release();
00060                 globalModuleServer().setError(false);
00061             } else {
00062                 m_modules.insert(modules_t::value_type(name, &module));
00063             }
00064         }
00065 
00066         Type* find (const std::string& name)
00067         {
00068             modules_t::iterator i = m_modules.find(name);
00069             if (i != m_modules.end()) {
00070                 return static_cast<Type*> (Module_getTable(*(*i).second));
00071             }
00072             return 0;
00073         }
00074 
00075         Type* findModule (const std::string& name)
00076         {
00077             return find(name);
00078         }
00079         void foreachModule (const typename Modules<Type>::Visitor& visitor)
00080         {
00081             for (modules_t::iterator i = m_modules.begin(); i != m_modules.end(); ++i) {
00082                 visitor.visit((*i).first.c_str(), *static_cast<const Type*> (Module_getTable(*(*i).second)));
00083             }
00084         }
00085 };
00086 
00087 template<typename Type>
00088 class InsertModules: public ModuleServer::Visitor
00089 {
00090         ModulesMap<Type>& m_modules;
00091     public:
00092         InsertModules (ModulesMap<Type>& modules) :
00093             m_modules(modules)
00094         {
00095         }
00096 
00097         void visit (const std::string& name, Module& module) const
00098         {
00099             m_modules.insert(name, module);
00100         }
00101 };
00102 
00103 // The ModulesRef class appears to be a container for a certain subset of Modules specified in
00104 // its constructor
00105 template<typename Type>
00106 class ModulesRef
00107 {
00108         ModulesMap<Type> m_modules;
00109     public:
00110         ModulesRef (const std::string& names)
00111         {
00112             if (!globalModuleServer().getError()) {
00113                 if (names == "*") {
00114                     InsertModules<Type> visitor(m_modules);
00115                     globalModuleServer().foreachModule(typename Type::Name(), typename Type::Version(), visitor);
00116                 } else {
00117                     StringTokeniser tokeniser(names.c_str());
00118                     for (;;) {
00119                         const char* name = tokeniser.getToken();
00120                         if (string_empty(name)) {
00121                             break;
00122                         }
00123                         Module* module = globalModuleServer().findModule(typename Type::Name(),
00124                                 typename Type::Version(), name);
00125                         // Module not found in the global module server
00126                         if (module == 0) {
00127                             globalModuleServer().setError(true);
00128                             break;
00129                         } else {
00130                             m_modules.insert(name, *module);
00131                         }
00132                     }
00133                 }
00134             }
00135         }
00136         ModulesMap<Type>& get ()
00137         {
00138             return m_modules;
00139         }
00140 };
00141 
00142 #endif

Generated by  doxygen 1.6.2