OGS 6.2.1-97-g73d1aeda3
CreateComponent.cpp
Go to the documentation of this file.
1 
14 #include "CreateComponent.h"
15 
16 #include "BaseLib/ConfigTree.h"
17 #include "ParameterLib/Parameter.h"
18 
19 #include "Components/Components.h"
20 #include "CreateProperty.h"
21 
22 namespace
23 {
24 std::unique_ptr<MaterialPropertyLib::Component> createComponent(
25  BaseLib::ConfigTree const& config,
26  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters)
27 {
28  using namespace MaterialPropertyLib;
29  // Parsing the component name
31  auto const& component_name = config.getConfigParameter<std::string>("name");
32 
33  // Check whether a name is given or not
34  if (component_name.empty())
35  {
36  OGS_FATAL("Component name is a mandatory field and cannot be empty.");
37  }
38 
39  // Parsing component properties. If a component name is given and this
40  // component is predefined in the class implementation, properties
41  // become optional. The default values of properties will be overwritten
42  // if specified.
43  std::unique_ptr<PropertyArray> properties =
45  createProperties(config.getConfigSubtreeOptional("properties"),
46  parameters);
47 
48  // If a name is given, it must conform with one of the derived component
49  // names in the following list:
50  if (boost::iequals(component_name, "water"))
51  {
52  return std::make_unique<Water>(std::move(properties));
53  }
54 
55  if (!properties)
56  {
57  // No component properties are provided. If the component is not
58  // specified, this results in a fatal error, since an unspecified
59  // component has no properties.
60  OGS_FATAL("No Properties defined for unspecified component");
61  }
62 
63  return std::make_unique<Component>(component_name, std::move(properties));
64 }
65 } // namespace
66 
67 namespace MaterialPropertyLib
68 {
69 std::vector<std::unique_ptr<Component>> createComponents(
70  boost::optional<BaseLib::ConfigTree> const& config,
71  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters)
72 {
73  if (!config)
74  {
75  return {};
76  }
77 
78  std::vector<std::unique_ptr<Component>> components;
79  for (
80  auto const& component_config :
82  config->getConfigSubtreeList("component"))
83  {
84  auto component = createComponent(component_config, parameters);
85 
86  if (std::find_if(components.begin(),
87  components.end(),
88  [component_name = component->name()](auto const& c) {
89  return c->name() == component_name;
90  }) != components.end())
91  {
92  OGS_FATAL("Found duplicates with the same component name tag '%s'.",
93  component->name().c_str());
94  }
95 
96  components.push_back(std::move(component));
97  }
98 
99  return components;
100 }
101 } // namespace MaterialPropertyLib
std::vector< std::unique_ptr< Component > > createComponents(boost::optional< BaseLib::ConfigTree > const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters)
std::unique_ptr< PropertyArray > createProperties(boost::optional< BaseLib::ConfigTree > const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters)
T getConfigParameter(std::string const &param) const
std::unique_ptr< MaterialPropertyLib::Component > createComponent(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters)
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
boost::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
Definition: ConfigTree.cpp:160