World Builder  1.1.0-pre
A geodynamic initial conditions generator
string.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 */
20 
21 
23 
24 namespace WorldBuilder
25 {
26  namespace Types
27  {
28 
29  String::String(std::string default_value_)
30  :
31  default_value(std::move(default_value_)),
32 
33  restricted_values({})
34  {
36  }
37 
38  String::String(std::string default_value_,
39  const std::string &restricted_value_)
40  :
41  default_value(std::move(default_value_)),
42  restricted_values({restricted_value_})
43  {
45  }
46 
47  String::String(String const &other)
48  :
49  value(other.value),
51  {
53  }
54 
55 
56  String::String(std::string default_value_,
57  std::vector<std::string> restricted_values_)
58  :
59  default_value(std::move(default_value_)),
60  restricted_values(std::move(restricted_values_))
61  {
63  }
64 
65  /*String::String(std::string default_value, std::string description)
66  :
67  value(default_value),
68  default_value(default_value),
69  description(description)
70  {
71  this->type_name = Types::type::String;
72  }*/
73 
74  String::String(std::string value_,
75  std::string default_value_,
76  std::string description_)
77  :
78  value(std::move(value_)),
79  default_value(std::move(default_value_)),
80  description(std::move(description_))
81  {
83  }
84 
86  = default;
87 
88  void
90  const std::string &name,
91  const std::string &documentation) const
92  {
93  using namespace rapidjson;
94  Document &declarations = prm.declarations;
95  const std::string base = prm.get_full_json_path() + "/" + name;
96  Pointer((base + "/default value").c_str()).Set(declarations,default_value.c_str());
97  Pointer((base + "/type").c_str()).Set(declarations,"string");
98  Pointer((base + "/description").c_str()).Set(declarations,documentation.c_str());
99  for (unsigned int i = 0; i < restricted_values.size(); ++i)
100  {
101  if (!restricted_values[i].empty())
102  {
103  if (i == 0 && Pointer((base + "/enum").c_str()).Get(declarations) == nullptr)
104  {
105  // The enum array doesn't exist yet, so we create it and fill it.
106  Pointer((base + "/enum/0").c_str()).Create(declarations);
107  Pointer((base + "/enum/0").c_str()).Set(declarations, restricted_values[i].c_str());
108  }
109  else
110  {
111  // The enum array already exist yet, so we add an element to the end.
112  Pointer((base + "/enum/-").c_str()).Set(declarations, restricted_values[i].c_str());
113  }
114  }
115  }
116  }
117 
118  } // namespace Types
119 } // namespace WorldBuilder
120 
std::string description
Definition: string.h:90
std::string get_full_json_path(size_t max_size=std::numeric_limits< size_t >::max()) const
Definition: parameters.cc:1933
String(std::string default_value)
Definition: string.cc:29
std::string default_value
Definition: string.h:89
void write_schema(Parameters &prm, const std::string &name, const std::string &documentation) const override final
Definition: string.cc:89
std::vector< std::string > restricted_values
Definition: string.h:91
rapidjson::Document declarations
Definition: parameters.h:248