Files
karl/src/simulation/parameter.cpp
T

71 lines
2.0 KiB
C++
Executable File

#include "simulation/parameter.h"
#include <cstdlib>
#include <stdexcept>
#include "jansson.h"
#include "simulation/errors.h"
#include "simulation/jansson_additionals.h"
Parameter::Parameter(std::string filename)
: j_error_(new json_error_t()), j_file_(openParameterFile(filename)) {}
Parameter::~Parameter() {
delete j_error_;
delete j_file_;
}
json_t* Parameter::openParameterFile(const std::string& filename) const {
json_t* j_file =
json_load_file(filename.c_str(), JSON_DECODE_INT_AS_REAL, j_error_);
if (!j_file) {
ERROR("Can not load parameter file: " + filename);
}
return j_file;
}
double Parameter::doubleValue(const json_path& path) const {
return json_double(json_object_by_path(j_file_, path));
}
double Parameter::doubleValue(const json_path& path,
double default_value) const {
return json_double(json_object_by_path(j_file_, path), default_value);
}
int Parameter::intValue(const json_path& path) const {
return json_int(json_object_by_path(j_file_, path));
}
int Parameter::intValue(const json_path& path, int default_value) const {
return json_int(json_object_by_path(j_file_, path), default_value);
}
std::string Parameter::stringValue(const json_path& path) const {
return json_string(json_object_by_path(j_file_, path));
}
std::string Parameter::stringValue(const json_path& path,
const std::string& default_value) const {
return json_string(json_object_by_path(j_file_, path), default_value);
}
bool Parameter::boolValue(const json_path& path) const {
return json_bool(json_object_by_path(j_file_, path));
}
bool Parameter::boolValue(const json_path& path, bool default_value) const {
return json_bool(json_object_by_path(j_file_, path), default_value);
}
bool Parameter::pathExists(const json_path& path) const {
return json_object_by_path(j_file_, path) != nullptr;
}
json_t* Parameter::jsonFile() const {
return j_file_;
}
json_t* Parameter::jsonObjectFromBase(const json_path& path) const {
return json_object_by_path(j_file_, path);
}