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 
23 
24 #include <algorithm>
25 
26 namespace WorldBuilder
27 {
28  namespace GravityModel
29  {
30 
32  = default;
33 
35  = default;
36 
37  void
38  Interface::declare_entries(Parameters &prm, const std::string &parent_name, const std::vector<std::string> &required_entries)
39  {
40 
41  unsigned int counter = 0;
42  for (auto &it : get_declare_map())
43  {
44  prm.enter_subsection("oneOf");
45  {
46  prm.enter_subsection(std::to_string(counter));
47  {
48  prm.enter_subsection("properties");
49  {
50  prm.declare_entry("", Types::Object(required_entries), "gravity model");
51 
52  prm.declare_entry("model",Types::String("",it.first),
53  "The name of the model for the gravity to use.");
54 
55  it.second(prm, parent_name);
56  }
57  prm.leave_subsection();
58  }
59  prm.leave_subsection();
60  }
61  prm.leave_subsection();
62 
63  counter++;
64 
65  }
66  }
67 
68  void
69  Interface::registerType(const std::string &name,
70  void ( *declare_entries)(Parameters &, const std::string &),
71  ObjectFactory *factory)
72  {
73  get_factory_map()[name] = factory;
75  }
76 
77  std::unique_ptr<Interface>
78  Interface::create(const std::string &name, WorldBuilder::World *world)
79  {
80  std::string lower_case_name;
81  std::transform(name.begin(),
82  name.end(),
83  std::back_inserter(lower_case_name),
84  ::tolower);;
85 
86  // Have a nice assert message to check whether a plugin exists in the case
87  // of a debug compilation.
88  WBAssertThrow(get_factory_map().find(lower_case_name) != get_factory_map().end(),
89  "Internal error: Plugin with name '" << lower_case_name << "' is not found. "
90  "The size of factories is " << get_factory_map().size() << ".");
91 
92  // Using at() because the [] will just insert values
93  // which is undesirable in this case. An exception is
94  // thrown when the name is not present.
95  return get_factory_map().at(lower_case_name)->create(world);
96  }
97  } // namespace GravityModel
98 } // namespace WorldBuilder
99 
static std::unique_ptr< Interface > create(const std::string &name, WorldBuilder::World *world)
Definition: interface.cc:78
static void declare_entries(Parameters &prm, const std::string &parent_name, const std::vector< std::string > &required_entries)
Definition: interface.cc:38
WorldBuilder::World * world
Definition: interface.h:99
void enter_subsection(const std::string &name)
Definition: parameters.cc:1871
static std::map< std::string, ObjectFactory * > & get_factory_map()
Definition: interface.h:104
static std::map< std::string, void(*)(Parameters &, const std::string &)> & get_declare_map()
Definition: interface.h:110
#define WBAssertThrow(condition, message)
Definition: assert.h:40
void declare_entry(const std::string &name, const Types::Interface &type, const std::string &documentation)
Definition: parameters.cc:197
static void registerType(const std::string &name, void(*)(Parameters &, const std::string &), ObjectFactory *factory)
Definition: interface.cc:69