Finite Volume Solver  prototype
A framework to build finite volume solvers for the AG Klein at the Freie Universität Berlin.
OutputFactory.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Maikel Nadolski
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #ifndef FUB_OUTPUT_FACTORY_HPP
22 #define FUB_OUTPUT_FACTORY_HPP
23 
26 
27 #include <functional>
28 #include <map>
29 #include <memory>
30 
31 namespace fub {
32 
33 template <typename Grid> class OutputFactory {
34 public:
35  using ProgramOptions = std::map<std::string, pybind11::object>;
36  OutputFactory() = default;
37 
38  template <typename Output, typename... Args>
39  bool RegisterOutput(std::string name, Args&&... args) {
40  return factories_
41  .emplace(name,
42  [=](const ProgramOptions& opts) {
43  return std::make_unique<Output>(opts, args...);
44  })
45  .second;
46  }
47 
48  bool Contains(const std::string& name) { return factories_.count(name) > 0; }
49 
50  std::unique_ptr<BasicOutput<Grid>> MakeOutput(const std::string& name,
51  const ProgramOptions& opts) {
52  auto it = factories_.find(name);
53  if (it == factories_.end()) {
54  return nullptr;
55  }
56  return it->second(opts);
57  }
58 
59 private:
60  std::map<std::string, std::function<std::unique_ptr<BasicOutput<Grid>>(
61  const ProgramOptions&)>>
63 };
64 
65 } // namespace fub
66 
67 #endif
Definition: OutputFactory.hpp:33
bool Contains(const std::string &name)
Definition: OutputFactory.hpp:48
bool RegisterOutput(std::string name, Args &&... args)
Definition: OutputFactory.hpp:39
std::unique_ptr< BasicOutput< Grid > > MakeOutput(const std::string &name, const ProgramOptions &opts)
Definition: OutputFactory.hpp:50
std::map< std::string, std::function< std::unique_ptr< BasicOutput< Grid > > const ProgramOptions &)> > factories_
Definition: OutputFactory.hpp:62
OutputFactory()=default
std::map< std::string, pybind11::object > ProgramOptions
Definition: OutputFactory.hpp:35
The fub namespace.
Definition: AnyBoundaryCondition.hpp:31
std::map< std::string, pybind11::object > ProgramOptions
Definition: ProgramOptions.hpp:40