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 <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
00059
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
00064 if(element==NULL) return NULL;
00065
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
00069 Object *ele=it->second.first();
00070
00071 ContainerType *ret=dynamic_cast<ContainerType*>(ele);
00072
00073 if(ret)
00074 return ret;
00075
00076 else
00077 it->second.second(ele);
00078 }
00079
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
00087 typedef Object* (*allocateFkt)();
00088
00089 typedef void (*deallocateFkt)(Object *obj);
00090
00091
00092 typedef std::multimap<MBXMLUtils::FQN, std::pair<allocateFkt, deallocateFkt> > Map;
00093 typedef typename Map::iterator MapIt;
00094
00095
00096 ObjectFactory() {}
00097
00098 static void registerXMLName(const MBXMLUtils::FQN &name, allocateFkt alloc, deallocateFkt dealloc);
00099
00100
00101
00102 static ObjectFactory& instance();
00103
00104
00105 Map registeredType;
00106
00107
00108 template<class CreateType>
00109 static Object* allocate() {
00110 return new CreateType;
00111 }
00112
00113
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