queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Factory.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 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 QUESO_FACTORY_H
26 #define QUESO_FACTORY_H
27 
28 #include <queso/asserts.h>
29 #include <queso/SharedPtr.h>
30 
31 #include <cstddef>
32 #include <map>
33 #include <string>
34 #include <iostream>
35 
36 namespace QUESO
37 {
38 
42 template <class Base>
43 class Factory
44 {
45 protected:
49  Factory(const std::string & name);
50 
51 public:
55  virtual ~Factory() {}
56 
60  static typename SharedPtr<Base>::Type build(const std::string & name);
61 
66  virtual typename SharedPtr<Base>::Type create() = 0;
67 
68 protected:
72  static std::map<std::string, Factory<Base> *> & factory_map();
73 };
74 
78 template <class Derived, class Base>
79 class FactoryImp: public Factory<Base>
80 {
81 public:
85  FactoryImp(const std::string & name) : Factory<Base>(name) { }
86 
91 
92 private:
96  virtual typename SharedPtr<Base>::Type create();
97 };
98 
99 // -----------------------------------------------------
100 // Factory members
101 template <class Base>
102 inline
103 Factory<Base>::Factory(const std::string & name)
104 {
105  // Make sure we haven't already added this name
106  // to the map
107  queso_assert(!factory_map().count(name));
108 
109  factory_map()[name] = this;
110 }
111 
112 template <class Base>
113 inline
114 typename SharedPtr<Base>::Type Factory<Base>::build(const std::string & name)
115 {
116  // name not found in the map
117  if (!factory_map().count(name))
118  {
119  std::cerr << "Tried to build an unknown type: " << name << std::endl;
120 
121  std::cerr << "valid options are:" << std::endl;
122 
123  for (typename std::map<std::string,Factory<Base> *>::const_iterator
124  it = factory_map().begin(); it != factory_map().end(); ++it)
125  std::cerr << " " << it->first << std::endl;
126 
127  queso_error_msg("Exiting...");
128 
129  // We'll never get here
130  return typename SharedPtr<Base>::Type();
131  }
132 
133  Factory<Base> * f = factory_map()[name];
134  return typename SharedPtr<Base>::Type(f->create());
135 }
136 
137 // Note - this cannot be inlined!
138 // template <class Base>
139 // std::map<std::string, Factory<Base> *> & Factory<Base>::factory_map()
140 // {
141 // static std::map<std::string, Factory<Base> *> _factory_map;
142 
143 // return _factory_map;
144 // }
145 
146 template <class Derived, class Base>
147 inline
149 {
150  return typename SharedPtr<Base>::Type(new Derived);
151 }
152 
153 } // namespace QUESO
154 
155 #endif // QUESO_FACTORY_H
std::shared_ptr< T > Type
Definition: SharedPtr.h:69
FactoryImp(const std::string &name)
Definition: Factory.h:85
virtual SharedPtr< Base >::Type create()
Definition: Factory.h:148
static SharedPtr< Base >::Type build(const std::string &name)
Definition: Factory.h:114
Factory(const std::string &name)
Definition: Factory.h:103
virtual ~Factory()
Definition: Factory.h:55
static std::map< std::string, Factory< Base > * > & factory_map()
virtual SharedPtr< Base >::Type create()=0

Generated on Tue Jun 5 2018 19:48:54 for queso-0.57.1 by  doxygen 1.8.5