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