From 7c9889acf373412783a2cccb2bcf492d058fe565 Mon Sep 17 00:00:00 2001 From: Wang-yijun Date: Fri, 16 Feb 2024 11:46:06 -0600 Subject: [PATCH] Fix local seed type and add get function for int --- include/world_builder/parameters.h | 1 + include/world_builder/types/int.h | 80 ++++++++++++++++++++++++++++++ source/world_builder/parameters.cc | 34 +++++++++++++ source/world_builder/types/int.cc | 64 ++++++++++++++++++++++++ source/world_builder/world.cc | 7 +-- 5 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 include/world_builder/types/int.h create mode 100644 source/world_builder/types/int.cc diff --git a/include/world_builder/parameters.h b/include/world_builder/parameters.h index ef406ce8c..9f7ea598e 100644 --- a/include/world_builder/parameters.h +++ b/include/world_builder/parameters.h @@ -26,6 +26,7 @@ #include "rapidjson/schema.h" #include "world_builder/point.h" +#include "world_builder/types/unsigned_int.h" namespace WorldBuilder { diff --git a/include/world_builder/types/int.h b/include/world_builder/types/int.h new file mode 100644 index 000000000..73d236f69 --- /dev/null +++ b/include/world_builder/types/int.h @@ -0,0 +1,80 @@ +/* + Copyright (C) 2018 - 2021 by the authors of the World Builder code. + + This file is part of the World Builder. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef WORLD_BUILDER_TYPES_INT_H +#define WORLD_BUILDER_TYPES_INT_H + + +#include "world_builder/types/interface.h" + + +namespace WorldBuilder +{ + class Parameters; + + namespace Types + { + + /** + * This class represents a bool value with documentation + */ + class Int final: public Interface + { + public: + /** + * A constructor for the load_entry function + */ + Int(int default_value = 0); + + /** + * Copy constructor + */ + Int(Int const &other); + + /** + * Destructor + */ + ~Int() override final; + + + /** + * Todo + */ + void write_schema(Parameters &prm, + const std::string &name, + const std::string &documentation) const override final; + + unsigned int value {0}; + unsigned int default_value; + + protected: + /** + * This implements the actual cloneing for the clone function in the base class. + */ + Int *clone_impl() const override final + { + return new Int(*this); + }; + private: + + }; + } // namespace Types +} // namespace WorldBuilder + +#endif diff --git a/source/world_builder/parameters.cc b/source/world_builder/parameters.cc index 8eaac431b..6b19b2e21 100644 --- a/source/world_builder/parameters.cc +++ b/source/world_builder/parameters.cc @@ -296,6 +296,40 @@ namespace WorldBuilder const std::string base = this->get_full_json_path(); const Value *value = Pointer((base + "/" + name).c_str()).Get(parameters); +#ifdef debug + bool required = false; + if (Pointer((base + "/required").c_str()).Get(declarations) != NULL) + { + for (auto &v : Pointer((base + "/required").c_str()).Get(declarations)->GetArray()) + { + if (v.GetString() == name) + { + required = true; + } + } + } + + WBAssert(value != NULL || required == false, + "Internal error: Value \"" << base << '/' << name << "/type\" not found in the input file, while it was set as required."); +#endif + if (value == nullptr) + { + value = Pointer((get_full_json_schema_path() + "/" + name + "/default value").c_str()).Get(declarations); + WBAssertThrow(value != nullptr, + "internal error: could not retrieve the default value at: " + << base + "/" + name + "/default value"); + } + + return value->GetUint(); + } + + template<> + int + Parameters::get(const std::string &name) + { + const std::string base = this->get_full_json_path(); + const Value *value = Pointer((base + "/" + name).c_str()).Get(parameters); + #ifdef debug bool required = false; if (Pointer((base + "/required").c_str()).Get(declarations) != NULL) diff --git a/source/world_builder/types/int.cc b/source/world_builder/types/int.cc new file mode 100644 index 000000000..608977f12 --- /dev/null +++ b/source/world_builder/types/int.cc @@ -0,0 +1,64 @@ +/* + Copyright (C) 2018 - 2021 by the authors of the World Builder code. + + This file is part of the World Builder. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ +#include "world_builder/types/int.h" + +#include "world_builder/parameters.h" + +namespace WorldBuilder +{ + namespace Types + { + Int::Int(int default_value_) + : + + default_value(default_value_) + { + this->type_name = Types::type::Int; + } + + + Int::Int(Int const &other) + : + value(other.value), + default_value(other.default_value) + { + this->type_name = Types::type::Int; + } + + Int::~Int () + = default; + + + void + Int::write_schema(Parameters &prm, + const std::string &name, + const std::string &documentation) const + { + using namespace rapidjson; + Document &declarations = prm.declarations; + + const std::string base = prm.get_full_json_path() + "/" + name; + Pointer((base + "/default value").c_str()).Set(declarations,default_value); + Pointer((base + "/type").c_str()).Set(declarations,"integer"); + Pointer((base + "/description").c_str()).Set(declarations,documentation.c_str()); + + } + } // namespace Types +} // namespace WorldBuilder + diff --git a/source/world_builder/world.cc b/source/world_builder/world.cc index 5fd66cf62..fb0bd92dc 100644 --- a/source/world_builder/world.cc +++ b/source/world_builder/world.cc @@ -30,6 +30,7 @@ #include "world_builder/types/object.h" #include "world_builder/types/plugin_system.h" #include "world_builder/types/point.h" +#include "world_builder/types/int.h" #include #include @@ -141,7 +142,7 @@ namespace WorldBuilder prm.declare_entry("features", Types::PluginSystem("",Features::Interface::declare_entries, {"model"}),"A list of features."); - prm.declare_entry("random number seed", Types::UnsignedInt(NaN::IQNAN), + prm.declare_entry("random number seed", Types::Int(-1), "Use random number seed = 1 to generate random numbers."); } @@ -244,9 +245,9 @@ namespace WorldBuilder /** * Local random number seed parameter */ - const int local_seed = prm.get("random number seed"); + const int local_seed = prm.get("random number seed"); - if (!isnan(local_seed)) + if (local_seed>=0) random_number_engine.seed(local_seed); /**