Many, many more comments
This commit is contained in:
@@ -10,8 +10,12 @@
|
||||
#include "simulation/errors.h"
|
||||
#include "simulation/unit_converter.h"
|
||||
|
||||
// define the json_path type; contains the keys of the nested json-objects
|
||||
// as the elements in the vector e.g. ["Root", "Group1", "Group2", ..., "GroupN"]
|
||||
typedef std::vector<std::string> json_path;
|
||||
|
||||
// return the value specified by "path" as a json_t pointer
|
||||
// (can point to a json-object, json-array, etc.)
|
||||
inline json_t* json_object_by_path(json_t* object, const json_path& path) {
|
||||
json_t* j_obj = object;
|
||||
for (auto str : path) {
|
||||
@@ -20,6 +24,7 @@ inline json_t* json_object_by_path(json_t* object, const json_path& path) {
|
||||
return j_obj;
|
||||
}
|
||||
|
||||
// return the value of the json_t pointer "object" as an double; defaults "default value"
|
||||
inline double json_double(json_t* object, double default_value = 0.) {
|
||||
if (object) {
|
||||
return json_real_value(object);
|
||||
@@ -28,6 +33,8 @@ inline double json_double(json_t* object, double default_value = 0.) {
|
||||
}
|
||||
}
|
||||
|
||||
// return the value of the json_t pointer "object" as an int; defaults to "default value"
|
||||
// if the pointer is not a json-real value
|
||||
inline int json_int(json_t* object, int default_value = 0) {
|
||||
if (object) {
|
||||
return std::floor(json_real_value(object));
|
||||
@@ -36,6 +43,8 @@ inline int json_int(json_t* object, int default_value = 0) {
|
||||
}
|
||||
}
|
||||
|
||||
// return the value of the json_t pointer "object" as a string; defaults "default value"
|
||||
// if the pointer is not a json-string
|
||||
inline std::string json_string(json_t* object, std::string default_value = "") {
|
||||
if (object) {
|
||||
return std::string(json_string_value(object));
|
||||
@@ -44,6 +53,8 @@ inline std::string json_string(json_t* object, std::string default_value = "") {
|
||||
}
|
||||
}
|
||||
|
||||
// return the value of the json_t pointer "object" as a bool; defaults to default_value
|
||||
// if the pointer is not a json-string or the string has a value different from "true" or "false"
|
||||
inline bool json_bool(json_t* object, bool default_value = false) {
|
||||
if (object) {
|
||||
return (json_string(object) == "true");
|
||||
@@ -52,6 +63,9 @@ inline bool json_bool(json_t* object, bool default_value = false) {
|
||||
}
|
||||
}
|
||||
|
||||
// returns a blitz::TinyVector<double, s> with the values from a json_t pointer which points to
|
||||
// a json-array; if the json_t pointer is not an array or the array has a different size than
|
||||
// the specified template parameter s an error is thrown and the execution of the program terminated
|
||||
template <int s>
|
||||
blitz::TinyVector<double, s> json_blitz_vector(json_t* object) {
|
||||
blitz::TinyVector<double, s> vector;
|
||||
@@ -73,6 +87,8 @@ blitz::TinyVector<double, s> json_blitz_vector(json_t* object) {
|
||||
return vector;
|
||||
}
|
||||
|
||||
// returns a vector<double> containing the values of a json-array pointed to by
|
||||
// json_t* object; if json_t* object is not a json-array returns an empty vector
|
||||
inline std::vector<double> json_vector(json_t* object) {
|
||||
std::vector<double> vector;
|
||||
size_t index;
|
||||
@@ -83,6 +99,7 @@ inline std::vector<double> json_vector(json_t* object) {
|
||||
return vector;
|
||||
}
|
||||
|
||||
// return the json-array in "object" as a json_path variable
|
||||
inline json_path json_path_get(json_t* object) {
|
||||
json_path path;
|
||||
|
||||
@@ -94,6 +111,8 @@ inline json_path json_path_get(json_t* object) {
|
||||
return path;
|
||||
}
|
||||
|
||||
// returns a json_path variable, that consists of the path appended by the
|
||||
// json_path append
|
||||
inline json_path json_path_append(const json_path& path,
|
||||
const json_path& append) {
|
||||
json_path result = path;
|
||||
@@ -103,12 +122,24 @@ inline json_path json_path_append(const json_path& path,
|
||||
return result;
|
||||
}
|
||||
|
||||
// returns the value of a json-object, which should consist of a json-real with the key
|
||||
// "value" and a json-string with the key "unit" transformed to the specified length
|
||||
// "output_unit" (supported units: "mm", "cm", "m"). If the key "unit" does not exist or is not
|
||||
// a json-string an error will be thrown and the program will terminate. If the key "value"
|
||||
// does not exist or not a json-real it will default to 0.0
|
||||
inline double json_read_length(json_t* object, std::string output_unit = "m") {
|
||||
double value = json_double(json_object_by_path(object, {"value"}));
|
||||
std::string unit = json_string(json_object_by_path(object, {"unit"}));
|
||||
return unitConvertion::length(value, unit, output_unit);
|
||||
}
|
||||
|
||||
/*
|
||||
returns the value of a json-object, which should consist of a json-real with the key
|
||||
"value" and a json-string with the key "unit" transformed to the specified energy
|
||||
"output_unit" (supported units: "eV", "J"). If the key "unit" does not exist or is not
|
||||
a json-string an error will be thrown and the program will terminate. If the key "value"
|
||||
does not exist or not a json-real it will default to 0.0
|
||||
*/
|
||||
inline double json_read_energy(json_t* object, std::string output_unit = "eV") {
|
||||
double value = json_double(json_object_by_path(object, {"value"}));
|
||||
std::string unit = json_string(json_object_by_path(object, {"unit"}));
|
||||
|
||||
Reference in New Issue
Block a user