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 
24 namespace WorldBuilder
25 {
26  namespace Features
27  {
28  namespace ContinentalPlateModels
29  {
30  namespace Composition
31  {
33  = default;
34 
36  = default;
37 
38  void
40  const std::string &parent_name,
41  const std::vector<std::string> &required_entries)
42  {
43  prm.declare_model_entries("composition",parent_name, get_declare_map(),required_entries);
44  }
45 
46 
47  void
48  Interface::registerType(const std::string &name,
49  void ( *declare_entries)(Parameters &, const std::string &),
50  ObjectFactory *factory)
51  {
52  get_factory_map()[name] = factory;
54  }
55 
56  std::unique_ptr<Interface>
57  Interface::create(const std::string &name, WorldBuilder::World *world)
58  {
59  std::string lower_case_name;
60  std::transform(name.begin(),
61  name.end(),
62  std::back_inserter(lower_case_name),
63  ::tolower);;
64 
65  // Have a nice assert message to check whether a plugin exists in the case
66  // of a debug compilation.
67  WBAssertThrow(get_factory_map().find(lower_case_name) != get_factory_map().end(),
68  "Internal error: Plugin with name '" << lower_case_name << "' is not found. "
69  "The size of factories is " << get_factory_map().size() << '.');
70 
71  // Using at() because the [] will just insert values
72  // which is undesirable in this case. An exception is
73  // thrown when the name is not present.
74  return get_factory_map().at(lower_case_name)->create(world);
75  }
76  } // namespace Composition
77  } // namespace ContinentalPlateModels
78  } // namespace Features
79 } // namespace WorldBuilder
80 
static void registerType(const std::string &name, void(*)(Parameters &, const std::string &), ObjectFactory *factory)
Definition: interface.cc:48
static std::unique_ptr< Interface > create(const std::string &name, WorldBuilder::World *world)
Definition: interface.cc:57
#define WBAssertThrow(condition, message)
Definition: assert.h:40
static std::map< std::string, void(*)(Parameters &, const std::string &)> & get_declare_map()
Definition: interface.h:123
void declare_model_entries(const std::string &model_group_name, const std::string &parent_name, const std::map< std::string, void(*)(Parameters &, const std::string &)> &declare_map, const std::vector< std::string > &required_entries={}, const std::vector< std::tuple< std::string, const WorldBuilder::Types::Interface &, std::string > > &extra_declarations={})
Definition: parameters.cc:1886
static std::map< std::string, ObjectFactory * > & get_factory_map()
Definition: interface.h:117
static void declare_entries(Parameters &prm, const std::string &parent_name, const std::vector< std::string > &required_entries)
Definition: interface.cc:39