All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
objectfactory.h
1 /* Copyright (C) 2004-2010 MBSim Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact: friedrich.at.gc@googlemail.com
18  */
19 
20 #ifndef _MBSIM_OBJECTFACTORY_H_
21 #define _MBSIM_OBJECTFACTORY_H_
22 
23 #include "objectfactory_part.h"
24 #include <utility>
25 #include <vector>
26 #include <functional>
27 #include <stdexcept>
28 #include <typeinfo>
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>
36 #else
37  #include <cxxabi.h>
38  #ifndef BOOST_CORE_DEMANGLE_REPLACEMENT
39  #define BOOST_CORE_DEMANGLE_REPLACEMENT
40  namespace boost {
41  namespace core {
42  inline std::string demangle(const std::string &name) {
43  int status;
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);
47  free(retc);
48  return ret;
49  }
50  }
51  }
52  #endif
53 #endif
54 
55 namespace MBSim {
56 
57 /* A tree of DOMEvalException's which store the tree of failed objects
58  * creations of the object factory */
60  public:
61  DOMEvalExceptionStack(const xercesc::DOMElement *element) : MBXMLUtils::DOMEvalException("", element) {}
62  DOMEvalExceptionStack(const DOMEvalExceptionStack &src) : MBXMLUtils::DOMEvalException(src), exVec(src.exVec) {}
63  ~DOMEvalExceptionStack() noexcept override = default;
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();
67  protected:
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;
71 };
72 
73 /* Just to distinguish a wrong type (not castable) error of the object factory
74  * from others */
76  public:
77  DOMEvalExceptionWrongType(const std::string &type, const xercesc::DOMElement *element) :
78  MBXMLUtils::DOMEvalException(type, element) {}
80  ~DOMEvalExceptionWrongType() noexcept override = default;
81 };
82 
87 
88  friend void registerClass_internal(const MBXMLUtils::FQN &name, const AllocateBase *alloc, const DeallocateBase *dealloc);
89  friend void deregisterClass_internal(const MBXMLUtils::FQN &name, const AllocateBase *alloc);
90 
91  public:
92 
96  template<class ContainerType>
97  static ContainerType* createAndInit(const xercesc::DOMElement *element) {
98  // just check if ContainerType is derived from fmatvec::Atom if not throw a compile error
99  static_assert(std::is_convertible<ContainerType*, fmatvec::Atom*>::value,
100  "In MBSim::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from fmatvec::Atom.");
101  // throw error if NULL is supplied as element
102  if(element==nullptr)
103  throw std::runtime_error("Internal error: NULL argument specified.");
104  // get all allocate functions for the given name
105  MBXMLUtils::FQN fqn=MBXMLUtils::E(element)->getTagName();
106  auto nameIt=instance().registeredType.find(fqn);
107  if(nameIt==instance().registeredType.end())
108  throw MBXMLUtils::DOMEvalException("Internal error: No objects of name {"+fqn.first+"}"+fqn.second+" registred", element);
109  DOMEvalExceptionStack allErrors(static_cast<xercesc::DOMElement*>(element->getParentNode()));
110  // try to create and init a object which each of the allocate function
111  for(auto & allocDeallocIt : nameIt->second) {
112  // create element
113  fmatvec::Atom *ele=(*allocDeallocIt.first)();
114  // try to cast the element to ContainerType
115  auto *ret=dynamic_cast<ContainerType*>(ele);
116  if(!ret) {
117  // cast not possible -> deallocate again and try next
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);
122  continue;
123  }
124  try {
125  ret->initializeUsingXML(const_cast<xercesc::DOMElement*>(element));
126  return ret;
127  }
128  catch(DOMEvalExceptionStack &ex) {
129  allErrors.add(boost::core::demangle(typeid(*ele).name()), std::make_shared<DOMEvalExceptionStack>(ex));
130  }
131  catch(MBXMLUtils::DOMEvalException &ex) {
132  allErrors.add(boost::core::demangle(typeid(*ele).name()), std::make_shared<MBXMLUtils::DOMEvalException>(ex));
133  }
134  catch(std::exception &ex) { // handles also MBSimError
135  allErrors.add(boost::core::demangle(typeid(*ele).name()),
136  std::make_shared<MBXMLUtils::DOMEvalException>(ex.what(), element));
137  }
138  catch(...) {
139  allErrors.add(boost::core::demangle(typeid(*ele).name()),
140  std::make_shared<MBXMLUtils::DOMEvalException>("Unknwon exception", element));
141  }
142  (*allocDeallocIt.second)(ele);
143  }
144  // if all failed -> return errors of all trys
145  throw allErrors;
146  }
147 
148  private:
149 
150  // convinence typedefs
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;
156 
157  // private ctor
158  ObjectFactory() = default;
159 
160  // create an singleton instance of the object factory.
161  static ObjectFactory& instance();
162 
163  // a vector of all registered types
164  NameMap registeredType;
165 };
166 
167 // a wrapper to allocate an object of type CreateType
168 template<class CreateType>
169 struct Allocate : public AllocateBase {
170  // create a new object of type CreateType using new
171  fmatvec::Atom* operator()() const override {
172  return new CreateType;
173  }
174  // check if this Allocator allocates the same object as other.
175  // This is the case if the type of this template class matches
176  // the type of other.
177  bool operator==(const AllocateBase& other) const override {
178  return typeid(*this)==typeid(other);
179  }
180 };
181 
182 // a wrapper to deallocate an object created by allocate
183 struct Deallocate : public DeallocateBase {
184  // deallocate a object using delete
185  void operator()(fmatvec::Atom *obj) const override {
186  delete obj;
187  }
188 };
189 
190 // a wrapper to get an singleton object of type CreateType (Must have the same signature as allocate()
191 template<class CreateType>
192 struct GetSingleton : public AllocateBase {
193  // create a new singelton object of type CreateType using CreateType::getInstance
194  fmatvec::Atom* operator()() const override {
195  return CreateType::getInstance();
196  }
197  // check if this Allocator returns the same object as other.
198  // This is the case if the type of this template class matches
199  // the type of other.
200  bool operator==(const AllocateBase& other) const override {
201  return typeid(*this)==typeid(other);
202  }
203 };
204 
205 // a wrapper to "deallocate" an singleton object (Must have the same signature as deallocate()
207  // deallocate a singleton object -> just do nothing
208  void operator()(fmatvec::Atom *obj) const override {
209  // just do nothing for singletons
210  }
211 };
212 
216 template<class CreateType>
218 
219  public:
220 
222  ObjectFactoryRegisterClassHelper(MBXMLUtils::FQN name_) : name(std::move(name_)) {
223  MBSim::registerClass_internal(name, new Allocate<CreateType>(), new Deallocate());
224  }
225 
228  MBSim::deregisterClass_internal(name, new Allocate<CreateType>());
229  }
230 
231  private:
232  MBXMLUtils::FQN name;
233 
234 };
235 
239 template<class CreateType>
241 
242  public:
243 
246  MBSim::registerClass_internal(name, new GetSingleton<CreateType>(), new DeallocateSingleton());
247  }
248 
251  MBSim::deregisterClass_internal(name, new GetSingleton<CreateType>());
252  }
253 
254  private:
255  MBXMLUtils::FQN name;
256 
257 };
258 
262 template<class EnumType>
263 class EnumFactory {
264 
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);
267 
268  public:
269 
271  EnumFactory(const EnumType &enumVar, MBXMLUtils::FQN fqn_) : fqn(std::move(fqn_)) {
272  registerEnum_internal<EnumType>(fqn, enumVar);
273  }
274 
277  deregisterEnum_internal<EnumType>(fqn);
278  }
279 
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())
284  throw MBXMLUtils::DOMEvalException("No enumeration value named {"+fqn.first+"}"+fqn.second+" registred", e);
285  return it->second.get();
286  }
287 
288  private:
289  MBXMLUtils::FQN fqn;
290 
291  // return the register map of the enum factory.
292  // (note: this function must not be defined in this file to avoid any imlicit instantation)
293  static std::map<MBXMLUtils::FQN, std::reference_wrapper<const EnumType>>& regmap();
294 };
295 
296 template<class EV>
297 void registerEnum_internal(const MBXMLUtils::FQN &name, const EV& value) {
298  EnumFactory<EV>::regmap().insert(std::make_pair(name, std::ref(value)));
299 }
300 
301 template<class EV>
302 void deregisterEnum_internal(const MBXMLUtils::FQN &name) {
303  EnumFactory<EV>::regmap().erase(name);
304 }
305 
306 // fix local xml name (remove template and namespace)
307 std::string fixXMLLocalName(std::string name);
308 
309 }
310 
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__)
314 
318 #define MBSIM_OBJECTFACTORY_REGISTERCLASS(NS, Class) \
319  static MBSim::ObjectFactoryRegisterClassHelper<Class> \
320  MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
321 
325 #define MBSIM_OBJECTFACTORY_REGISTERCLASSASSINGLETON(NS, Class) \
326  static MBSim::ObjectFactoryRegisterClassHelperAsSingleton<Class> \
327  MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)((NS)%MBSim::fixXMLLocalName(#Class));
328 
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));
335 
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));
342 
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));
349 
351 #define MBSIM_OBJECTFACTORY_REGISTERENUM(EnumType, NS, enumName) \
352  static MBSim::EnumFactory<EnumType> \
353  MBSIM_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(enumName, (NS)%#enumName);
354 
355 #endif
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
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

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home