20 #ifndef _OPENMBV_OBJECTFACTORY_H_
21 #define _OPENMBV_OBJECTFACTORY_H_
26 #include <mbxmlutilshelper/dom.h>
27 #include <xercesc/dom/DOMElement.hpp>
28 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
29 # include <boost/static_assert.hpp>
30 # include <boost/type_traits.hpp>
47 template<
class CreateType>
55 template<
class ContainerType>
56 static boost::shared_ptr<ContainerType>
create(
const xercesc::DOMElement *element) {
57 #ifdef HAVE_BOOST_TYPE_TRAITS_HPP
60 BOOST_STATIC_ASSERT_MSG((boost::is_convertible<ContainerType*, Object*>::value),
61 "In OpenMBV::ObjectFactory::create<ContainerType>(...) ContainerType must be derived from Object.");
64 if(element==NULL)
return boost::shared_ptr<ContainerType>();
66 std::pair<MapIt, MapIt> range=instance().registeredType.equal_range(MBXMLUtils::E(element)->getTagName());
67 for(MapIt it=range.first; it!=range.second; it++) {
69 boost::shared_ptr<Object> ele=it->second();
71 boost::shared_ptr<ContainerType> ret=boost::dynamic_pointer_cast<ContainerType>(ele);
77 throw std::runtime_error(
"No class named "+MBXMLUtils::X()%element->getTagName()+
" found which is of type "+
78 typeid(ContainerType).name()+
".");
82 template<
class CreateType>
83 static boost::shared_ptr<CreateType>
create() {
84 return boost::shared_ptr<CreateType>(
new CreateType, &deleter<CreateType>);
88 template<
class CreateType>
89 static boost::shared_ptr<CreateType>
create(
const boost::shared_ptr<CreateType> &t) {
90 return boost::shared_ptr<CreateType>(
new CreateType(*t.get()), &deleter<CreateType>);
96 typedef boost::shared_ptr<Object> (*allocateFkt)();
99 typedef std::multimap<MBXMLUtils::FQN, allocateFkt> Map;
100 typedef typename Map::iterator MapIt;
105 static void registerXMLName(
const MBXMLUtils::FQN &name, allocateFkt alloc);
109 static ObjectFactory& instance();
115 template<
class CreateType>
116 static boost::shared_ptr<Object> allocate() {
117 return boost::shared_ptr<CreateType>(
new CreateType, &deleter<CreateType>);
122 static void deleter(T *t) {
delete t; }
129 template<
class CreateType>
136 ObjectFactory::registerXMLName<CreateType>(name);
143 #define OPENMBV_OBJECTFACTORY_CONCAT1(X, Y) X##Y
144 #define OPENMBV_OBJECTFACTORY_CONCAT(X, Y) OPENMBV_OBJECTFACTORY_CONCAT1(X, Y)
145 #define OPENMBV_OBJECTFACTORY_APPENDLINE(X) OPENMBV_OBJECTFACTORY_CONCAT(X, __LINE__)
149 #define OPENMBV_OBJECTFACTORY_REGISTERXMLNAME(ThisType, name) \
150 static OpenMBV::ObjectFactoryRegisterXMLNameHelper<ThisType> \
151 OPENMBV_OBJECTFACTORY_APPENDLINE(objectFactoryRegistrationDummyVariable)(name);
Definition: objectfactory.h:40
static void registerXMLName(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:48
ObjectFactoryRegisterXMLNameHelper(const MBXMLUtils::FQN &name)
Definition: objectfactory.h:135
Definition: objectfactory.h:130
static boost::shared_ptr< ContainerType > create(const xercesc::DOMElement *element)
Definition: objectfactory.h:56
static boost::shared_ptr< CreateType > create(const boost::shared_ptr< CreateType > &t)
Definition: objectfactory.h:89
static boost::shared_ptr< CreateType > create()
Definition: objectfactory.h:83