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/core/demangle.hpp>
45 const char* what()
const noexcept
override;
46 std::vector<std::pair<std::string, std::shared_ptr<MBXMLUtils::DOMEvalException>>> &getExceptionVector();
48 std::vector<std::pair<std::string, std::shared_ptr<MBXMLUtils::DOMEvalException>>> exVec;
49 mutable std::string whatStr;
50 void generateWhat(std::stringstream &str,
const std::string &indent)
const;
75 template<
class ContainerType>
76 static ContainerType*
createAndInit(
const xercesc::DOMElement *element) {
78 static_assert(std::is_convertible<ContainerType*, fmatvec::Atom*>::value,
79 "In MBSim::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from fmatvec::Atom.");
82 throw std::runtime_error(
"Internal error: NULL argument specified.");
85 auto nameIt=instance().registeredType.find(fqn);
86 if(nameIt==instance().registeredType.end())
90 for(
auto & allocDeallocIt : nameIt->second) {
94 auto *ret=
dynamic_cast<ContainerType*
>(ele);
97 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
98 std::make_shared<DOMEvalExceptionWrongType>(
99 boost::core::demangle(
typeid(ContainerType).name()), element));
100 (*allocDeallocIt.second)(ele);
104 ret->initializeUsingXML(const_cast<xercesc::DOMElement*>(element));
108 allErrors.add(boost::core::demangle(
typeid(*ele).name()), std::make_shared<DOMEvalExceptionStack>(ex));
111 allErrors.add(boost::core::demangle(
typeid(*ele).name()), std::make_shared<MBXMLUtils::DOMEvalException>(ex));
113 catch(std::exception &ex) {
114 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
115 std::make_shared<MBXMLUtils::DOMEvalException>(ex.what(), element));
118 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
119 std::make_shared<MBXMLUtils::DOMEvalException>(
"Unknwon exception", element));
121 (*allocDeallocIt.second)(ele);
130 typedef std::pair<const AllocateBase*, const DeallocateBase*> AllocDeallocPair;
131 typedef std::vector<AllocDeallocPair> AllocDeallocVector;
132 typedef AllocDeallocVector::iterator AllocDeallocVectorIt;
133 typedef std::map<MBXMLUtils::FQN, AllocDeallocVector> NameMap;
134 typedef NameMap::iterator NameMapIt;
143 NameMap registeredType;
147 template<
class CreateType>
151 return new CreateType;
157 return typeid(*this)==
typeid(other);
172 template<
class CreateType>
195 template<
class EnumType>
198 template<
class EV>
friend void registerEnum_internal(
const MBXMLUtils::FQN &name,
const EV& value);
199 template<
class EV>
friend void deregisterEnum_internal(
const MBXMLUtils::FQN &name);
205 registerEnum_internal<EnumType>(fqn, enumVar);
210 deregisterEnum_internal<EnumType>(fqn);
214 static const EnumType&
get(
const MBXMLUtils::FQN &fqn,
const xercesc::DOMElement *e=
nullptr) {
215 auto it=regmap().find(fqn);
216 if(it==regmap().end())
218 return it->second.get();
226 static std::map<MBXMLUtils::FQN, std::reference_wrapper<const EnumType>>& regmap();
230 void registerEnum_internal(
const MBXMLUtils::FQN &name,
const EV& value) {
231 EnumFactory<EV>::regmap().insert(std::make_pair(name, std::ref(value)));
236 EnumFactory<EV>::regmap().erase(name);
240 std::string fixXMLLocalName(std::string name);
244 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
245 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
246 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
251 #define MBSIM_OBJECTFACTORY_REGISTERCLASS(NS, Class) \
252 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
253 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
257 #define MBSIM_OBJECTFACTORY_REGISTERCLASS_AND_INSTANTIATE(NS, Class) \
258 template class Class; \
259 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
260 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
264 #define MBSIM_OBJECTFACTORY_REGISTERCLASSWITHTEMPLATENAME_AND_INSTANTIATE(NS, Class, TemplateName) \
265 template class Class; \
266 static MBSim::ObjectFactoryRegisterClassHelper<Class> \
267 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%(MBSim::fixXMLLocalName(#Class)+"_"+#TemplateName));
270 #define MBSIM_OBJECTFACTORY_REGISTERENUM(EnumType, NS, enumName) \
271 static MBSim::EnumFactory<EnumType> \
272 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(enumName, (NS)%#enumName);
EnumFactory(const EnumType &enumVar, MBXMLUtils::FQN fqn_)
Definition: objectfactory.h:204
Definition: objectfactory.h:39
Definition: objectfactory.h:173
~ObjectFactoryRegisterClassHelper()
Definition: objectfactory.h:183
Definition: objectfactory_part.h:49
Definition: objectfactory.h:148
ObjectFactoryRegisterClassHelper(MBXMLUtils::FQN name_)
Definition: objectfactory.h:178
~EnumFactory()
Definition: objectfactory.h:209
Definition: objectfactory.h:162
Definition: objectfactory.h:66
Definition: objectfactory_part.h:37
Definition: objectfactory.h:55
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:76
fmatvec::Atom * operator()() const override
Implement this function to allocate a new object.
Definition: objectfactory.h:150
Definition: objectfactory.h:196
void operator()(fmatvec::Atom *obj) const override
Implement this function to deallocate the object obj.
Definition: objectfactory.h:164
bool isEqual(const AllocateBase &other) const override
Definition: objectfactory.h:156