rmp::Value
A tree: nothing, a bool, a number, a string, a list or an object — exactly what JSON holds, so a real structure fits and not only flat pairs.
Type
What a Value holds, one kind per JSON value. NONE is nothing: a missing key, an index out of range, a default-constructed Value, and JSON's null. BOOL is true or false, NUMBER a double, STRING text. LIST is values in order, and OBJECT is keys naming values, in the order they were added.
| Value | Means | |
|---|---|---|
NONE | ||
BOOL | ||
NUMBER | ||
STRING | ||
LIST | ||
OBJECT |
Value
A template so that only a real bool gets here: as a plain Value(bool), every pointer in the program would quietly convert to true.
list, object
An empty list or object, for when "nothing yet" has to be one of them: v["inventory"] = rmp::Value::list();
Reading: never throws, never changes anything
as_bool, as_int, as_float
The fallback when this is not of that type — a missing key is NONE. Types are not converted: a number is not a bool and "7" is not 7. as_int truncates towards zero and clamps to int's range; as_float clamps to float's, so a number in a save never reads as an infinity.
as_string
Valid while this Value is alive and unchanged — and adding to the object or list that holds it moves it. The fallback's lifetime is yours.
type
What this holds. Reading never needs it — a read of the wrong type gives its fallback — but walking a Value you did not write does.
size
Elements of a list, keys of an object; 0 for anything else. A member assigned NONE counts as absent, here and in key(), contains(), == and the file: nothing and missing read the same.
contains
Whether this is an object with that key. False for anything else.
key
The i-th key of an object, in the order the keys were added; "" out of range. With operator[](key) it is how to walk an object you did not write.
Structure
operator[]
[] on a const Value reads: a missing key, an index out of range, or a Value of another type is NONE.
[] on a non-const Value gives a Ref — a path into this Value that READS exactly like the const one, creating nothing, and WRITES by creating what is missing on the way: v["a"]["b"].as_int(1) leaves v as it was, v["a"]["b"] = 1 builds both objects. So reading a save never grows it, and a read never says anything. A write that cannot land — through a number read from an old save, at a negative index, past the end of a list (size() appends one) — goes nowhere and is said once: turning a number into an object behind your back would lose it.
push
Append. NONE becomes a list first; anything else that is not a list is left alone and said once.
erase
Remove a key from an object. False when it was not there.
version
The [save] version this came from, when rmp::save::read() filled it; a Value made in code is of this build's version. Not compared by ==.
changed
A Value read with rmp::assets::load_value() remembers its file in resources/: changed() is true once the file differs from what this Value holds — edited and saved while the game runs — and reload() reads it again into this Value, which keeps remembering it:
if (tuning.changed()) tuning.reload();
speed = tuning["speed"].as_float(200);Debug builds on the desktop only: a release reads its resources from the pack, which does not change, and everywhere else changed() is false. Cheap enough for every frame: changed() compares two numbers, and the file itself is looked at four times a second at most. A Value made in code, read from a save or taken out of another (tuning["enemy"]) has no file: changed() is false and reload() does nothing. Neither is compared by ==.
reload
Reads the file again into this Value; false, and this Value as it was, when it has no file or the file is not JSON now (half saved, say).
operator==
Same type and same contents, keys in any order. A true equality: a NaN equals a NaN, so a Value always equals itself and every copy of it.
Value::Ref
What [] on a non-const Value gives: the root it started from and the keys and indices it went through, looked up again on every use — so a Ref kept while other keys are added never points at moved memory. You do not name this type; you index, read and assign through it:
v["player"]["x"] = 3; // creates "player"
int x = v["player"]["x"].as_int(0); // creates nothing
const rmp::Value &p = v["player"]; // the Value itself, to readTo fill a whole subtree, build it and assign it: v["player"] = player;
get
Reading: the Value this names, or NONE where the path leads nowhere.
operator const Value &
The Value it names, wherever a const rmp::Value & is wanted: a Ref passes where a Value is read.
as_bool, as_int, as_float, as_string, type, size, contains, key
Value's reads, answered from get(): they create nothing.
operator[]
Deeper, still creating nothing.
operator=
Writing: the path is created, then assigned. A Ref assigned from a Ref copies the VALUE, never the path — v["a"] = v["b"] copies b into a.
push, erase
Value's push() and erase() on the Value this names. push() creates the path first, like a write; erase() creates nothing and is false when the path leads nowhere.