World Builder  1.1.0-pre
A geodynamic initial conditions generator
interface.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018-2024 by the authors of the World Builder code.
3 
4  This file is part of the World Builder.
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19 
21 
22 #include <algorithm>
23 
26 
27 namespace WorldBuilder
28 {
29  namespace Features
30  {
31  namespace SubductingPlateModels
32  {
33  namespace Grains
34  {
36  = default;
37 
39  = default;
40 
41  void
43  const std::string &parent_name,
44  const std::vector<std::string> &required_entries)
45  {
46  unsigned int counter = 0;
47  for (auto &it : get_declare_map())
48  {
49  // prevent infinite recursion
50  if (it.first != parent_name)
51  {
52  prm.enter_subsection("oneOf");
53  {
54  prm.enter_subsection(std::to_string(counter));
55  {
56  prm.enter_subsection("properties");
57  {
58  prm.declare_entry("", Types::Object(required_entries), "grains object");
59 
60  prm.declare_entry("model", Types::String("",it.first),
61  "The name of the grains model.");
62 
63  it.second(prm, parent_name);
64  }
65  prm.leave_subsection();
66  }
67  prm.leave_subsection();
68  }
69  prm.leave_subsection();
70 
71  counter++;
72 
73  }
74  }
75  }
76 
77 
78  void
79  Interface::registerType(const std::string &name,
80  void ( *declare_entries)(Parameters &, const std::string &),
81  ObjectFactory *factory)
82  {
83  get_factory_map()[name] = factory;
85  }
86 
87  std::unique_ptr<Interface>
88  Interface::create(const std::string &name, WorldBuilder::World *world)
89  {
90  std::string lower_case_name;
91  std::transform(name.begin(),
92  name.end(),
93  std::back_inserter(lower_case_name),
94  ::tolower);;
95 
96  // Have a nice assert message to check whether a plugin exists in the case
97  // of a debug compilation.
98  WBAssertThrow(get_factory_map().find(lower_case_name) != get_factory_map().end(),
99  "Internal error: Plugin with name '" << lower_case_name << "' is not found. "
100  "The size of factories is " << get_factory_map().size() << '.');
101 
102  // Using at() because the [] will just insert values
103  // which is undesirable in this case. An exception is
104  // thrown when the name is not present.
105  return get_factory_map().at(lower_case_name)->create(world);
106  }
107  } // namespace Grains
108  } // namespace SubductingPlateModels
109  } // namespace Features
110 } // namespace WorldBuilder
111 
void enter_subsection(const std::string &name)
Definition: parameters.cc:1871
static std::map< std::string, ObjectFactory * > & get_factory_map()
Definition: interface.h:126
#define WBAssertThrow(condition, message)
Definition: assert.h:40
static void registerType(const std::string &name, void(*)(Parameters &, const std::string &), ObjectFactory *factory)
Definition: interface.cc:79
static std::unique_ptr< Interface > create(const std::string &name, WorldBuilder::World *world)
Definition: interface.cc:88
static void declare_entries(Parameters &prm, const std::string &parent_name, const std::vector< std::string > &required_entries)
Definition: interface.cc:42
static std::map< std::string, void(*)(Parameters &, const std::string &)> & get_declare_map()
Definition: interface.h:132
void declare_entry(const std::string &name, const Types::Interface &type, const std::string &documentation)
Definition: parameters.cc:197