GRINS-0.8.0
factory_abstract.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // GRINS - General Reacting Incompressible Navier-Stokes
5 //
6 // Copyright (C) 2014-2017 Paul T. Bauman, Roy H. Stogner
7 // Copyright (C) 2010-2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #ifndef GRINS_FACTORY_ABSTRACT_H
26 #define GRINS_FACTORY_ABSTRACT_H
27 
28 // C++
29 #include <string>
30 #include <map>
31 #include <sstream>
32 
33 // libMesh
34 #include "libmesh/libmesh_common.h" // libmesh_error_msg()
35 #include "libmesh/auto_ptr.h" // libMesh::UniquePtr
36 
37 namespace GRINS
38 {
40 
44  template<typename Base>
46  {
47  public:
48 
49  virtual ~FactoryAbstract(){}
50 
52  static libMesh::UniquePtr<Base> build(const std::string & name);
53 
55  virtual libMesh::UniquePtr<Base> create () = 0;
56 
57  protected:
58 
60  FactoryAbstract(const std::string & name);
61 
63  static FactoryAbstract<Base>& get_factory(const std::string & name);
64 
66  template<typename DerivedType>
67  static DerivedType& get_factory_subclass(const std::string & name);
68 
69  static std::map<std::string, FactoryAbstract<Base>*>& factory_map();
70 
71  };
72 
73  template <class Base>
74  inline
75  FactoryAbstract<Base>::FactoryAbstract(const std::string & name)
76  {
77  // Make sure we haven't already added this name
78  // to the map
79  libmesh_assert (!factory_map().count(name));
80 
81  factory_map()[name] = this;
82  }
83 
84  template <class Base>
85  inline
86  libMesh::UniquePtr<Base> FactoryAbstract<Base>::build(const std::string & name)
87  {
88  FactoryAbstract<Base>& factory = get_factory(name);
89  return factory.create();
90  }
91 
92  template <class Base>
93  inline
95  {
96  if (!factory_map().count(name))
97  {
98  std::stringstream ss;
99  ss << "ERROR: Tried to build an unknown FactoryAbstract type: "
100  << name << std::endl
101  << "valid options are:" << std::endl;
102 
103  for( typename std::map<std::string,FactoryAbstract<Base>*>::const_iterator
104  it = factory_map().begin(); it != factory_map().end(); ++it )
105  ss << " " << it->first << std::endl;
106 
107  libmesh_error_msg(ss.str());
108  }
109 
110  FactoryAbstract<Base>* factory = factory_map()[name];
111  return *factory;
112  }
113 
114  template <class Base>
115  template<typename DerivedType>
116  inline
117  DerivedType& FactoryAbstract<Base>::get_factory_subclass(const std::string & name)
118  {
119  FactoryAbstract<Base>& raw_factory = get_factory(name);
120  DerivedType& factory = libMesh::cast_ref<DerivedType&>(raw_factory);
121  return factory;
122  }
123 
124 } // end namespace GRINS
125 
126 #endif // GRINS_FACTORY_ABSTRACT_H
Abstract factory.
virtual libMesh::UniquePtr< Base > create()=0
Subclasses implement the actual construction of the Base object in create().
FactoryAbstract(const std::string &name)
Constructor is protected. Use the build() method to construct Base objects.
static std::map< std::string, FactoryAbstract< Base > * > & factory_map()
static libMesh::UniquePtr< Base > build(const std::string &name)
Use this method to build objects of type Base.
GRINS namespace.
static FactoryAbstract< Base > & get_factory(const std::string &name)
Helper method that looks up the factory and returns it if present, or error if it's not...
static DerivedType & get_factory_subclass(const std::string &name)
Like get_factory, but will downcast to DerivedType.

Generated on Tue Dec 19 2017 12:47:27 for GRINS-0.8.0 by  doxygen 1.8.9.1