ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_DustModelRegistry.H
Go to the documentation of this file.
1 #ifndef ERF_DUST_MODEL_REGISTRY_H
2 #define ERF_DUST_MODEL_REGISTRY_H
3 
4 #include <ERF_DustModel.H>
5 
6 #include <functional>
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 /**
12  * @file ERF_DustModelRegistry.H
13  * @brief The dust models compiled into this executable
14  *
15  * ERF lists no model itself. Each model registers itself from its own .cpp with
16  *
17  * ERF_REGISTER_DUST_MODEL(MyModel, "mymodel")
18  *
19  * at namespace scope, and erf.dust.model then selects it by that name. The
20  * model's directory (DUST_HOME in GNU make, ERF_DUST_MODEL_DIRS in CMake) adds
21  * that .cpp to the build, so adding a model changes no file of ERF.
22  *
23  * The registration runs during static initialization, so the .cpp that holds it
24  * must be linked as an object file (ERF's CMake build compiles model directories
25  * into its OBJECT library, and GNU make links every object). This is the scheme of
26  * the fire engines (ERF_FireEngineRegistry.H) and the structure models.
27  */
28 
29 /// Creates a model; the model reads its inputs in initialize().
30 using DustModelCreator = std::function<std::unique_ptr<DustModel>()>;
31 
32 /**
33  * @brief Add a model to the registry; aborts on an empty name or a name registered twice
34  *
35  * Called by ERF_REGISTER_DUST_MODEL; returns true so the macro can hold the
36  * result in a static variable.
37  */
38 bool register_dust_model (const std::string& name, DustModelCreator creator);
39 
40 /// Names erf.dust.model accepts, sorted; empty when no model is compiled in.
41 std::vector<std::string> dust_model_names ();
42 
43 /// The model registered as name, or nullptr when no model has that name.
44 std::unique_ptr<DustModel> make_dust_model (const std::string& name);
45 
46 /**
47  * @brief The model the inputs select, or nullptr when erf.dust.enable is not true
48  *
49  * erf.dust.model names the model; with one model compiled in it may be left
50  * out. Aborts when dust is enabled and no model, or no model of that name, is
51  * compiled in, listing the names this executable accepts, and when the model
52  * fills more dust scalars than the build has (NSCALARS_DUST).
53  */
54 std::unique_ptr<DustModel> make_dust_model_from_inputs ();
55 
56 #define ERF_DUST_MODEL_CONCAT_(a, b) a##b
57 #define ERF_DUST_MODEL_CONCAT(a, b) ERF_DUST_MODEL_CONCAT_(a, b)
58 
59 /**
60  * @brief Register model class Class under name: erf.dust.model = name
61  * selects it. Class is default-constructed.
62  */
63 #define ERF_REGISTER_DUST_MODEL(Class, name) \
64  namespace { \
65  [[maybe_unused]] const bool ERF_DUST_MODEL_CONCAT(erf_dust_model_registered_, __LINE__) = \
66  register_dust_model((name), []() -> std::unique_ptr<DustModel> { \
67  return std::make_unique<Class>(); \
68  }); \
69  }
70 
71 #endif
std::unique_ptr< DustModel > make_dust_model_from_inputs()
The model the inputs select, or nullptr when erf.dust.enable is not true.
Definition: ERF_DustModelRegistry.cpp:63
std::vector< std::string > dust_model_names()
Names erf.dust.model accepts, sorted; empty when no model is compiled in.
Definition: ERF_DustModelRegistry.cpp:46
std::unique_ptr< DustModel > make_dust_model(const std::string &name)
The model registered as name, or nullptr when no model has that name.
Definition: ERF_DustModelRegistry.cpp:54
std::function< std::unique_ptr< DustModel >()> DustModelCreator
Creates a model; the model reads its inputs in initialize().
Definition: ERF_DustModelRegistry.H:30
bool register_dust_model(const std::string &name, DustModelCreator creator)
Add a model to the registry; aborts on an empty name or a name registered twice.
Definition: ERF_DustModelRegistry.cpp:31
The interface between ERF and a dust model.
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139