ug4
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 <boost/static_assert.hpp>
37 #include <boost/mpl/for_each.hpp>
38 #include <boost/mpl/vector.hpp>
39 #include <boost/type_traits/is_base_of.hpp>
40 #include "../boost_serialization.h"
42 
43 namespace ug{
44 
45 namespace detail{
46 namespace factory{
48  template <class TBase, class TDerived>
50  {
51  return new TDerived;
52  }
53 }
54 }
55 
56 
58 
73 template <class TBase, class TPairSeq = boost::mpl::vector<> >
74 class Factory
75 {
76 public:
78  {
80  boost::mpl::for_each<TPairSeq>(func);
81  }
82 
83  SmartPtr<TBase> create(const std::string& className)
84  {
85  return create(className.c_str());
86  }
87 
88  SmartPtr<TBase> create(const char* className)
89  {
90  return SmartPtr<TBase>(create_raw(className));
91  }
92 
93  TBase* create_raw(const std::string& className)
94  {
95  return create_raw(className.c_str());
96  }
97 
98  TBase* create_raw(const char* className)
99  {
100  typename class_map_t::iterator iclass = m_classMap.find(std::string(className));
101  UG_COND_THROW(iclass == m_classMap.end(),
102  "Unregistered class-name used in 'Factory::create_raw': " << className);
103  return iclass->second.factory();
104  }
105 
106  template <class TDerived>
107  void register_class(const char* name)
108  {
109  // UG_LOG("registering derived class: " << className << std::endl);
110  BOOST_STATIC_ASSERT((boost::is_base_of<TBase, TDerived>::value));
111 
112  std::string className(name);
113 
114  m_classMap[className] = ClassInfo(
115  name,
116  &detail::factory::DerivedClassFactory<TBase, TDerived>);
117 
118  std::string typeName(typeid(TDerived).name());
119  m_classNameMap[typeName] = className;
120 
121  m_classNames.push_back(className);
122  }
123 
124  size_t num_classes() const {return m_classNames.size();}
125  const std::string& class_name(const size_t i) const {return m_classNames.at(i);}
126  const std::string& class_name(const TBase& cls) const
127  {
128  std::string typeName = typeid(cls).name();
129  class_name_map_t::const_iterator i = m_classNameMap.find(typeName);
130  if(i != m_classNameMap.end())
131  return i->second;
132  static const std::string defaultName ("");
133  return defaultName;
134  }
135 
136 private:
137  typedef TBase* (*factory_sig)();
138 
139  struct ClassInfo {
140  ClassInfo () {}
141  ClassInfo (const char* _name,
142  factory_sig _factory) :
143  name(_name),
144  factory(_factory)
145  {}
146 
147  const char* name;
149  };
150 
151  typedef std::map<std::string, ClassInfo> class_map_t;
152  typedef std::map<std::string, std::string> class_name_map_t;
155  std::vector<std::string> m_classNames;
156 };
157 
158 }// end of namespace
159 
160 #endif //__H__UG_factory
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
A factory class which creates instances given a class-name.
Definition: factory.h:75
std::map< std::string, ClassInfo > class_map_t
Definition: factory.h:151
class_name_map_t m_classNameMap
key: type-name, value: class-names
Definition: factory.h:154
std::vector< std::string > m_classNames
Definition: factory.h:155
std::map< std::string, std::string > class_name_map_t
Definition: factory.h:152
class_map_t m_classMap
contains ClassInfo objects accessible by class-name.
Definition: factory.h:153
SmartPtr< TBase > create(const std::string &className)
Definition: factory.h:83
TBase * create_raw(const char *className)
Definition: factory.h:98
SmartPtr< TBase > create(const char *className)
Definition: factory.h:88
void register_class(const char *name)
Definition: factory.h:107
TBase * create_raw(const std::string &className)
Definition: factory.h:93
size_t num_classes() const
Definition: factory.h:124
TBase *(* factory_sig)()
Definition: factory.h:137
const std::string & class_name(const TBase &cls) const
Definition: factory.h:126
Factory()
Definition: factory.h:77
const std::string & class_name(const size_t i) const
Definition: factory.h:125
string ClassInfo(const IExportedClass &c)
Prints the (const) method of one class.
Definition: class_helper.cpp:376
#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:49
the ug namespace
function func(x, y, z, t, si)
Definition: factory.h:139
factory_sig factory
Definition: factory.h:148
const char * name
Definition: factory.h:147
ClassInfo()
Definition: factory.h:140
ClassInfo(const char *_name, factory_sig _factory)
Definition: factory.h:141
Definition: register_type_pair_functor.h:43