Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIWindowFactoryManager.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIWindowFactoryManager.cpp
00003         created:        22/2/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implements the WindowFactoryManager
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIWindowFactoryManager.h"
00027 #include "CEGUIWindowFactory.h"
00028 #include "CEGUIExceptions.h"
00029 #include <algorithm>
00030 
00031 // Start of CEGUI namespace section
00032 namespace CEGUI
00033 {
00034 /*************************************************************************
00035         Static Data Definitions
00036 *************************************************************************/
00037 // singleton instance pointer
00038 template<> WindowFactoryManager* Singleton<WindowFactoryManager>::ms_Singleton  = NULL;
00039 
00040 
00041 /*************************************************************************
00042         Adds a WindowFactory object to the registry
00043 *************************************************************************/
00044 void WindowFactoryManager::addFactory(WindowFactory* factory)
00045 {
00046         // throw exception if passed factory is null.
00047         if (factory == NULL)
00048         {
00049                 throw NullObjectException((utf8*)"WindowFactoryManager::addFactory - The provided WindowFactory pointer was NULL");
00050         }
00051 
00052         // throw exception if type name for factory is already in use
00053         if (d_factoryRegistry.find(factory->getTypeName()) != d_factoryRegistry.end())
00054         {
00055                 throw AlreadyExistsException((utf8*)"WindowFactoryManager::addFactory - A WindowFactory for type '" + factory->getTypeName() + (utf8*)"' is already registered.");
00056         }
00057 
00058         // add the factory to the registry
00059         d_factoryRegistry[factory->getTypeName()] = factory;
00060 
00061         Logger::getSingleton().logEvent((utf8*)"WindowFactory for '" + factory->getTypeName() +"' windows added.");
00062 }
00063 
00064 
00065 /*************************************************************************
00066         removes a WindowFactory from the registry (by name)
00067 *************************************************************************/
00068 void WindowFactoryManager::removeFactory(const String& name)
00069 {
00070         d_factoryRegistry.erase(name);
00071 
00072         Logger::getSingleton().logEvent((utf8*)"WindowFactory for '" + name +"' windows removed.");
00073 }
00074 
00075 
00076 /*************************************************************************
00077         removes a WindowFactory from the registry (by pointer)
00078 *************************************************************************/
00079 void WindowFactoryManager::removeFactory(WindowFactory* factory)
00080 {
00081         if (factory != NULL)
00082         {
00083                 removeFactory(factory->getTypeName());
00084         }
00085 
00086 }
00087 
00088 
00089 /*************************************************************************
00090         returns a pointer to the requested WindowFactory object
00091 *************************************************************************/
00092 WindowFactory* WindowFactoryManager::getFactory(const String& type) const
00093 {
00094         // first try for a 'real' type
00095         WindowFactoryRegistry::const_iterator pos = d_factoryRegistry.find(type);
00096 
00097         // found an actual factory for this type
00098         if (pos != d_factoryRegistry.end())
00099         {
00100                 return pos->second;
00101         }
00102         // no real type exists with that type so try for an aliased type
00103         else
00104         {
00105                 TypeAliasRegistry::const_iterator aliasPos = d_aliasRegistry.find(type);
00106 
00107         // found aliased type
00108                 if (aliasPos != d_aliasRegistry.end())
00109                 {
00110                         // recursively call getFactory, for the alias target type (allows aliasing of aliased names)
00111                         return getFactory(aliasPos->second.getActiveTarget());
00112                 }
00113                 // no aliased type, try for a falagard mapped type
00114                 else
00115                 {
00116             FalagardMapRegistry::const_iterator falagard = d_falagardRegistry.find(type);
00117 
00118             // found falagard mapping for this type
00119             if (falagard != d_falagardRegistry.end())
00120             {
00121                 // recursively call getFactory on the target base type
00122                 return getFactory(falagard->second.d_baseType);
00123             }
00124             // type not found anywhere, give up with an exception.
00125             else
00126             {
00127               throw UnknownObjectException("WindowFactoryManager::getFactory - A WindowFactory object (nor an alias, or mapping) for '" + type + "' Window objects is not registered with the system.");
00128             }
00129         }
00130     }
00131 }
00132 
00133 
00134 /*************************************************************************
00135         Returns true if a WindowFactory (or an alias) for a specified type
00136         is present
00137 *************************************************************************/
00138 bool WindowFactoryManager::isFactoryPresent(const String& name) const
00139 {
00140         // first try for a 'real' type
00141         if (d_factoryRegistry.find(name) != d_factoryRegistry.end())
00142         {
00143                 return true;
00144         }
00145         // no real type exists with that type, see if we have an appropriate alias instead.
00146         else
00147         {
00148                 return (d_aliasRegistry.find(name) != d_aliasRegistry.end());
00149         }
00150 
00151 }
00152 
00153 
00154 WindowFactoryManager& WindowFactoryManager::getSingleton(void)
00155 {
00156         return Singleton<WindowFactoryManager>::getSingleton();
00157 }
00158 
00159 
00160 WindowFactoryManager* WindowFactoryManager::getSingletonPtr(void)
00161 {
00162         return Singleton<WindowFactoryManager>::getSingletonPtr();
00163 }
00164 
00165 
00166 /*************************************************************************
00167         Return a WindowFactoryManager::WindowFactoryIterator object to
00168         iterate over the available WindowFactory types.
00169 *************************************************************************/
00170 WindowFactoryManager::WindowFactoryIterator     WindowFactoryManager::getIterator(void) const
00171 {
00172         return WindowFactoryIterator(d_factoryRegistry.begin(), d_factoryRegistry.end());
00173 }
00174 
00175 
00176 /*************************************************************************
00177         Return a WindowFactoryManager::TypeAliasIterator object to iterate
00178         over the defined aliases for window types.
00179 *************************************************************************/
00180 WindowFactoryManager::TypeAliasIterator WindowFactoryManager::getAliasIterator(void) const
00181 {
00182         return TypeAliasIterator(d_aliasRegistry.begin(), d_aliasRegistry.end());
00183 }
00184 
00185 
00186 /*************************************************************************
00187         Add a window type alias mapping
00188 *************************************************************************/
00189 void WindowFactoryManager::addWindowTypeAlias(const String& aliasName, const String& targetType)
00190 {
00191         // throw if target type does not exist
00192         if (!isFactoryPresent(targetType))
00193         {
00194                 throw UnknownObjectException((utf8*)"WindowFactoryManager::addWindowTypeAlias - alias '" + aliasName + "' could not be created because the target type '" + targetType + "' is unknown within the system.");
00195         }
00196 
00197         TypeAliasRegistry::iterator pos = d_aliasRegistry.find(aliasName);
00198 
00199         if (pos == d_aliasRegistry.end())
00200         {
00201                 d_aliasRegistry[aliasName].d_targetStack.push_back(targetType);
00202         }
00203         // alias already exists, add our new entry to the list already there
00204         else
00205         {
00206                 pos->second.d_targetStack.push_back(targetType);
00207         }
00208 
00209         Logger::getSingleton().logEvent((utf8*)"Window type alias named '" + aliasName + "' added for window type '" + targetType +"'.");
00210 }
00211 
00212 
00213 /*************************************************************************
00214         Remove a window type alias mapping
00215 *************************************************************************/
00216 void WindowFactoryManager::removeWindowTypeAlias(const String& aliasName, const String& targetType)
00217 {
00218         // find alias name
00219         TypeAliasRegistry::iterator pos = d_aliasRegistry.find(aliasName);
00220 
00221         // if alias name exists
00222         if (pos != d_aliasRegistry.end())
00223         {
00224                 // find the specified target for this alias
00225                 std::vector<String>::iterator aliasPos = std::find(pos->second.d_targetStack.begin(), pos->second.d_targetStack.end(), targetType);
00226                 
00227                 // if the target exists for this alias
00228                 if (aliasPos != pos->second.d_targetStack.end())
00229                 {
00230                         // erase the target mapping
00231                         pos->second.d_targetStack.erase(aliasPos);
00232 
00233                         Logger::getSingleton().logEvent((utf8*)"Window type alias named '" + aliasName + "' removed for window type '" + targetType +"'.");
00234 
00235                         // if the list of targets for this alias is now empty
00236                         if (pos->second.d_targetStack.empty())
00237                         {
00238                                 // erase the alias name also
00239                                 d_aliasRegistry.erase(aliasName);
00240 
00241                                 Logger::getSingleton().logEvent((utf8*)"Window type alias named '" + aliasName + "' has no more targets and has been removed.", Informative);
00242                         }
00243 
00244                 }
00245 
00246         }
00247 
00248 }
00249 
00250 void WindowFactoryManager::addFalagardWindowMapping(const String& newType, const String& targetType, const String& lookName)
00251 {
00252     WindowFactoryManager::FalagardWindowMapping mapping;
00253     mapping.d_windowType = newType;
00254     mapping.d_baseType   = targetType;
00255     mapping.d_lookName   = lookName;
00256 
00257     // see if the type we're creating already exists
00258     if (d_falagardRegistry.find(newType) != d_falagardRegistry.end())
00259     {
00260         // type already exists, log the fact that it's going to be replaced.
00261         Logger::getSingleton().logEvent("Falagard mapping for type '" + newType + "' already exists - current mapping will be replaced.");
00262     }
00263 
00264     Logger::getSingleton().logEvent("Creating falagard mapping for type '" + newType + "' using base type '" + targetType + "' and LookN'Feel '" + lookName + "'.");
00265 
00266     d_falagardRegistry[newType] = mapping;
00267 }
00268 
00269 void WindowFactoryManager::removeFalagardWindowMapping(const String& type)
00270 {
00271     FalagardMapRegistry::iterator iter = d_falagardRegistry.find(type);
00272 
00273     if (iter != d_falagardRegistry.end())
00274     {
00275         Logger::getSingleton().logEvent("Removing falagard mapping for type '" + type + "'.");
00276         d_falagardRegistry.erase(iter);
00277     }
00278 }
00279 
00280 WindowFactoryManager::FalagardMappingIterator WindowFactoryManager::getFalagardMappingIterator() const
00281 {
00282     return FalagardMappingIterator(d_falagardRegistry.begin(), d_falagardRegistry.end());
00283 }
00284 
00285 bool WindowFactoryManager::isFalagardMappedType(const String& type) const
00286 {
00287     return d_falagardRegistry.find(type) != d_falagardRegistry.end();
00288 }
00289 
00290 const String& WindowFactoryManager::getMappedLookForType(const String& type) const
00291 {
00292     FalagardMapRegistry::const_iterator iter = d_falagardRegistry.find(type);
00293 
00294     if (iter != d_falagardRegistry.end())
00295     {
00296         return (*iter).second.d_lookName;
00297     }
00298     // type does not exist as a mapped type
00299     else
00300     {
00301         throw InvalidRequestException("WindowFactoryManager::getMappedLookForType - Window factory type '" + type + "' is not a falagard mapped type (or does not exist at all).");
00302     }
00303 }
00304 
00306 /*************************************************************************
00307         Methods for AliasTargetStack class
00308 *************************************************************************/
00310 const String& WindowFactoryManager::AliasTargetStack::getActiveTarget(void) const
00311 {
00312         return d_targetStack.back();
00313 }
00314 
00315 
00316 uint WindowFactoryManager::AliasTargetStack::getStackedTargetCount(void) const
00317 {
00318         return (uint)d_targetStack.size();
00319 }
00320 
00321 
00322 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:35 2005 for Crazy Eddies GUI System by  doxygen 1.4.3