World Builder  1.1.0-pre
A geodynamic initial conditions generator
object.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 
24 
25 namespace WorldBuilder
26 {
27  namespace Types
28  {
29  Object::Object(std::vector<std::string> required_,
30  const bool additional_properties_)
31  :
32  required(std::move(required_)),
33  additional_properties(additional_properties_)
34  {
36  }
37 
38  Object::Object(Object const &other)
39  :
40  required(other.required),
42  {
44  }
45 
47  = default;
48 
49  void
51  const std::string & /*name*/,
52  const std::string &documentation) const
53  {
54  // The object has to be defined just above the properties section.
55  // because it is very inconvenient to make this object there, we
56  // take care here to do that.
57  WBAssertThrow(!prm.path.empty() && prm.path.back() == "properties",
58  "The Object must be defined in a subsection called properties.");
59  prm.leave_subsection();
60  {
61  using namespace rapidjson;
62  Document &declarations = prm.declarations;
63  const std::string path = prm.get_full_json_path();
64 
65  Pointer((path + "/type").c_str()).Set(declarations,"object");
66  Pointer((path + "/description").c_str()).Set(declarations,documentation.c_str());
67  Pointer((path + "/additionalProperties").c_str()).Set(declarations,additional_properties);
68 
69  if (!required.empty())
70  {
71  for (unsigned int i = 0; i < required.size(); ++i)
72  {
73  if (i == 0 && Pointer((path + "/required").c_str()).Get(declarations) == nullptr)
74  {
75  // The required array doesn't exist yet, so we create it and fill it.
76  Pointer((path + "/required/0").c_str()).Create(declarations);
77  Pointer((path + "/required/0").c_str()).Set(declarations, required[i].c_str());
78  }
79  else
80  {
81  // The required array already exist yet, so we add an element to the end.
82  Pointer((path + "/required/-").c_str()).Set(declarations, required[i].c_str());
83  }
84  }
85  }
86  }
87  prm.enter_subsection("properties");
88  }
89  } // namespace Types
90 } // namespace WorldBuilder
91 
std::vector< std::string > required
Definition: object.h:74
void enter_subsection(const std::string &name)
Definition: parameters.cc:1871
void write_schema(Parameters &prm, const std::string &name, const std::string &documentation) const override final
Definition: object.cc:50
std::string get_full_json_path(size_t max_size=std::numeric_limits< size_t >::max()) const
Definition: parameters.cc:1933
Object(std::vector< std::string > required=std::vector< std::string >(), const bool additional_properties=false)
Definition: object.cc:29
#define WBAssertThrow(condition, message)
Definition: assert.h:40
std::vector< std::string > path
Definition: parameters.h:246
rapidjson::Document declarations
Definition: parameters.h:248