Skip to content

Commit

Permalink
Fix local seed type and add get function for int
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang-yijun committed Feb 16, 2024
1 parent 5b94714 commit 7c9889a
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/world_builder/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "rapidjson/schema.h"
#include "world_builder/point.h"
#include "world_builder/types/unsigned_int.h"

namespace WorldBuilder
{
Expand Down
80 changes: 80 additions & 0 deletions include/world_builder/types/int.h
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

#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
34 changes: 34 additions & 0 deletions source/world_builder/parameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions source/world_builder/types/int.cc
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
#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

7 changes: 4 additions & 3 deletions source/world_builder/world.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
#include <world_builder/coordinate_system.h>
Expand Down Expand Up @@ -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.");

}
Expand Down Expand Up @@ -244,9 +245,9 @@ namespace WorldBuilder
/**
* Local random number seed parameter
*/
const int local_seed = prm.get<unsigned int>("random number seed");
const int local_seed = prm.get<int>("random number seed");

if (!isnan(local_seed))
if (local_seed>=0)
random_number_engine.seed(local_seed);

/**
Expand Down

0 comments on commit 7c9889a

Please sign in to comment.