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 
20 
22 
23 #include <algorithm>
24 
26 
27 namespace WorldBuilder
28 {
29  namespace Features
30  {
31  namespace FaultModels
32  {
33  namespace Velocity
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  // The type needs to be stored in a separate value, otherwise there are memory issues
47  const Types::String type = Types::String("replace", std::vector<std::string> {"replace", "add", "subtract"});
48 
49  prm.declare_model_entries("velocity",parent_name, get_declare_map(),required_entries,
50  {
51  std::tuple<std::string,const WorldBuilder::Types::Interface &, std::string>
52  {
53  "operation", type,
54  "Whether the value should replace any value previously defined at this location (replace), "
55  "add the value to the previously define value (add) or subtract the value to the previously "
56  "define value (subtract)."
57  }
58  });
59  }
60 
61 
62  void
63  Interface::registerType(const std::string &name,
64  void ( *declare_entries)(Parameters &, const std::string &),
65  ObjectFactory *factory)
66  {
67  get_factory_map()[name] = factory;
69  }
70 
71  std::unique_ptr<Interface>
72  Interface::create(const std::string &name, WorldBuilder::World *world)
73  {
74  std::string lower_case_name;
75  std::transform(name.begin(),
76  name.end(),
77  std::back_inserter(lower_case_name),
78  ::tolower);;
79 
80  // Have a nice assert message to check whether a plugin exists in the case
81  // of a debug compilation.
82  WBAssertThrow(get_factory_map().find(lower_case_name) != get_factory_map().end(),
83  "Internal error: Plugin with name '" << lower_case_name << "' is not found. "
84  "The size of factories is " << get_factory_map().size() << '.');
85 
86  // Using at() because the [] will just insert values
87  // which is undesirable in this case. An exception is
88  // thrown when the name is not present.
89  return get_factory_map().at(lower_case_name)->create(world);
90  }
91  } // namespace Velocity
92  } // namespace FaultModels
93  } // namespace Features
94 } // namespace WorldBuilder
95 
static std::map< std::string, void(*)(Parameters &, const std::string &)> & get_declare_map()
Definition: interface.h:131
static void declare_entries(Parameters &prm, const std::string &parent_name, const std::vector< std::string > &required_entries)
Definition: interface.cc:42
static std::unique_ptr< Interface > create(const std::string &name, WorldBuilder::World *world)
Definition: interface.cc:72
static std::map< std::string, ObjectFactory * > & get_factory_map()
Definition: interface.h:125
#define WBAssertThrow(condition, message)
Definition: assert.h:40
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 void registerType(const std::string &name, void(*)(Parameters &, const std::string &), ObjectFactory *factory)
Definition: interface.cc:63