20 #ifndef _MBSIM_OBJECTFACTORY_H_
21 #define _MBSIM_OBJECTFACTORY_H_
23 #include "objectfactory_part.h"
27 #include "mbsim/utils/utils.h"
28 #include <mbsim/mbsim_event.h>
29 #include <mbxmlutilshelper/utils.h>
30 #include "fmatvec/atom.h"
31 #ifdef HAVE_BOOST_CORE_DEMANGLE_HPP // not available for older boost versions
32 # include <boost/core/demangle.hpp>
36 inline std::string demangle(
const std::string &name) {
54 void add(
const std::string &type,
const std::shared_ptr<MBXMLUtils::DOMEvalException> &ex);
55 const char* what()
const throw();
56 std::vector<std::pair<std::string, std::shared_ptr<MBXMLUtils::DOMEvalException> > > &getExceptionVector();
58 std::vector<std::pair<std::string, std::shared_ptr<MBXMLUtils::DOMEvalException> > > exVec;
59 mutable std::string whatStr;
60 void generateWhat(std::stringstream &str,
const std::string &indent)
const;
86 template<
class ContainerType>
87 static ContainerType*
createAndInit(
const xercesc::DOMElement *element) {
89 static_assert(std::is_convertible<ContainerType*, fmatvec::Atom*>::value,
90 "In MBSim::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from fmatvec::Atom.");
93 throw MBSimError(
"Internal error: NULL argument specified.");
96 NameMapIt nameIt=instance().registeredType.find(fqn);
97 if(nameIt==instance().registeredType.end())
101 for(AllocDeallocVectorIt allocDeallocIt=nameIt->second.begin(); allocDeallocIt!=nameIt->second.end(); ++allocDeallocIt) {
105 ContainerType *ret=
dynamic_cast<ContainerType*
>(ele);
108 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
109 std::make_shared<DOMEvalExceptionWrongType>(
110 boost::core::demangle(
typeid(ContainerType).name()), element));
111 (*allocDeallocIt->second)(ele);
115 ret->initializeUsingXML(const_cast<xercesc::DOMElement*>(element));
119 allErrors.add(boost::core::demangle(
typeid(*ele).name()), std::make_shared<DOMEvalExceptionStack>(ex));
122 allErrors.add(boost::core::demangle(
typeid(*ele).name()), std::make_shared<MBXMLUtils::DOMEvalException>(ex));
124 catch(std::exception &ex) {
125 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
126 std::make_shared<MBXMLUtils::DOMEvalException>(ex.what(), element));
129 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
130 std::make_shared<MBXMLUtils::DOMEvalException>(
"Unknwon exception", element));
132 (*allocDeallocIt->second)(ele);
141 typedef std::pair<const AllocateBase*, const DeallocateBase*> AllocDeallocPair;
142 typedef std::vector<AllocDeallocPair> AllocDeallocVector;
143 typedef AllocDeallocVector::iterator AllocDeallocVectorIt;
144 typedef std::map<MBXMLUtils::FQN, AllocDeallocVector> NameMap;
145 typedef NameMap::iterator NameMapIt;
152 static ObjectFactory& instance();
155 NameMap registeredType;
159 template<
class CreateType>
163 return new CreateType;
169 return typeid(*this)==
typeid(other);
182 template<
class CreateType>
186 return CreateType::getInstance();
192 return typeid(*this)==
typeid(other);
207 template<
class CreateType>
230 template<
class CreateType>
251 std::string fixXMLLocalName(std::string name);
255 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
256 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
257 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
262 #define MBSIM_OBJECTFACTORY_REGISTERCLASS(NS, Class) \
263 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
264 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(NS%MBSim::fixXMLLocalName(#Class));
269 #define MBSIM_OBJECTFACTORY_REGISTERCLASSASSINGLETON(NS, Class) \
270 static MBSim::ObjectFactoryRegisterClassHelperAsSingleton<Class> \
271 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(NS%MBSim::fixXMLLocalName(#Class));
275 #define MBSIM_OBJECTFACTORY_REGISTERCLASS_AND_INSTANTIATE(NS, Class) \
276 template class Class; \
277 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
278 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(NS%MBSim::fixXMLLocalName(#Class));
282 #define MBSIM_OBJECTFACTORY_REGISTERCLASSASSINGLETON_AND_INSTANTIATE(NS, Class) \
283 template class Class; \
284 static MBSim::ObjectFactoryRegisterClassHelperAsSingleton<Class> \
285 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(NS%MBSim::fixXMLLocalName(#Class));
Definition: objectfactory.h:49
Definition: objectfactory.h:208
~ObjectFactoryRegisterClassHelper()
Definition: objectfactory.h:218
void operator()(fmatvec::Atom *obj) const override
Implement this function to deallocate the object obj.
Definition: objectfactory.h:199
Definition: objectfactory_part.h:48
Definition: objectfactory.h:183
Definition: objectfactory.h:160
ObjectFactoryRegisterClassHelper(const MBXMLUtils::FQN &name_)
Definition: objectfactory.h:213
Definition: objectfactory.h:197
~ObjectFactoryRegisterClassHelperAsSingleton()
Definition: objectfactory.h:241
bool operator==(const AllocateBase &other) const override
Definition: objectfactory.h:191
fmatvec::Atom * operator()() const override
Implement this function to allocate a new object.
Definition: objectfactory.h:185
Definition: objectfactory.h:174
Definition: objectfactory.h:76
Definition: objectfactory_part.h:37
Definition: objectfactory.h:65
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:87
fmatvec::Atom * operator()() const override
Implement this function to allocate a new object.
Definition: objectfactory.h:162
basic error class for mbsim
Definition: mbsim_event.h:38
ObjectFactoryRegisterClassHelperAsSingleton(const MBXMLUtils::FQN &name_)
Definition: objectfactory.h:236
Definition: objectfactory.h:231
void operator()(fmatvec::Atom *obj) const override
Implement this function to deallocate the object obj.
Definition: objectfactory.h:176
bool operator==(const AllocateBase &other) const override
Definition: objectfactory.h:168