factorywithnames.h

Go to the documentation of this file.
00001 /*
00002     This file is part of Advanced Strategic Command; http://www.asc-hq.de
00003     Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program 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
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; see the file COPYING. If not, write to the
00017     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00018     Boston, MA  02111-1307  USA
00019 */
00020 
00021 
00022 #ifndef factoryWithNamesH
00023 #define factoryWithNamesH
00024 
00025 #include <sigc++/sigc++.h>
00026 #include "loki/Singleton.h"
00027 #include "loki/Functor.h"
00028 
00029 #include "factory.h"
00030 
00031 
00032 template < class AbstractProduct, typename IdentifierType, typename ObjectCreatorCallBack = AbstractProduct*(*)(), typename NameType = ASCString >
00033 class FactoryWithNames : protected Factory<AbstractProduct, IdentifierType>
00034 {
00035    private:
00036       typedef std::map<NameType, IdentifierType> NameMap;
00037       NameMap names;
00038    public:
00039       vector<NameType> getNames(){
00040          vector<NameType> nameList;
00041          
00042          for ( typename NameMap::iterator i = names.begin(); i != names.end(); ++i )
00043             nameList.push_back( i->first );
00044          return nameList;
00045       }
00046 
00047       IdentifierType getID( const ASCString& name )
00048       {
00049          return names[name];
00050       }
00051 
00052       bool registerClass( IdentifierType id, typename FactoryWithNames<AbstractProduct, IdentifierType, typename FactoryWithNames::ObjectCreatorCallBack, NameType>::ObjectCreatorCallBack createFn, Loki::Functor<NameType, LOKI_TYPELIST_1(const IdentifierType&)> nameProvider )
00053       {
00054          return registerClass( id, createFn, nameProvider(id) );
00055       }
00056 
00057       bool registerClass( IdentifierType id, typename FactoryWithNames<AbstractProduct, IdentifierType, typename FactoryWithNames::ObjectCreatorCallBack, NameType>::ObjectCreatorCallBack createFn, NameType name )
00058       {
00059          if ( Factory<AbstractProduct, IdentifierType>::registerClass ( id, createFn )) {
00060             names[name] = id;
00061             return true;
00062          } else
00063             return false;
00064       }
00065 
00066       AbstractProduct* createObject( IdentifierType id ) {
00067          return Factory<AbstractProduct, IdentifierType>::createObject( id );
00068       }
00069 
00070 };
00071 
00072 #endif

Generated on Mon May 21 01:26:32 2012 for Advanced Strategic Command by  doxygen 1.5.1