00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00058
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
00063 if(element==NULL) return NULL;
00064
00065 std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(element->ValueStr());
00066 for(MapIt it=range.first; it!=range.second; it++) {
00067
00068 Object *ele=it->second.first();
00069
00070 ContainerType *ret=dynamic_cast<ContainerType*>(ele);
00071
00072 if(ret)
00073 return ret;
00074
00075 else
00076 it->second.second(ele);
00077 }
00078
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
00086 typedef Object* (*allocateFkt)();
00087
00088 typedef void (*deallocateFkt)(Object *obj);
00089
00090
00091 typedef std::multimap<std::string, std::pair<allocateFkt, deallocateFkt> > Map;
00092 typedef typename Map::iterator MapIt;
00093
00094
00095 ObjectFactory() {}
00096
00097 static void registerXMLName(const std::string &name, allocateFkt alloc, deallocateFkt dealloc);
00098
00099
00100
00101 static ObjectFactory& instance();
00102
00103
00104 Map registeredType;
00105
00106
00107 template<class CreateType>
00108 static Object* allocate() {
00109 return new CreateType;
00110 }
00111
00112
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