openmbvcppinterface  3.1.0
OpenMBV C++ Interface
objectfactory.h
1 /* Copyright (C) 2004-2010 OpenMBV 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 _OPENMBV_OBJECTFACTORY_H_
21 #define _OPENMBV_OBJECTFACTORY_H_
22 
23 #include <map>
24 #include <stdexcept>
25 #include <typeinfo>
26 #include <mbxmlutilshelper/dom.h>
27 #include <xercesc/dom/DOMElement.hpp>
28 
29 namespace OpenMBV {
30 
31 class Object;
32 
37 
38  public:
39 
43  template<class CreateType>
44  static void registerXMLName(const MBXMLUtils::FQN &name) {
45  registerXMLName(name, &allocate<CreateType>);
46  }
47 
51  template<class ContainerType>
52  static std::shared_ptr<ContainerType> create(const xercesc::DOMElement *element) {
53  // just check if ContainerType is derived from Object if not throw a compile error
54  static_assert(std::is_convertible<ContainerType*, Object*>::value,
55  "In OpenMBV::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from Object.");
56  // return NULL if no input is supplied
57  if(element==nullptr) return std::shared_ptr<ContainerType>();
58  // loop over all all registred types corresponding to element->ValueStr()
59  std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(MBXMLUtils::E(element)->getTagName());
60  for(auto it=range.first; it!=range.second; it++) {
61  // allocate a new object using the allocate function pointer
62  std::shared_ptr<Object> ele=it->second();
63  // try to cast ele up to ContainerType
64  std::shared_ptr<ContainerType> ret=std::dynamic_pointer_cast<ContainerType>(ele);
65  // if possible, return it
66  if(ret)
67  return ret;
68  }
69  // no matching element found: throw error
70  throw std::runtime_error("No class named "+MBXMLUtils::X()%element->getTagName()+" found which is of type "+
71  typeid(ContainerType).name()+".");
72  }
73 
75  template<class CreateType>
76  static std::shared_ptr<CreateType> create() {
77  return std::shared_ptr<CreateType>(new CreateType, &deleter<CreateType>);
78  }
79 
81  template<class CreateType>
82  static std::shared_ptr<CreateType> create(const std::shared_ptr<CreateType> &t) {
83  return std::shared_ptr<CreateType>(new CreateType(*t.get()), &deleter<CreateType>);
84  }
85 
86  static void addErrorMsg(const std::string &msg);
87  static std::string getAndClearErrorMsg();
88 
89  private:
90 
91  // a pointer to a function allocating an object
92  using allocateFkt = std::shared_ptr<Object> (*)();
93 
94  // convinence typedefs
95  using Map = std::multimap<MBXMLUtils::FQN, allocateFkt>;
96  using MapIt = typename Map::iterator;
97 
98  // private ctor
99  ObjectFactory() = default;
100 
101  static void registerXMLName(const MBXMLUtils::FQN &name, allocateFkt alloc);
102 
103  // create an singleton instance of the object factory.
104  // only declaration here and defition and explicit instantation for all Object in objectfactory.cc (required for Windows)
105  static ObjectFactory& instance();
106 
107  // a multimap of all registered types
108  Map registeredType;
109 
110  // a wrapper to allocate an object of type CreateType: used by create(xercesc::DOMElement *)
111  template<class CreateType>
112  static std::shared_ptr<Object> allocate() {
113  return std::shared_ptr<CreateType>(new CreateType, &deleter<CreateType>);
114  }
115 
116  // a wrapper to deallocate an object of type T: all dtors are protected but ObjectFactory is a friend of all classes
117  template<class T>
118  static void deleter(T *t) { delete t; }
119 
120  static std::string errorMsg;
121 };
122 
126 template<class CreateType>
128 
129  public:
130 
133  try {
134  ObjectFactory::registerXMLName<CreateType>(name);
135  }
136  catch(std::exception &ex) {
137  ObjectFactory::addErrorMsg(ex.what());
138  }
139  catch(...) {
140  ObjectFactory::addErrorMsg("Unknown error");
141  }
142  };
143 
144 };
145 
146 }
147 
150 #define OPENMBV_OBJECTFACTORY_REGISTERXMLNAME(ThisType, name) \
151  static OpenMBV::ObjectFactoryRegisterXMLNameHelper<ThisType> BOOST_PP_CAT(objectFactoryRegistrationDummyVariable_, __LINE__)(name);
152 
153 
154 #endif
ObjectFactoryRegisterXMLNameHelper(const MBXMLUtils::FQN &name) noexcept
Definition: objectfactory.h:132
static std::shared_ptr< ContainerType > create(const xercesc::DOMElement *element)
Definition: objectfactory.h:52
Definition: objectfactory.h:36
static void registerXMLName(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:44
static std::shared_ptr< CreateType > create(const std::shared_ptr< CreateType > &t)
Definition: objectfactory.h:82
Definition: objectfactory.h:127
static std::shared_ptr< CreateType > create()
Definition: objectfactory.h:76