ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_FloodEngineRegistry.H
Go to the documentation of this file.
1 #ifndef ERF_FLOOD_ENGINE_REGISTRY_H
2 #define ERF_FLOOD_ENGINE_REGISTRY_H
3 
5 #include <ERF_FloodEngine.H>
6 #include <AMReX.H>
7 
8 #include <functional>
9 #include <memory>
10 #include <string>
11 #include <type_traits>
12 #include <vector>
13 
14 /**
15  * @file ERF_FloodEngineRegistry.H
16  * @brief The flood engines compiled into this executable
17  *
18  * ERF lists no engine itself. Each engine registers itself from its own .cpp
19  * with
20  *
21  * ERF_REGISTER_FLOOD_ENGINE(MyEngine, "myengine")
22  *
23  * at namespace scope, and erf.flood.engine then selects it by that name. The
24  * engine's directory (FLOOD_HOME in GNU make, ERF_FLOOD_ENGINE_DIRS in CMake)
25  * adds that .cpp to the build, so adding an engine changes no file of ERF.
26  *
27  * The registration runs during static initialization, so the .cpp that holds
28  * it must be linked as an object file: inside a static archive (.a) that
29  * nothing else references, the linker drops it and the engine is missing.
30  * ERF's CMake build compiles engines into its OBJECT library, and GNU make
31  * links every object, so both keep it; an engine that ships a prebuilt
32  * library keeps its registering .cpp as source.
33  *
34  * The registry follows the fire-engine registry (ERF_FireEngineRegistry.H)
35  * and is independent of it: a build may hold fire engines, flood engines,
36  * both or neither.
37  */
38 
39 /// Creates an engine from the coupling inputs.
40 using FloodEngineCreator = std::function<std::unique_ptr<FloodEngine>(const FloodCouplingParams&)>;
41 
42 /**
43  * @brief Add an engine to the registry; aborts on a name registered twice
44  *
45  * Called by ERF_REGISTER_FLOOD_ENGINE; returns true so the macro can hold the
46  * result in a static variable.
47  */
48 bool register_flood_engine (const std::string& name, FloodEngineCreator creator);
49 
50 /// Names erf.flood.engine accepts, sorted; empty when no engine is compiled in.
51 std::vector<std::string> flood_engine_names ();
52 
53 /// The engine erf.flood.engine names, or nullptr when no engine has that name.
54 std::unique_ptr<FloodEngine> make_flood_engine (const std::string& name, const FloodCouplingParams& params);
55 
56 #define ERF_FLOOD_ENGINE_CONCAT_(a, b) a##b
57 #define ERF_FLOOD_ENGINE_CONCAT(a, b) ERF_FLOOD_ENGINE_CONCAT_(a, b)
58 
59 /**
60  * @brief Register engine class Class under name: erf.flood.engine = name
61  * selects it. Class is constructed from the coupling inputs,
62  * Class(const FloodCouplingParams&), or default-constructed when it has
63  * no such constructor.
64  */
65 #define ERF_REGISTER_FLOOD_ENGINE(Class, name) \
66  namespace { \
67  [[maybe_unused]] const bool ERF_FLOOD_ENGINE_CONCAT(erf_flood_engine_registered_, __LINE__) = \
68  register_flood_engine((name), [](const FloodCouplingParams& p) -> std::unique_ptr<FloodEngine> { \
69  if constexpr (std::is_constructible_v<Class, const FloodCouplingParams&>) { \
70  return std::make_unique<Class>(p); \
71  } else { \
72  amrex::ignore_unused(p); \
73  return std::make_unique<Class>(); \
74  } \
75  }); \
76  }
77 
78 #endif
The inputs of the atmosphere side of the flood coupling.
std::vector< std::string > flood_engine_names()
Names erf.flood.engine accepts, sorted; empty when no engine is compiled in.
Definition: ERF_FloodEngineRegistry.cpp:15
std::unique_ptr< FloodEngine > make_flood_engine(const std::string &name, const FloodCouplingParams &params)
The engine erf.flood.engine names, or nullptr when no engine has that name.
Definition: ERF_FloodEngineRegistry.cpp:21
std::function< std::unique_ptr< FloodEngine >(const FloodCouplingParams &)> FloodEngineCreator
Creates an engine from the coupling inputs.
Definition: ERF_FloodEngineRegistry.H:40
bool register_flood_engine(const std::string &name, FloodEngineCreator creator)
Add an engine to the registry; aborts on a name registered twice.
Definition: ERF_FloodEngineRegistry.cpp:9
The interface between the flood coupling and a flood engine.
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
Definition: ERF_FloodCouplingParams.H:20