libMesh
factory.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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"
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 UniquePtr<Base> build (const std::string & name);
66 
71  virtual UniquePtr<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 UniquePtr<Base> create () libmesh_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
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 (typename std::map<std::string,Factory<Base> *>::const_iterator
139  it = factory_map().begin(); it != factory_map().end(); ++it)
140  libMesh::err << " " << it->first << std::endl;
141 
142  libmesh_error_msg("Exiting...");
143 
144  // We'll never get here
145  return UniquePtr<Base>();
146  }
147 
148  Factory<Base> * f = factory_map()[name];
149  return UniquePtr<Base>(f->create());
150 }
151 
152 
153 
154 template <class Derived, class Base>
155 inline
157 {
158  return UniquePtr<Base>(new Derived);
159 }
160 
161 
162 } // namespace libMesh
163 
164 #endif // LIBMESH_FACTORY_H
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:39
OStreamProxy err
~FactoryImp()
Destructor.
Definition: factory.h:100
static std::map< std::string, Factory< Base > * > & factory_map()
Map from a name to a Factory<Base> * pointer.
The libMesh namespace provides an interface to certain functionality in the library.
Factory implementation class.
Definition: factory.h:88
virtual UniquePtr< Base > create() libmesh_override
Definition: factory.h:156
libmesh_assert(j)
std::unique_ptr< T > UniquePtr
Definition: auto_ptr.h:46
Factory class definition.
Definition: factory.h:46
static UniquePtr< Base > build(const std::string &name)
Builds an object of type Base identified by name.
Definition: factory.h:129
FactoryImp(const std::string &name)
Constructor.
Definition: factory.h:95
virtual ~Factory()
Destructor.
Definition: factory.h:60
Factory(const std::string &name)
Constructor.
Definition: factory.h:116
virtual UniquePtr< Base > create()=0
Create a Base class.