AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Sunday the 20th of May 2012 @ 04:46pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cGuiFactory.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "gui/cGuiFactory.h"
00026 
00027 #include "gui/cGuiElement.h"
00028 
00029 #include "n2l/dynVars.h"
00030 #include "n2l/vfs.h"
00031 
00032 /******************************************************************************/
00033 namespace n2l
00034 {
00035 
00036     /**************************************************************************/
00037     cGuiFactory::cGuiFactory()
00038     {
00039     }
00040 
00041     /**************************************************************************/
00042     cGuiFactory::~cGuiFactory()
00043     {
00044     }
00045 
00046     /**************************************************************************/
00047     const tBool cGuiFactory::registerLoader( const tString &iHeader,
00048         const tLoaderDefMethod iMethod )
00049     {
00050         tDefLoadMap::const_iterator i = mDefLoadMap.find(strToLower(iHeader));
00051         if (i!=mDefLoadMap.end() && i->second!=iMethod)
00052             throw cDuplicateKeyException( "cGuiFactory::registerLoader",
00053                 "Two loaders tried to register the same type", iHeader );
00054         return mDefLoadMap[strToLower(iHeader)] = iMethod;
00055     }
00056 
00057     /**************************************************************************/
00058     const cAutoPtr<cGuiElement> cGuiFactory::load( const cDynVar &iDef ) const
00059     {
00060         if (!iDef.keyExists("type"))
00061             throw cBadDataUseException( "cGuiFactory::load(iDef)",
00062                 "No type provided" );
00063 
00064         // Legacy load method
00065         if (iDef.keyExistsAsArray("data"))
00066             return load(iDef["data"], iDef["type"]);
00067         else
00068             return load(iDef, iDef["type"]);
00069     }
00070 
00071     /**************************************************************************/
00072     const cAutoPtr<cGuiElement> cGuiFactory::load( const cDynVar &iDef,
00073         const tString &iType ) const
00074     {
00075         tDefLoadMap::const_iterator i = mDefLoadMap.find(strToLower(iType));
00076         if (i == mDefLoadMap.end())
00077             throw cBadDataUseException( "cGuiFactory::load(iDef,iType)",
00078                 "Asked to load a type I have no knowledge of: " + iType );
00079         return i->second(iDef);     
00080     }
00081 
00082     /**************************************************************************/
00083     const cAutoPtr<cGuiElement> cGuiFactory::load(
00084         const cVfsNodeInterface &iNode ) const
00085     {
00086         if (!iNode.likeFile())
00087             throw cBadDataUseException( "cGuiFactory::load(iNode)",
00088                 "Node is not like a file: " + iNode.name() );
00089 
00090         const tString FirstLine( iNode.firstLine() );
00091         if (iNode.buffer().size()<=(iNode.firstLine().size()+1))
00092             throw cParsingException( "cGuiFactory::load(iNode)",
00093                 "No data after header in file: " + iNode.name() );
00094 
00095         cDynVar def;
00096         def.unserialize( iNode.buffer().c_str()+FirstLine.size()+1 );
00097 
00098         try {
00099             return load( def, FirstLine );
00100         }
00101         catch ( const cException &iE )
00102         {
00103             throw cBadDataUseException( "cGuiFactory::load(iDef)",
00104                 "Failed inner load on file \"" + iNode.name() + "\" with "
00105                 "exception: " + tString(iE) );
00106         }
00107     }
00108 
00109 } // namespace n2l
©2012 Aaron Cameron