00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _MBSIM_OBJECTFACTORY_H_
00021 #define _MBSIM_OBJECTFACTORY_H_
00022
00023 #include <map>
00024 #include <stdexcept>
00025 #include <typeinfo>
00026 #include "mbxmlutilstinyxml/tinyxml.h"
00027 #include "mbxmlutilstinyxml/utils.h"
00028 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
00029 # include <boost/static_assert.hpp>
00030 # include <boost/type_traits.hpp>
00031 #endif
00032 #include "mbsim/utils/utils.h"
00033
00034 #define COMMA ,
00035
00036 namespace MBSim {
00037
00041 template<class BaseType>
00042 class ObjectFactory {
00043
00044 public:
00045
00049 template<class CreateType>
00050 static void registerXMLName(const std::string &name) {
00051 registerXMLName(name, &allocate<CreateType>, &deallocate);
00052 }
00053
00057 template<class CreateType>
00058 static void registerXMLNameAsSingleton(const std::string &name) {
00059 registerXMLName(name, &singleton<CreateType>, NULL);
00060 }
00061
00065 template<class CreateType>
00066 static void deregisterXMLName(const std::string &name) {
00067 deregisterXMLName(name, &allocate<CreateType>);
00068 }
00069
00073 template<class CreateType>
00074 static void deregisterXMLNameAsSingleton(const std::string &name) {
00075 deregisterXMLName(name, &singleton<CreateType>);
00076 }
00077
00081 template<class ContainerType>
00082 static ContainerType* create(const MBXMLUtils::TiXmlElement *element) {
00083 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
00084
00085
00086 BOOST_STATIC_ASSERT_MSG((boost::is_convertible<ContainerType*, BaseType*>::value),
00087 "In MBSim::ObjectFactory<BaseType>::create<ContainerType>(...) ContainerType must be derived from BaseType.");
00088 #endif
00089
00090 if(element==NULL) return NULL;
00091
00092 std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(element->ValueStr());
00093 for(MapIt it=range.first; it!=range.second; it++) {
00094
00095 BaseType *ele=it->second.first();
00096
00097 ContainerType *ret=dynamic_cast<ContainerType*>(ele);
00098
00099 if(ret)
00100 return ret;
00101
00102
00103 else
00104 it->second.second(ele);
00105 }
00106
00107 throw std::runtime_error("No class named "+element->ValueStr()+" found which is of type "+
00108 MBXMLUtils::demangleSymbolName(typeid(ContainerType).name())+".");
00109 }
00110
00111 private:
00112
00113
00114 typedef BaseType* (*allocateFkt)();
00115
00116 typedef void (*deallocateFkt)(BaseType *obj);
00117
00118
00119 typedef std::multimap<std::string, std::pair<allocateFkt, deallocateFkt> > Map;
00120 typedef typename Map::iterator MapIt;
00121
00122
00123 ObjectFactory() {}
00124
00125 static void registerXMLName(const std::string &name, allocateFkt alloc, deallocateFkt dealloc) {
00126
00127 std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(name);
00128 for(MapIt it=range.first; it!=range.second; it++)
00129 if(it->second.first==alloc)
00130 return;
00131
00132 instance().registeredType.insert(std::make_pair(name, std::make_pair(alloc, dealloc)));
00133 }
00134
00135 static void deregisterXMLName(const std::string &name, allocateFkt alloc) {
00136
00137 std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(name);
00138 for(MapIt it=range.first; it!=range.second; it++)
00139 if(it->second.first==alloc) {
00140 instance().registeredType.erase(it);
00141 return;
00142 }
00143 }
00144
00145
00146
00147 static ObjectFactory<BaseType>& instance();
00148
00149
00150 Map registeredType;
00151
00152
00153 template<class CreateType>
00154 static BaseType* allocate() {
00155 return new CreateType;
00156 }
00157
00158
00159 static void deallocate(BaseType *obj) {
00160 delete obj;
00161 }
00162
00163
00164 template<class CreateType>
00165 static BaseType* singleton() {
00166 return CreateType::getInstance();
00167 }
00168
00169 };
00170
00174 template<class BaseType, class CreateType>
00175 class ObjectFactoryRegisterXMLNameHelper {
00176
00177 public:
00178
00180 ObjectFactoryRegisterXMLNameHelper(const std::string &name_) : name(name_) {
00181 ObjectFactory<BaseType>::template registerXMLName<CreateType>(name);
00182 };
00183
00185 ~ObjectFactoryRegisterXMLNameHelper() {
00186 ObjectFactory<BaseType>::template deregisterXMLName<CreateType>(name);
00187 };
00188
00189 private:
00190 std::string name;
00191
00192 };
00193
00197 template<class BaseType, class CreateType>
00198 class ObjectFactoryRegisterXMLNameHelperAsSingleton {
00199
00200 public:
00201
00203 ObjectFactoryRegisterXMLNameHelperAsSingleton(const std::string &name_) : name(name_) {
00204 ObjectFactory<BaseType>::template registerXMLNameAsSingleton<CreateType>(name);
00205 };
00206
00208 ~ObjectFactoryRegisterXMLNameHelperAsSingleton() {
00209 ObjectFactory<BaseType>::template deregisterXMLNameAsSingleton<CreateType>(name);
00210 };
00211
00212 private:
00213 std::string name;
00214
00215 };
00216
00217 }
00218
00219 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
00220 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
00221 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
00222
00226 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAME(BaseType, ThisType, name) \
00227 static MBSim::ObjectFactoryRegisterXMLNameHelper<BaseType,ThisType> \
00228 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
00229
00233 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAMEASSINGLETON(BaseType, ThisType, name) \
00234 static MBSim::ObjectFactoryRegisterXMLNameHelperAsSingleton<BaseType,ThisType> \
00235 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(name);
00236
00237 #endif