ug4
Loading...
Searching...
No Matches
factory.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016: G-CSC, Goethe University Frankfurt
3 * Author: Sebastian Reiter
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__UG_factory
34#define __H__UG_factory
35
36#include <type_traits> // for std::is_base_of
37#include <boost/mpl/for_each.hpp>
38#include <boost/mpl/vector.hpp>
39#include "../boost_serialization.h"
41
42namespace ug{
43
44namespace detail{
45namespace factory{
47 template <class TBase, class TDerived>
49 {
50 return new TDerived;
51 }
52}
53}
54
55
57
72template <class TBase, class TPairSeq = boost::mpl::vector<> >
74{
75public:
77 {
79 boost::mpl::for_each<TPairSeq>(func);
80 }
81
82 SmartPtr<TBase> create(const std::string& className)
83 {
84 return create(className.c_str());
85 }
86
87 SmartPtr<TBase> create(const char* className)
88 {
89 return SmartPtr<TBase>(create_raw(className));
90 }
91
92 TBase* create_raw(const std::string& className)
93 {
94 return create_raw(className.c_str());
95 }
96
97 TBase* create_raw(const char* className)
98 {
99 typename class_map_t::iterator iclass = m_classMap.find(std::string(className));
100 UG_COND_THROW(iclass == m_classMap.end(),
101 "Unregistered class-name used in 'Factory::create_raw': " << className);
102 return iclass->second.factory();
103 }
104
105 template <class TDerived>
106 void register_class(const char* name)
107 {
108 // UG_LOG("registering derived class: " << className << std::endl);
109 static_assert((std::is_base_of<TBase, TDerived>::value), "TDerived is not a subclass of TBase");
110
111 std::string className(name);
112
113 m_classMap[className] = ClassInfo(
114 name,
115 &detail::factory::DerivedClassFactory<TBase, TDerived>);
116
117 std::string typeName(typeid(TDerived).name());
118 m_classNameMap[typeName] = className;
119
120 m_classNames.push_back(className);
121 }
122
123 size_t num_classes() const {return m_classNames.size();}
124 const std::string& class_name(const size_t i) const {return m_classNames.at(i);}
125 const std::string& class_name(const TBase& cls) const
126 {
127 std::string typeName = typeid(cls).name();
128 class_name_map_t::const_iterator i = m_classNameMap.find(typeName);
129 if(i != m_classNameMap.end())
130 return i->second;
131 static const std::string defaultName ("");
132 return defaultName;
133 }
134
135private:
136 typedef TBase* (*factory_sig)();
137
138 struct ClassInfo {
140 ClassInfo (const char* _name,
141 factory_sig _factory) :
142 name(_name),
143 factory(_factory)
144 {}
145
146 const char* name;
148 };
149
150 typedef std::map<std::string, ClassInfo> class_map_t;
151 typedef std::map<std::string, std::string> class_name_map_t;
154 std::vector<std::string> m_classNames;
155};
156
157}// end of namespace
158
159#endif //__H__UG_factory
location name
Definition checkpoint_util.lua:128
Definition smart_pointer.h:107
A factory class which creates instances given a class-name.
Definition factory.h:74
std::map< std::string, ClassInfo > class_map_t
Definition factory.h:150
class_name_map_t m_classNameMap
key: type-name, value: class-names
Definition factory.h:153
std::vector< std::string > m_classNames
Definition factory.h:154
TBase * create_raw(const std::string &className)
Definition factory.h:92
std::map< std::string, std::string > class_name_map_t
Definition factory.h:151
class_map_t m_classMap
contains ClassInfo objects accessible by class-name.
Definition factory.h:152
void register_class(const char *name)
Definition factory.h:106
TBase *(* factory_sig)()
Definition factory.h:136
TBase * create_raw(const char *className)
Definition factory.h:97
const std::string & class_name(const size_t i) const
Definition factory.h:124
const std::string & class_name(const TBase &cls) const
Definition factory.h:125
size_t num_classes() const
Definition factory.h:123
SmartPtr< TBase > create(const char *className)
Definition factory.h:87
SmartPtr< TBase > create(const std::string &className)
Definition factory.h:82
Factory()
Definition factory.h:76
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition error.h:61
TBase * DerivedClassFactory()
used internally to construct derived classes of a given type
Definition factory.h:48
the ug namespace
function func(x, y, z, t, si)
Definition factory.h:138
factory_sig factory
Definition factory.h:147
const char * name
Definition factory.h:146
ClassInfo()
Definition factory.h:139
ClassInfo(const char *_name, factory_sig _factory)
Definition factory.h:140
Definition register_type_pair_functor.h:43