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