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"
34 #ifdef HAVE_BOOST_CORE_DEMANGLE_HPP // not available for older boost versions
35 # include <boost/core/demangle.hpp>
39 inline std::string demangle(
const std::string &name) {
54 DOMEvalExceptionStack(
const xercesc::DOMElement *element) : MBXMLUtils::DOMEvalException(
"", element) {}
57 void add(
const std::string &type,
const boost::shared_ptr<MBXMLUtils::DOMEvalException> &ex);
58 const char* what()
const throw();
59 std::vector<std::pair<std::string, boost::shared_ptr<MBXMLUtils::DOMEvalException> > > &getExceptionVector();
61 std::vector<std::pair<std::string, boost::shared_ptr<MBXMLUtils::DOMEvalException> > > exVec;
62 mutable std::string whatStr;
63 void generateWhat(std::stringstream &str,
const std::string &indent)
const;
71 MBXMLUtils::DOMEvalException(type, element) {}
86 template<
class CreateType>
94 template<
class CreateType>
102 template<
class CreateType>
110 template<
class CreateType>
118 template<
class ContainerType>
120 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
123 BOOST_STATIC_ASSERT_MSG((boost::is_convertible<ContainerType*, fmatvec::Atom*>::value),
124 "In MBSim::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from fmatvec::Atom.");
128 throw MBSimError(
"Internal error: NULL argument specified.");
130 MBXMLUtils::FQN fqn=MBXMLUtils::E(element)->getTagName();
131 NameMapIt nameIt=instance().registeredType.find(fqn);
132 if(nameIt==instance().registeredType.end())
133 throw MBXMLUtils::DOMEvalException(
"Internal error: No objects of name {"+fqn.first+
"}"+fqn.second+
" registred", element);
136 for(AllocDeallocVectorIt allocDeallocIt=nameIt->second.begin(); allocDeallocIt!=nameIt->second.end(); ++allocDeallocIt) {
140 ContainerType *ret=
dynamic_cast<ContainerType*
>(ele);
143 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
144 boost::make_shared<DOMEvalExceptionWrongType>(
145 boost::core::demangle(
typeid(ContainerType).name()), element));
146 allocDeallocIt->second(ele);
150 ret->initializeUsingXML(const_cast<xercesc::DOMElement*>(element));
154 allErrors.add(boost::core::demangle(
typeid(*ele).name()), boost::make_shared<DOMEvalExceptionStack>(ex));
156 catch(MBXMLUtils::DOMEvalException &ex) {
157 allErrors.add(boost::core::demangle(
typeid(*ele).name()), boost::make_shared<MBXMLUtils::DOMEvalException>(ex));
159 catch(std::exception &ex) {
160 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
161 boost::make_shared<MBXMLUtils::DOMEvalException>(ex.what(), element));
164 allErrors.add(boost::core::demangle(
typeid(*ele).name()),
165 boost::make_shared<MBXMLUtils::DOMEvalException>(
"Unknwon exception", element));
167 allocDeallocIt->second(ele);
181 typedef std::vector<std::pair<AllocateFkt, DeallocateFkt> > AllocDeallocVector;
182 typedef AllocDeallocVector::iterator AllocDeallocVectorIt;
183 typedef std::map<MBXMLUtils::FQN, AllocDeallocVector> NameMap;
184 typedef NameMap::iterator NameMapIt;
189 static void registerXMLName(
const MBXMLUtils::FQN &name, AllocateFkt alloc, DeallocateFkt dealloc);
195 static ObjectFactory& instance();
198 NameMap registeredType;
201 template<
class CreateType>
203 return new CreateType;
212 template<
class CreateType>
214 return CreateType::getInstance();
227 template<
class CreateType>
234 ObjectFactory::template registerXMLName<CreateType>(name);
239 ObjectFactory::template deregisterXMLName<CreateType>(name);
243 MBXMLUtils::FQN name;
250 template<
class CreateType>
257 ObjectFactory::template registerXMLNameAsSingleton<CreateType>(name);
262 ObjectFactory::template deregisterXMLNameAsSingleton<CreateType>(name);
266 MBXMLUtils::FQN name;
272 #define MBSIM_OBJECTFACTORY_CONCAT1(X, Y) X##Y
273 #define MBSIM_OBJECTFACTORY_CONCAT(X, Y) MBSIM_OBJECTFACTORY_CONCAT1(X, Y)
274 #define MBSIM_OBJECTFACTORY_APPENDLINE(X) MBSIM_OBJECTFACTORY_CONCAT(X, __LINE__)
279 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAME(ThisType, name) \
280 static MBSim::ObjectFactoryRegisterXMLNameHelper<ThisType> \
281 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
286 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAMEASSINGLETON(ThisType, name) \
287 static MBSim::ObjectFactoryRegisterXMLNameHelperAsSingleton<ThisType> \
288 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(name);
292 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAME_AND_INSTANTIATE(ThisType, name) \
293 template class ThisType; \
294 static MBSim::ObjectFactoryRegisterXMLNameHelper<ThisType> \
295 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
299 #define MBSIM_OBJECTFACTORY_REGISTERXMLNAMEASSINGLETON_AND_INSTANTIATE(ThisType, name) \
300 template class ThisType; \
301 static MBSim::ObjectFactoryRegisterXMLNameHelperAsSingleton<ThisType> \
302 MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariableAsSingleTon)(name);
static void deregisterXMLNameAsSingleton(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:111
~ObjectFactoryRegisterXMLNameHelper()
Definition: objectfactory.h:238
Definition: objectfactory.h:228
Definition: objectfactory.h:52
static void registerXMLNameAsSingleton(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:95
ObjectFactoryRegisterXMLNameHelper(const MBXMLUtils::FQN &name_)
Definition: objectfactory.h:233
~ObjectFactoryRegisterXMLNameHelperAsSingleton()
Definition: objectfactory.h:261
static void registerXMLName(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:87
ObjectFactoryRegisterXMLNameHelperAsSingleton(const MBXMLUtils::FQN &name_)
Definition: objectfactory.h:256
Definition: objectfactory.h:79
Definition: objectfactory.h:68
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:119
static void deregisterXMLName(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:103
basic error class for mbsim
Definition: mbsim_event.h:38
Definition: objectfactory.h:251