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

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, see if we have an appropriate alias instead.
00103         else
00104         {
00105                 TypeAliasRegistry::const_iterator aliasPos = d_aliasRegistry.find(type);
00106 
00107                 // no alias either, throw an exception
00108                 if (aliasPos == d_aliasRegistry.end())
00109                 {
00110                         throw UnknownObjectException((utf8*)"WindowFactoryManager::getFactory - A WindowFactory object (or an alias) for '" + type + (utf8*)"' Window objects is not registered with the system.");
00111                 }
00112                 else
00113                 {
00114                         // recursively call getFactory, for the alias target type (allows aliasing of aliased names)
00115                         return getFactory(aliasPos->second.getActiveTarget());
00116                 }
00117 
00118         }
00119 
00120 }
00121 
00122 
00123 /*************************************************************************
00124         Returns true if a WindowFactory (or an alias) for a specified type
00125         is present
00126 *************************************************************************/
00127 bool WindowFactoryManager::isFactoryPresent(const String& name) const
00128 {
00129         // first try for a 'real' type
00130         if (d_factoryRegistry.find(name) != d_factoryRegistry.end())
00131         {
00132                 return true;
00133         }
00134         // no real type exists with that type, see if we have an appropriate alias instead.
00135         else
00136         {
00137                 return (d_aliasRegistry.find(name) != d_aliasRegistry.end());
00138         }
00139 
00140 }
00141 
00142 
00143 WindowFactoryManager& WindowFactoryManager::getSingleton(void)
00144 {
00145         return Singleton<WindowFactoryManager>::getSingleton();
00146 }
00147 
00148 
00149 WindowFactoryManager* WindowFactoryManager::getSingletonPtr(void)
00150 {
00151         return Singleton<WindowFactoryManager>::getSingletonPtr();
00152 }
00153 
00154 
00155 /*************************************************************************
00156         Return a WindowFactoryManager::WindowFactoryIterator object to
00157         iterate over the available WindowFactory types.
00158 *************************************************************************/
00159 WindowFactoryManager::WindowFactoryIterator     WindowFactoryManager::getIterator(void) const
00160 {
00161         return WindowFactoryIterator(d_factoryRegistry.begin(), d_factoryRegistry.end());
00162 }
00163 
00164 
00165 /*************************************************************************
00166         Return a WindowFactoryManager::TypeAliasIterator object to iterate
00167         over the defined aliases for window types.
00168 *************************************************************************/
00169 WindowFactoryManager::TypeAliasIterator WindowFactoryManager::getAliasIterator(void) const
00170 {
00171         return TypeAliasIterator(d_aliasRegistry.begin(), d_aliasRegistry.end());
00172 }
00173 
00174 
00175 /*************************************************************************
00176         Add a window type alias mapping
00177 *************************************************************************/
00178 void WindowFactoryManager::addWindowTypeAlias(const String& aliasName, const String& targetType)
00179 {
00180         // throw if target type does not exist
00181         if (!isFactoryPresent(targetType))
00182         {
00183                 throw UnknownObjectException((utf8*)"WindowFactoryManager::addWindowTypeAlias - alias '" + aliasName + "' could not be created because the target type '" + targetType + "' is unknown within the system.");
00184         }
00185 
00186         TypeAliasRegistry::iterator pos = d_aliasRegistry.find(aliasName);
00187 
00188         if (pos == d_aliasRegistry.end())
00189         {
00190                 d_aliasRegistry[aliasName].d_targetStack.push_back(targetType);
00191         }
00192         // alias already exists, add our new entry to the list already there
00193         else
00194         {
00195                 pos->second.d_targetStack.push_back(targetType);
00196         }
00197 
00198         Logger::getSingleton().logEvent((utf8*)"Window type alias named '" + aliasName + "' added for window type '" + targetType +"'.");
00199 }
00200 
00201 
00202 /*************************************************************************
00203         Remove a window type alias mapping
00204 *************************************************************************/
00205 void WindowFactoryManager::removeWindowTypeAlias(const String& aliasName, const String& targetType)
00206 {
00207         // find alias name
00208         TypeAliasRegistry::iterator pos = d_aliasRegistry.find(aliasName);
00209 
00210         // if alias name exists
00211         if (pos != d_aliasRegistry.end())
00212         {
00213                 // find the specified target for this alias
00214                 std::vector<String>::iterator aliasPos = std::find(pos->second.d_targetStack.begin(), pos->second.d_targetStack.end(), targetType);
00215                 
00216                 // if the target exists for this alias
00217                 if (aliasPos != pos->second.d_targetStack.end())
00218                 {
00219                         // erase the target mapping
00220                         pos->second.d_targetStack.erase(aliasPos);
00221 
00222                         Logger::getSingleton().logEvent((utf8*)"Window type alias named '" + aliasName + "' removed for window type '" + targetType +"'.");
00223 
00224                         // if the list of targets for this alias is now empty
00225                         if (pos->second.d_targetStack.empty())
00226                         {
00227                                 // erase the alias name also
00228                                 d_aliasRegistry.erase(aliasName);
00229 
00230                                 Logger::getSingleton().logEvent((utf8*)"Window type alias named '" + aliasName + "' has no more targets and has been removed.", Informative);
00231                         }
00232 
00233                 }
00234 
00235         }
00236 
00237 }
00238 
00239 
00241 /*************************************************************************
00242         Methods for AliasTargetStack class
00243 *************************************************************************/
00245 const String& WindowFactoryManager::AliasTargetStack::getActiveTarget(void) const
00246 {
00247         return d_targetStack.back();
00248 }
00249 
00250 
00251 uint WindowFactoryManager::AliasTargetStack::getStackedTargetCount(void) const
00252 {
00253         return (uint)d_targetStack.size();
00254 }
00255 
00256 
00257 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:08 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1