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>);
00050     }
00051 
00055     template<class ContainerType>
00056     static boost::shared_ptr<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 boost::shared_ptr<ContainerType>();
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         boost::shared_ptr<Object> ele=it->second();
00070         // try to cast ele up to ContainerType
00071         boost::shared_ptr<ContainerType> ret=boost::dynamic_pointer_cast<ContainerType>(ele);
00072         // if possible, return it
00073         if(ret)
00074           return ret;
00075       }
00076       // no matching element found: throw error
00077       throw std::runtime_error("No class named "+MBXMLUtils::X()%element->getTagName()+" found which is of type "+
00078                                typeid(ContainerType).name()+".");
00079     }
00080 
00082     template<class CreateType>
00083     static boost::shared_ptr<CreateType> create() {
00084       return boost::shared_ptr<CreateType>(new CreateType, &deleter<CreateType>);
00085     }
00086 
00088     template<class CreateType>
00089     static boost::shared_ptr<CreateType> create(const boost::shared_ptr<CreateType> &t) {
00090       return boost::shared_ptr<CreateType>(new CreateType(*t.get()), &deleter<CreateType>);
00091     }
00092 
00093   private:
00094 
00095     // a pointer to a function allocating an object
00096     typedef boost::shared_ptr<Object> (*allocateFkt)();
00097 
00098     // convinence typedefs
00099     typedef std::multimap<MBXMLUtils::FQN, allocateFkt> Map;
00100     typedef typename Map::iterator MapIt;
00101 
00102     // private ctor
00103     ObjectFactory() {}
00104 
00105     static void registerXMLName(const MBXMLUtils::FQN &name, allocateFkt alloc);
00106 
00107     // create an singleton instance of the object factory.
00108     // only declaration here and defition and explicit instantation for all Object in objectfactory.cc (required for Windows)
00109     static ObjectFactory& instance();
00110 
00111     // a multimap of all registered types
00112     Map registeredType;
00113 
00114     // a wrapper to allocate an object of type CreateType: used by create(xercesc::DOMElement *)
00115     template<class CreateType>
00116     static boost::shared_ptr<Object> allocate() {
00117       return boost::shared_ptr<CreateType>(new CreateType, &deleter<CreateType>);
00118     }
00119 
00120     // a wrapper to deallocate an object of type T: all dtors are protected but ObjectFactory is a friend of all classes
00121     template<class T>
00122     static void deleter(T *t) { delete t; }
00123 
00124 };
00125 
00129 template<class CreateType>
00130 class ObjectFactoryRegisterXMLNameHelper {
00131 
00132   public:
00133 
00135     ObjectFactoryRegisterXMLNameHelper(const MBXMLUtils::FQN &name) {
00136       ObjectFactory::registerXMLName<CreateType>(name);
00137     };
00138 
00139 };
00140 
00141 }
00142 
00143 #define OPENMBV_OBJECTFACTORY_CONCAT1(X, Y) X##Y
00144 #define OPENMBV_OBJECTFACTORY_CONCAT(X, Y) OPENMBV_OBJECTFACTORY_CONCAT1(X, Y)
00145 #define OPENMBV_OBJECTFACTORY_APPENDLINE(X) OPENMBV_OBJECTFACTORY_CONCAT(X, __LINE__)
00146 
00149 #define OPENMBV_OBJECTFACTORY_REGISTERXMLNAME(ThisType, name) \
00150   static OpenMBV::ObjectFactoryRegisterXMLNameHelper<ThisType> \
00151     OPENMBV_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
00152 
00153 #endif