20 #ifndef _MBSIM_OBJECTFACTORY_H_
21 #define _MBSIM_OBJECTFACTORY_H_
23 #include "objectfactory_part.h"
29 #include <mbsim/mbsim_event.h>
30 #include <xercesc/dom/DOMAttr.hpp>
31 #include <mbxmlutilshelper/utils.h>
32 #include "fmatvec/atom.h"
33 #include <boost/version.hpp>
34 #if BOOST_VERSION >= 105600
35 #include <boost/core/demangle.hpp>
38 #ifndef BOOST_CORE_DEMANGLE_REPLACEMENT
39 #define BOOST_CORE_DEMANGLE_REPLACEMENT
42 inline std::string demangle(
const std::string &name) {
44 char* retc=abi::__cxa_demangle(name.c_str(),
nullptr,
nullptr, &status);
45 if(status!=0)
throw std::runtime_error(
"Cannot demangle c++ symbol.");
46 std::string ret(retc);
64 void add(
const std::string &type,
const std::shared_ptr<MBXMLUtils::DOMEvalException> &ex);
65 const char* what()
const noexcept
override;
66 std::vector<std::pair<std::string, std::shared_ptr<MBXMLUtils::DOMEvalException> > > &getExceptionVector();
68 std::vector<std::pair<std::string, std::shared_ptr<MBXMLUtils::DOMEvalException> > > exVec;
69 mutable std::string whatStr;
70 void generateWhat(std::stringstream &str,
const std::string &indent)
const;
96 template<
class ContainerType>
97 static ContainerType*
createAndInit(
const xercesc::DOMElement *element) {
99 static_assert(std::is_convertible<ContainerType*, fmatvec::Atom*>::value,
100 "In MBSim::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from fmatvec::Atom.");
103 throw std::runtime_error(
"Internal error: NULL argument specified.");
106 auto nameIt=instance().registeredType.find(fqn);
107 if(nameIt==instance().registeredType.end())
111 for(
auto & allocDeallocIt : nameIt->second) {
115 auto *ret=
dynamic_cast<ContainerType*
>(ele);
118 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
119 std::make_shared<DOMEvalExceptionWrongType>(
120 boost::core::demangle(
typeid(ContainerType).name()), element));
121 (*allocDeallocIt.second)(ele);
125 ret->initializeUsingXML(const_cast<xercesc::DOMElement*>(element));
129 allErrors.add(boost::core::demangle(
typeid(*ele).name()), std::make_shared<DOMEvalExceptionStack>(ex));
132 allErrors.add(boost::core::demangle(
typeid(*ele).name()), std::make_shared<MBXMLUtils::DOMEvalException>(ex));
134 catch(std::exception &ex) {
135 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
136 std::make_shared<MBXMLUtils::DOMEvalException>(ex.what(), element));
139 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
140 std::make_shared<MBXMLUtils::DOMEvalException>(
"Unknwon exception", element));
142 (*allocDeallocIt.second)(ele);
151 typedef std::pair<const AllocateBase*, const DeallocateBase*> AllocDeallocPair;
152 typedef std::vector<AllocDeallocPair> AllocDeallocVector;
153 typedef AllocDeallocVector::iterator AllocDeallocVectorIt;
154 typedef std::map<MBXMLUtils::FQN, AllocDeallocVector> NameMap;
155 typedef NameMap::iterator NameMapIt;
164 NameMap registeredType;
168 template<
class CreateType>
172 return new CreateType;
178 return typeid(*this)==
typeid(other);
191 template<
class CreateType>
195 return CreateType::getInstance();
201 return typeid(*this)==
typeid(other);
216 template<
class CreateType>
239 template<
class CreateType>
262 template<
class EnumType>
265 template<
class EV>
friend void registerEnum_internal(
const MBXMLUtils::FQN &name,
const EV& value);
266 template<
class EV>
friend void deregisterEnum_internal(
const MBXMLUtils::FQN &name);
272 registerEnum_internal<EnumType>(fqn, enumVar);
277 deregisterEnum_internal<EnumType>(fqn);
281 static const EnumType&
get(
const MBXMLUtils::FQN &fqn,
const xercesc::DOMElement *e=
nullptr) {
282 auto it=regmap().find(fqn);
283 if(it==regmap().end())
285 return it->second.get();
293 static std::map<MBXMLUtils::FQN, std::reference_wrapper<const EnumType>>& regmap();
297 void registerEnum_internal(
const MBXMLUtils::FQN &name,
const EV& value) {
298 EnumFactory<EV>::regmap().insert(std::make_pair(name, std::ref(value)));
303 EnumFactory<EV>::regmap().erase(name);
307 std::string fixXMLLocalName(std::string name);
311 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
312 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
313 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
318 #define MBSIM_OBJECTFACTORY_REGISTERCLASS(NS, Class) \
319 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
320 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
325 #define MBSIM_OBJECTFACTORY_REGISTERCLASSASSINGLETON(NS, Class) \
326 static MBSim::ObjectFactoryRegisterClassHelperAsSingleton<Class> \
327 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
331 #define MBSIM_OBJECTFACTORY_REGISTERCLASS_AND_INSTANTIATE(NS, Class) \
332 template class Class; \
333 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
334 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
338 #define MBSIM_OBJECTFACTORY_REGISTERCLASSWITHTEMPLATENAME_AND_INSTANTIATE(NS, Class, TemplateName) \
339 template class Class; \
340 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
341 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%(MBSim::fixXMLLocalName(#Class)+"_"+#TemplateName));
345 #define MBSIM_OBJECTFACTORY_REGISTERCLASSASSINGLETON_AND_INSTANTIATE(NS, Class) \
346 template class Class; \
347 static MBSim::ObjectFactoryRegisterClassHelperAsSingleton<Class> \
348 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
351 #define MBSIM_OBJECTFACTORY_REGISTERENUM(EnumType, NS, enumName) \
352 static MBSim::EnumFactory<EnumType> \
353 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(enumName, (NS)%#enumName);
EnumFactory(const EnumType &enumVar, MBXMLUtils::FQN fqn_)
Definition: objectfactory.h:271
Definition: objectfactory.h:59
Definition: objectfactory.h:217
~ObjectFactoryRegisterClassHelper()
Definition: objectfactory.h:227
void operator()(fmatvec::Atom *obj) const override
Implement this function to deallocate the object obj.
Definition: objectfactory.h:208
Definition: objectfactory_part.h:48
Definition: objectfactory.h:192
Definition: objectfactory.h:169
ObjectFactoryRegisterClassHelper(MBXMLUtils::FQN name_)
Definition: objectfactory.h:222
Definition: objectfactory.h:206
~ObjectFactoryRegisterClassHelperAsSingleton()
Definition: objectfactory.h:250
bool operator==(const AllocateBase &other) const override
Definition: objectfactory.h:200
~EnumFactory()
Definition: objectfactory.h:276
fmatvec::Atom * operator()() const override
Implement this function to allocate a new object.
Definition: objectfactory.h:194
Definition: objectfactory.h:183
Definition: objectfactory.h:86
Definition: objectfactory_part.h:37
Definition: objectfactory.h:75
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:97
fmatvec::Atom * operator()() const override
Implement this function to allocate a new object.
Definition: objectfactory.h:171
Definition: objectfactory.h:263
ObjectFactoryRegisterClassHelperAsSingleton(MBXMLUtils::FQN name_)
Definition: objectfactory.h:245
Definition: objectfactory.h:240
void operator()(fmatvec::Atom *obj) const override
Implement this function to deallocate the object obj.
Definition: objectfactory.h:185
bool operator==(const AllocateBase &other) const override
Definition: objectfactory.h:177