MBSim

Project Page
objectfactory.h
00001 /* Copyright (C) 2004-2010 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: friedrich.at.gc@googlemail.com
00018  */
00019 
00020 #ifndef _MBSIM_OBJECTFACTORY_H_
00021 #define _MBSIM_OBJECTFACTORY_H_
00022 
00023 #include <map>
00024 #include <stdexcept>
00025 #include <typeinfo>
00026 #include "mbxmlutilstinyxml/tinyxml.h"
00027 #include "mbxmlutilstinyxml/utils.h"
00028 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
00029 # include <boost/static_assert.hpp>
00030 # include <boost/type_traits.hpp>
00031 #endif
00032 #include "mbsim/utils/utils.h"
00033 
00034 #define COMMA ,
00035 
00036 namespace MBSim {
00037 
00041 template<class BaseType>
00042 class ObjectFactory {
00043 
00044   public:
00045 
00049     template<class CreateType>
00050     static void registerXMLName(const std::string &name) {
00051       registerXMLName(name, &allocate<CreateType>, &deallocate);
00052     }
00053 
00057     template<class CreateType>
00058     static void registerXMLNameAsSingleton(const std::string &name) {
00059       registerXMLName(name, &singleton<CreateType>, NULL);
00060     }
00061 
00065     template<class CreateType>
00066     static void deregisterXMLName(const std::string &name) {
00067       deregisterXMLName(name, &allocate<CreateType>);
00068     }
00069 
00073     template<class CreateType>
00074     static void deregisterXMLNameAsSingleton(const std::string &name) {
00075       deregisterXMLName(name, &singleton<CreateType>);
00076     }
00077 
00081     template<class ContainerType>
00082     static ContainerType* create(const MBXMLUtils::TiXmlElement *element) {
00083 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
00084       // just check if ContainerType is derived from BaseType if not throw a compile error if boost is avaliable
00085       // if boost is not avaliable a runtime error will occure later. (so it does not care if boost is not available)
00086       BOOST_STATIC_ASSERT_MSG((boost::is_convertible<ContainerType*, BaseType*>::value),
00087         "In MBSim::ObjectFactory<BaseType>::create<ContainerType>(...) ContainerType must be derived from BaseType.");
00088 #endif
00089       // return NULL if no input is supplied
00090       if(element==NULL) return NULL;
00091       // loop over all all registred types corresponding to element->ValueStr()
00092       std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(element->ValueStr());
00093       for(MapIt it=range.first; it!=range.second; it++) {
00094         // allocate a new object OR get singleton object using the allocate function pointer
00095         BaseType *ele=it->second.first();
00096         // try to cast ele up to ContainerType
00097         ContainerType *ret=dynamic_cast<ContainerType*>(ele);
00098         // if possible, return it
00099         if(ret)
00100           return ret;
00101         // if not possible, deallocate newly created (wrong) object OR do nothing for
00102         // singleton objects (is maybe reused later) and continue searching
00103         else
00104           it->second.second(ele);
00105       }
00106       // no matching element found: throw error
00107       throw std::runtime_error("No class named "+element->ValueStr()+" found which is of type "+
00108                                MBXMLUtils::demangleSymbolName(typeid(ContainerType).name())+".");
00109     }
00110 
00111   private:
00112 
00113     // a pointer to a function allocating an object
00114     typedef BaseType* (*allocateFkt)();
00115     // a pointer to a function deallocating an object
00116     typedef void (*deallocateFkt)(BaseType *obj);
00117 
00118     // convinence typedefs
00119     typedef std::multimap<std::string, std::pair<allocateFkt, deallocateFkt> > Map;
00120     typedef typename Map::iterator MapIt;
00121 
00122     // private ctor
00123     ObjectFactory() {}
00124 
00125     static void registerXMLName(const std::string &name, allocateFkt alloc, deallocateFkt dealloc) {
00126       // check if name was already registred with the same &allocate<CreateType>: if yes return and do not add it twice
00127       std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(name);
00128       for(MapIt it=range.first; it!=range.second; it++)
00129         if(it->second.first==alloc)
00130           return;
00131       // name is not registred with &allocate<CreateType>: register it
00132       instance().registeredType.insert(std::make_pair(name, std::make_pair(alloc, dealloc)));
00133     }
00134 
00135     static void deregisterXMLName(const std::string &name, allocateFkt alloc) {
00136       // search all registered funcs of name
00137       std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(name);
00138       for(MapIt it=range.first; it!=range.second; it++)
00139         if(it->second.first==alloc) {
00140           instance().registeredType.erase(it);
00141           return;
00142         }
00143     }
00144 
00145     // create an singleton instance of the object factory.
00146     // only declaration here and defition and explicit instantation for all BaseType in objectfactory.cc (required for Windows)
00147     static ObjectFactory<BaseType>& instance();
00148 
00149     // a multimap of all registered types
00150     Map registeredType;
00151 
00152     // a wrapper to allocate an object of type CreateType
00153     template<class CreateType>
00154     static BaseType* allocate() {
00155       return new CreateType;
00156     }
00157 
00158     // a wrapper to deallocate an object created by allocate
00159     static void deallocate(BaseType *obj) {
00160       delete obj;
00161     }
00162 
00163     // a wrapper to get an singleton object of type CreateType (Must have the same signature as allocate()
00164     template<class CreateType>
00165     static BaseType* singleton() {
00166       return CreateType::getInstance();
00167     }
00168 
00169 };
00170 
00174 template<class BaseType, class CreateType>
00175 class ObjectFactoryRegisterXMLNameHelper {
00176 
00177   public:
00178 
00180     ObjectFactoryRegisterXMLNameHelper(const std::string &name_) : name(name_) {
00181       ObjectFactory<BaseType>::template registerXMLName<CreateType>(name);
00182     };
00183 
00185     ~ObjectFactoryRegisterXMLNameHelper() {
00186       ObjectFactory<BaseType>::template deregisterXMLName<CreateType>(name);
00187     };
00188 
00189   private:
00190     std::string name;
00191 
00192 };
00193 
00197 template<class BaseType, class CreateType>
00198 class ObjectFactoryRegisterXMLNameHelperAsSingleton {
00199 
00200   public:
00201 
00203     ObjectFactoryRegisterXMLNameHelperAsSingleton(const std::string &name_) : name(name_) {
00204       ObjectFactory<BaseType>::template registerXMLNameAsSingleton<CreateType>(name);
00205     };
00206 
00208     ~ObjectFactoryRegisterXMLNameHelperAsSingleton() {
00209       ObjectFactory<BaseType>::template deregisterXMLNameAsSingleton<CreateType>(name);
00210     };
00211 
00212   private:
00213     std::string name;
00214 
00215 };
00216 
00217 }
00218 
00219 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
00220 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
00221 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
00222 
00226 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAME(BaseType, ThisType, name) \
00227   static MBSim::ObjectFactoryRegisterXMLNameHelper<BaseType,ThisType> \
00228     MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
00229 
00233 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAMEASSINGLETON(BaseType, ThisType, name) \
00234   static MBSim::ObjectFactoryRegisterXMLNameHelperAsSingleton<BaseType,ThisType> \
00235     MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(name);
00236 
00237 #endif