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

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cGuiACFactory.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/cGuiACFactory.h"
00026 
00027 #include "n2l/resourceManagement.h"
00028 
00029 #include "gui/cGuiElement.h"
00030 #include "gui/cGuiACInt.h"
00031 
00032 #include "n2l/dynVars.h"
00033 #include "n2l/vfs.h"
00034 
00035 /******************************************************************************/
00036 namespace n2l
00037 {
00038 
00039     /**************************************************************************/
00040     cGuiACFactory::cGuiACFactory()
00041     {
00042     }
00043 
00044     /**************************************************************************/
00045     cGuiACFactory::~cGuiACFactory()
00046     {
00047     }
00048 
00049     /**************************************************************************/
00050     const tBool cGuiACFactory::registerLoader( const tString &iHeader,
00051         const tLoaderDefMethod iMethod )
00052     {
00053         tDefLoadMap::const_iterator i = mDefLoadMap.find(strToLower(iHeader));
00054         if (i!=mDefLoadMap.end() && i->second!=iMethod)
00055             throw cDuplicateKeyException( "cGuiACFactory::registerLoader",
00056                 "Two loaders tried to register the same type", iHeader );
00057         mDefLoadMap[strToLower(iHeader)] = iMethod;
00058         return true;
00059     }
00060 
00061     /**************************************************************************/
00062     const cAutoPtr<cGuiACInt> cGuiACFactory::load(
00063         const cDynVar &iDef ) const
00064     {
00065         if (!iDef.isArray())
00066             return load( *cResourceManager::getVfsNode( iDef ) );
00067     
00068         if (!iDef.keyExists("type"))
00069             throw cBadDataUseException( "cGuiACFactory::load(iDef)",
00070                 "No type provided" );
00071 
00072         // Legacy load method
00073         if (iDef.keyExistsAsArray("data"))
00074             return load(iDef["data"], iDef["type"]);
00075         else
00076             return load(iDef, iDef["type"]);
00077     }
00078 
00079     /**************************************************************************/
00080     const cAutoPtr<cGuiACInt> cGuiACFactory::load(
00081         const cDynVar &iDef, const tString &iType ) const
00082     {
00083         tDefLoadMap::const_iterator i = mDefLoadMap.find(strToLower(iType));
00084         if (i == mDefLoadMap.end())
00085             throw cBadDataUseException( "cGuiACFactory::load(iDef,iType)",
00086                 "Asked to load a type I have no knowledge of: " + iType );
00087         return i->second(iDef);     
00088     }
00089 
00090     /**************************************************************************/
00091     const cAutoPtr<cGuiACInt> cGuiACFactory::load(
00092         const cVfsNodeInterface &iNode ) const
00093     {
00094         if (!iNode.likeFile())
00095             throw cBadDataUseException( "cGuiACFactory::load(iNode)",
00096                 "Node is not like a file: " + iNode.name() );
00097 
00098         const tString FirstLine( iNode.firstLine() );
00099         if (iNode.buffer().size()<=(iNode.firstLine().size()+1))
00100             throw cParsingException( "cGuiACFactory::load(iNode)",
00101                 "No data after header in file: " + iNode.name() );
00102 
00103         cDynVar def;
00104         def.unserialize( iNode.buffer().c_str()+FirstLine.size()+1 );
00105 
00106         try {
00107             return load( def, FirstLine );
00108         }
00109         catch ( const cException &iE )
00110         {
00111             throw cBadDataUseException( "cGuiACFactory::load(iDef)",
00112                 "Failed inner load on file \"" + iNode.name() + "\" with "
00113                 "exception: " + tString(iE) );
00114         }
00115     }
00116 
00117 } // namespace n2l
©2012 Aaron Cameron