20 #ifndef _MBSIM_OBJECTFACTORY_H_
21 #define _MBSIM_OBJECTFACTORY_H_
26 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
27 # include <boost/static_assert.hpp>
28 # include <boost/type_traits.hpp>
30 #include "mbsim/utils/utils.h"
31 #include <mbsim/mbsim_event.h>
32 #include <mbxmlutilshelper/utils.h>
33 #include "fmatvec/atom.h"
43 DOMEvalExceptionStack(
const xercesc::DOMElement *element) : MBXMLUtils::DOMEvalException(
"", element) {}
46 void add(
const std::string &type,
const boost::shared_ptr<MBXMLUtils::DOMEvalException> &ex);
47 const char* what()
const throw();
48 std::vector<std::pair<std::string, boost::shared_ptr<MBXMLUtils::DOMEvalException> > > &getExceptionVector();
50 std::vector<std::pair<std::string, boost::shared_ptr<MBXMLUtils::DOMEvalException> > > exVec;
51 mutable std::string whatStr;
52 void generateWhat(std::stringstream &str,
const std::string &indent)
const;
60 MBXMLUtils::DOMEvalException(type, element) {}
75 template<
class CreateType>
83 template<
class CreateType>
91 template<
class CreateType>
99 template<
class CreateType>
107 template<
class ContainerType>
109 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
112 BOOST_STATIC_ASSERT_MSG((boost::is_convertible<ContainerType*, fmatvec::Atom*>::value),
113 "In MBSim::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from fmatvec::Atom.");
117 throw MBSimError(
"Internal error: NULL argument specified.");
119 MBXMLUtils::FQN fqn=MBXMLUtils::E(element)->getTagName();
120 NameMapIt nameIt=instance().registeredType.find(fqn);
121 if(nameIt==instance().registeredType.end())
122 throw MBXMLUtils::DOMEvalException(
"Internal error: No objects of name {"+fqn.first+
"}"+fqn.second+
" registred", element);
125 for(AllocDeallocVectorIt allocDeallocIt=nameIt->second.begin(); allocDeallocIt!=nameIt->second.end(); ++allocDeallocIt) {
129 ContainerType *ret=
dynamic_cast<ContainerType*
>(ele);
132 allErrors.add(MBXMLUtils::demangleSymbolName(
typeid(*ele).name()),
133 boost::make_shared<DOMEvalExceptionWrongType>(
134 MBXMLUtils::demangleSymbolName(
typeid(ContainerType).name()), element));
135 allocDeallocIt->second(ele);
139 ret->initializeUsingXML(const_cast<xercesc::DOMElement*>(element));
143 allErrors.add(MBXMLUtils::demangleSymbolName(
typeid(*ele).name()), boost::make_shared<DOMEvalExceptionStack>(ex));
145 catch(MBXMLUtils::DOMEvalException &ex) {
146 allErrors.add(MBXMLUtils::demangleSymbolName(
typeid(*ele).name()), boost::make_shared<MBXMLUtils::DOMEvalException>(ex));
148 catch(std::exception &ex) {
149 allErrors.add(MBXMLUtils::demangleSymbolName(
typeid(*ele).name()),
150 boost::make_shared<MBXMLUtils::DOMEvalException>(ex.what(), element));
153 allErrors.add(MBXMLUtils::demangleSymbolName(
typeid(*ele).name()),
154 boost::make_shared<MBXMLUtils::DOMEvalException>(
"Unknwon exception", element));
156 allocDeallocIt->second(ele);
170 typedef std::vector<std::pair<AllocateFkt, DeallocateFkt> > AllocDeallocVector;
171 typedef AllocDeallocVector::iterator AllocDeallocVectorIt;
172 typedef std::map<MBXMLUtils::FQN, AllocDeallocVector> NameMap;
173 typedef NameMap::iterator NameMapIt;
178 static void registerXMLName(
const MBXMLUtils::FQN &name, AllocateFkt alloc, DeallocateFkt dealloc);
184 static ObjectFactory& instance();
187 NameMap registeredType;
190 template<
class CreateType>
192 return new CreateType;
201 template<
class CreateType>
203 return CreateType::getInstance();
216 template<
class CreateType>
223 ObjectFactory::template registerXMLName<CreateType>(name);
228 ObjectFactory::template deregisterXMLName<CreateType>(name);
232 MBXMLUtils::FQN name;
239 template<
class CreateType>
246 ObjectFactory::template registerXMLNameAsSingleton<CreateType>(name);
251 ObjectFactory::template deregisterXMLNameAsSingleton<CreateType>(name);
255 MBXMLUtils::FQN name;
261 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
262 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
263 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
268 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAME(ThisType, name) \
269 static MBSim::ObjectFactoryRegisterXMLNameHelper<ThisType> \
270 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
275 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAMEASSINGLETON(ThisType, name) \
276 static MBSim::ObjectFactoryRegisterXMLNameHelperAsSingleton<ThisType> \
277 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(name);
281 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAME_AND_INSTANTIATE(ThisType, name) \
282 template class ThisType; \
283 static MBSim::ObjectFactoryRegisterXMLNameHelper<ThisType> \
284 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
288 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAMEASSINGLETON_AND_INSTANTIATE(ThisType, name) \
289 template class ThisType; \
290 static MBSim::ObjectFactoryRegisterXMLNameHelperAsSingleton<ThisType> \
291 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(name);
static void deregisterXMLNameAsSingleton(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:100
~ObjectFactoryRegisterXMLNameHelper()
Definition: objectfactory.h:227
Definition: objectfactory.h:217
Definition: objectfactory.h:41
static void registerXMLNameAsSingleton(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:84
ObjectFactoryRegisterXMLNameHelper(const MBXMLUtils::FQN &name_)
Definition: objectfactory.h:222
~ObjectFactoryRegisterXMLNameHelperAsSingleton()
Definition: objectfactory.h:250
static void registerXMLName(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:76
ObjectFactoryRegisterXMLNameHelperAsSingleton(const MBXMLUtils::FQN &name_)
Definition: objectfactory.h:245
Definition: objectfactory.h:68
Definition: objectfactory.h:57
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:108
static void deregisterXMLName(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:92
basic error class for mbsim
Definition: mbsim_event.h:38
Definition: objectfactory.h:240