Hosted by: BerliOS Developer Logo

OpenMBV C++ Interface

BerliOS Project Page
/home/user/MBSimWindows/openmbv/openmbvcppinterface/openmbvcppinterface/objectfactory.h
00001 /* Copyright (C) 2004-2010 OpenMBV 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 _OPENMBV_OBJECTFACTORY_H_
00021 #define _OPENMBV_OBJECTFACTORY_H_
00022 
00023 #include <map>
00024 #include <stdexcept>
00025 #include <typeinfo>
00026 #include "mbxmlutilstinyxml/tinyxml.h"
00027 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
00028 # include <boost/static_assert.hpp>
00029 # include <boost/type_traits.hpp>
00030 #endif
00031 
00032 namespace OpenMBV {
00033 
00034 class Object;
00035 
00039 class ObjectFactory {
00040 
00041   public:
00042 
00046     template<class CreateType>
00047     static void registerXMLName(const std::string &name) {
00048       registerXMLName(name, &allocate<CreateType>, &deallocate);
00049     }
00050 
00054     template<class ContainerType>
00055     static ContainerType* create(const MBXMLUtils::TiXmlElement *element) {
00056 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
00057       // just check if ContainerType is derived from Object if not throw a compile error if boost is avaliable
00058       // if boost is not avaliable a runtime error will occure later. (so it does not care if boost is not available)
00059       BOOST_STATIC_ASSERT_MSG((boost::is_convertible<ContainerType*, Object*>::value),
00060         "In OpenMBV::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from Object.");
00061 #endif
00062       // return NULL if no input is supplied
00063       if(element==NULL) return NULL;
00064       // loop over all all registred types corresponding to element->ValueStr()
00065       std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(element->ValueStr());
00066       for(MapIt it=range.first; it!=range.second; it++) {
00067         // allocate a new object using the allocate function pointer
00068         Object *ele=it->second.first();
00069         // try to cast ele up to ContainerType
00070         ContainerType *ret=dynamic_cast<ContainerType*>(ele);
00071         // if possible, return it
00072         if(ret)
00073           return ret;
00074         // if not possible, deallocate newly created (wrong) object and continue searching
00075         else
00076           it->second.second(ele);
00077       }
00078       // no matching element found: throw error
00079       throw std::runtime_error("No class named "+element->ValueStr()+" found which is of type "+
00080                                typeid(ContainerType).name()+".");
00081     }
00082 
00083   private:
00084 
00085     // a pointer to a function allocating an object
00086     typedef Object* (*allocateFkt)();
00087     // a pointer to a function deallocating an object
00088     typedef void (*deallocateFkt)(Object *obj);
00089 
00090     // convinence typedefs
00091     typedef std::multimap<std::string, std::pair<allocateFkt, deallocateFkt> > Map;
00092     typedef typename Map::iterator MapIt;
00093 
00094     // private ctor
00095     ObjectFactory() {}
00096 
00097     static void registerXMLName(const std::string &name, allocateFkt alloc, deallocateFkt dealloc);
00098 
00099     // create an singleton instance of the object factory.
00100     // only declaration here and defition and explicit instantation for all Object in objectfactory.cc (required for Windows)
00101     static ObjectFactory& instance();
00102 
00103     // a multimap of all registered types
00104     Map registeredType;
00105 
00106     // a wrapper to allocate an object of type CreateType
00107     template<class CreateType>
00108     static Object* allocate() {
00109       return new CreateType;
00110     }
00111 
00112     // a wrapper to deallocate an object created by allocate
00113     static void deallocate(Object *obj);
00114 
00115 };
00116 
00120 template<class CreateType>
00121 class ObjectFactoryRegisterXMLNameHelper {
00122 
00123   public:
00124 
00126     ObjectFactoryRegisterXMLNameHelper(const std::string &name) {
00127       ObjectFactory::registerXMLName<CreateType>(name);
00128     };
00129 
00130 };
00131 
00132 }
00133 
00134 #define OPENMBV_OBJECTFACTORY_CONCAT1(X, Y) X##Y
00135 #define OPENMBV_OBJECTFACTORY_CONCAT(X, Y) OPENMBV_OBJECTFACTORY_CONCAT1(X, Y)
00136 #define OPENMBV_OBJECTFACTORY_APPENDLINE(X) OPENMBV_OBJECTFACTORY_CONCAT(X, __LINE__)
00137 
00140 #define OPENMBV_OBJECTFACTORY_REGISTERXMLNAME(ThisType, name) \
00141   static OpenMBV::ObjectFactoryRegisterXMLNameHelper<ThisType> \
00142     OPENMBV_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
00143 
00144 #endif