factory.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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 
41 template <class Base>
42 class Factory
43 {
44 protected:
45 
49  Factory (const std::string& name);
50 
51 public:
52 
56  virtual ~Factory () {}
57 
61  static AutoPtr<Base> build (const std::string& name);
62 
67  virtual AutoPtr<Base> create () = 0;
68 
69 
70 protected:
71 
75  static std::map<std::string, Factory<Base>*>& factory_map();
76 };
77 
78 
79 
83 template <class Derived, class Base>
84 class FactoryImp: public Factory<Base>
85 {
86 public:
87 
91  FactoryImp (const std::string& name) : Factory<Base>(name) { }
92 
97 
98 private:
99 
104 
105 };
106 
107 
108 
109 // -----------------------------------------------------
110 // Factory members
111 template <class Base>
112 inline
113 Factory<Base>::Factory (const std::string& name)
114 {
115  // Make sure we haven't already added this name
116  // to the map
117  libmesh_assert (!factory_map().count(name));
118 
119  factory_map()[name] = this;
120 }
121 
122 
123 
124 template <class Base>
125 inline
127 {
128  // name not found in the map
129  if (!factory_map().count(name))
130  {
131  libMesh::err << "Tried to build an unknown type: " << name << std::endl;
132 
133  libMesh::err << "valid options are:" << std::endl;
134 
135  for (typename std::map<std::string,Factory<Base>*>::const_iterator
136  it = factory_map().begin(); it != factory_map().end(); ++it)
137  libMesh::err << " " << it->first << std::endl;
138 
139  libmesh_error();
140 
141  // Do this the stoopid way for IBM xlC
142  AutoPtr<Base> ret_val (NULL);
143 
144  return ret_val;
145  }
146 
147  // Do this the stoopid way for IBM xlC
148  Factory<Base> *f = factory_map()[name];
149 
150  AutoPtr<Base> ret_val (f->create());
151 
152  return ret_val;
153 }
154 
155 
156 
157 // Note - this cannot be inlined!
158 // template <class Base>
159 // std::map<std::string, Factory<Base>*>& Factory<Base>::factory_map()
160 // {
161 // static std::map<std::string, Factory<Base>*> _factory_map;
162 
163 // return _factory_map;
164 // }
165 
166 
167 
168 template <class Derived, class Base>
169 inline
171 {
172  // Do this the stoopid way for IBM xlC
173  AutoPtr<Base> ret_val (new Derived);
174 
175  return ret_val;
176 }
177 
178 
179 } // namespace libMesh
180 
181 #endif // LIBMESH_FACTORY_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:04 UTC

Hosted By:
SourceForge.net Logo