factory.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_FACTORY_H
21 #define LIBMESH_FACTORY_H
22 
23 
24 // Local includes
25 #include "libmesh/libmesh_common.h"
26 #include "libmesh/auto_ptr.h" // libmesh_make_unique
27 
28 // C++ includes
29 #include <cstddef>
30 #include <map>
31 #include <string>
32 
33 namespace libMesh
34 {
35 
36 
37 
45 template <class Base>
46 class Factory
47 {
48 protected:
49 
53  Factory (const std::string & name);
54 
55 public:
56 
60  virtual ~Factory () {}
61 
65  static std::unique_ptr<Base> build (const std::string & name);
66 
71  virtual std::unique_ptr<Base> create () = 0;
72 
73 
74 protected:
75 
79  static std::map<std::string, Factory<Base> *> & factory_map();
80 };
81 
82 
83 
87 template <class Derived, class Base>
88 class FactoryImp: public Factory<Base>
89 {
90 public:
91 
95  FactoryImp (const std::string & name) : Factory<Base>(name) { }
96 
101 
102 private:
103 
107  virtual std::unique_ptr<Base> create () override;
108 };
109 
110 
111 
112 // -----------------------------------------------------
113 // Factory members
114 template <class Base>
115 inline
116 Factory<Base>::Factory (const std::string & name)
117 {
118  // Make sure we haven't already added this name
119  // to the map
120  libmesh_assert (!factory_map().count(name));
121 
122  factory_map()[name] = this;
123 }
124 
125 
126 
127 template <class Base>
128 inline
129 std::unique_ptr<Base> Factory<Base>::build (const std::string & name)
130 {
131  // name not found in the map
132  if (!factory_map().count(name))
133  {
134  libMesh::err << "Tried to build an unknown type: " << name << std::endl;
135 
136  libMesh::err << "valid options are:" << std::endl;
137 
138  for (const auto & pr : factory_map())
139  libMesh::err << " " << pr.first << std::endl;
140 
141  libmesh_error_msg("Exiting...");
142  }
143 
144  Factory<Base> * f = factory_map()[name];
145  return std::unique_ptr<Base>(f->create());
146 }
147 
148 
149 
150 template <class Derived, class Base>
151 inline
152 std::unique_ptr<Base> FactoryImp<Derived,Base>::create ()
153 {
154  return libmesh_make_unique<Derived>();
155 }
156 
157 
158 } // namespace libMesh
159 
160 #endif // LIBMESH_FACTORY_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
static std::map< std::string, Factory< Base > * > & factory_map()
Handles name-based creation of objects.
Definition: factory.h:46
virtual std::unique_ptr< Base > create()=0
FactoryImp(const std::string &name)
Definition: factory.h:95
OStreamProxy err(std::cerr)
virtual ~Factory()
Definition: factory.h:60
virtual std::unique_ptr< Base > create() override
Definition: factory.h:152
Factory(const std::string &name)
Definition: factory.h:116
static std::unique_ptr< Base > build(const std::string &name)
Definition: factory.h:129