libMesh
Loading...
Searching...
No Matches
factory.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
27// C++ includes
28#include <cstddef>
29#include <map>
30#include <memory> // make_unique
31#include <string>
32
33namespace libMesh
34{
35
36
37
45template <class Base>
47{
48protected:
49
53 Factory (const std::string & name);
54
55public:
56
60 virtual ~Factory () = default;
61
65 static std::unique_ptr<Base> build (const std::string & name);
66
71 virtual std::unique_ptr<Base> create () = 0;
72
73
74protected:
75
79 static std::map<std::string, Factory<Base> *> & factory_map();
80};
81
82
83
87template <class Derived, class Base>
88class FactoryImp: public Factory<Base>
89{
90public:
91
95 FactoryImp (const std::string & name) : Factory<Base>(name) { }
96
100 ~FactoryImp () = default;
101
102private:
103
107 virtual std::unique_ptr<Base> create () override;
108};
109
110
111
112// -----------------------------------------------------
113// Factory members
114template <class Base>
115inline
116Factory<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
127template <class Base>
128inline
129std::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
150template <class Derived, class Base>
151inline
152std::unique_ptr<Base> FactoryImp<Derived,Base>::create ()
153{
154 return std::make_unique<Derived>();
155}
156
157
158} // namespace libMesh
159
160#endif // LIBMESH_FACTORY_H
Factory implementation class.
Definition factory.h:89
FactoryImp(const std::string &name)
Constructor.
Definition factory.h:95
~FactoryImp()=default
Destructor.
virtual std::unique_ptr< Base > create() override
Definition factory.h:152
Factory class definition.
Definition factory.h:47
virtual ~Factory()=default
Destructor.
Factory(const std::string &name)
Constructor.
Definition factory.h:116
static std::map< std::string, Factory< Base > * > & factory_map()
Map from a name to a Factory<Base> * pointer.
static std::unique_ptr< Base > build(const std::string &name)
Builds an object of type Base identified by name.
Definition factory.h:129
virtual std::unique_ptr< Base > create()=0
Create a Base class.
The libMesh namespace provides an interface to certain functionality in the library.
OStreamProxy err
libmesh_assert(ctx)