ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfaceEngineRegistry.H
Go to the documentation of this file.
1 #ifndef ERF_SURFACE_ENGINE_REGISTRY_H
2 #define ERF_SURFACE_ENGINE_REGISTRY_H
3 
5 #include <AMReX.H>
6 
7 #include <functional>
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 /**
14  * @file ERF_SurfaceEngineRegistry.H
15  * @brief The registry behind ERF_REGISTER_FIRE_ENGINE and ERF_REGISTER_FLOOD_ENGINE
16  *
17  * One registry per engine base class: SurfaceEngineRegistry<FireEngine,
18  * FireCouplingParams> holds the fire engines, SurfaceEngineRegistry<FloodEngine,
19  * FloodCouplingParams> the flood engines. Each coupling instantiates its
20  * registry in one .cpp (ERF_FireEngineRegistry.cpp, ERF_FloodEngineRegistry.cpp)
21  * behind its own functions (register_fire_engine, fire_engine_names,
22  * make_fire_engine, and the flood ones), so the map lives in that
23  * translation unit and the registration macros are unchanged.
24  */
25 template <class Engine, class Params>
27 {
28 public:
29  /// Creates an engine from the coupling inputs.
30  using Creator = std::function<std::unique_ptr<Engine>(const Params&)>;
31 
32  /// Add an engine; aborts on an empty name or a name registered twice.
33  static bool add (const SurfaceCouplingNames& names, const std::string& name, Creator creator)
34  {
35  auto& registry = map();
36  const std::string macro = std::string("ERF_REGISTER_") + names.upper + "_ENGINE";
37  if (name.empty()) {
38  amrex::Abort(names.tag() + " " + macro + ": an engine registered an empty name");
39  }
40  if (registry.count(name) > 0) {
41  amrex::Abort(names.tag() + " " + macro + ": two engines registered the name \"" + name
42  + "\"; each engine directory in the build must use its own name");
43  }
44  registry.emplace(name, std::move(creator));
45  return true;
46  }
47 
48  /// The registered names, sorted.
49  static std::vector<std::string> names ()
50  {
51  std::vector<std::string> out;
52  for (const auto& kv : map()) { out.push_back(kv.first); }
53  return out; // std::map keeps them sorted
54  }
55 
56  /// The engine registered under name, or nullptr.
57  static std::unique_ptr<Engine> make (const std::string& name, const Params& params)
58  {
59  const auto& registry = map();
60  const auto it = registry.find(name);
61  if (it == registry.end()) { return nullptr; }
62  return it->second(params);
63  }
64 
65 private:
66  // A function-local static, so it exists before the first registration
67  // whatever the order in which the engines' translation units are initialized
68  static std::map<std::string, Creator>& map ()
69  {
70  static std::map<std::string, Creator> registry;
71  return registry;
72  }
73 };
74 
75 #endif
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
The names a surface coupling uses in its inputs and messages.
Definition: ERF_SurfaceEngineRegistry.H:27
static std::unique_ptr< Engine > make(const std::string &name, const Params &params)
The engine registered under name, or nullptr.
Definition: ERF_SurfaceEngineRegistry.H:57
static std::map< std::string, Creator > & map()
Definition: ERF_SurfaceEngineRegistry.H:68
static bool add(const SurfaceCouplingNames &names, const std::string &name, Creator creator)
Add an engine; aborts on an empty name or a name registered twice.
Definition: ERF_SurfaceEngineRegistry.H:33
static std::vector< std::string > names()
The registered names, sorted.
Definition: ERF_SurfaceEngineRegistry.H:49
std::function< std::unique_ptr< Engine >(const Params &)> Creator
Creates an engine from the coupling inputs.
Definition: ERF_SurfaceEngineRegistry.H:30
Definition: ERF_SurfaceCouplingNames.H:18