On this page
  1. Type
  2. Value
  3. list, object
  4. Reading: never throws, never changes anything
  5. as_bool, as_int, as_float
  6. as_string
  7. type
  8. size
  9. contains
  10. key
  11. Structure
  12. operator[]
  13. push
  14. erase
  15. version
  16. changed
  17. reload
  18. operator==
  19. Value::Ref
  20. get
  21. operator const Value &
  22. as_bool, as_int, as_float, as_string, type, size, contains, key
  23. operator[]
  24. operator=
  25. push, erase

rmp::Value

class #include <rmp/save.h> include/rmp/save.h:64
class 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

enum class 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.

ValueMeans
NONE
BOOL
NUMBER
STRING
LIST
OBJECT

Value

template <class B> requires std::is_same_v<B, bool> // NOLINTNEXTLINE(google-explicit-constructor): v["alive"] = true; a bool is a Value Value(B b)
template <class N> requires(std::is_arithmetic_v<N> && !std::is_same_v<N, bool>) // NOLINTNE
template <class S> requires(std::is_convertible_v<const S &, std::string_view> && !std::is_pointer_v<S> && !std::is_null_pointer_v<S>) // NOLINTNEXTLINE(goo

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

static Value list()
static Value object()
static function include/rmp/save.h:100

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

bool as_bool(bool fallback = false) const
int as_int(int fallback = 0) const
float as_float(float fallback = 0) const

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

std::string_view as_string(std::string_view fallback = "") const

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

Type type() const

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

int size() const

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

bool contains(std::string_view key) const

Whether this is an object with that key. False for anything else.

key

std::string_view key(int index) const

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[]

const Value &operator[](std::string_view key) const
const Value &operator[](int index) const
Ref operator[](std::string_view key)
Ref operator[](int index)

[] 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

void push(Value value)

Append. NONE becomes a list first; anything else that is not a list is left alone and said once.

erase

bool erase(std::string_view key)

Remove a key from an object. False when it was not there.

version

int version() const

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

bool changed() const

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

bool 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==

bool operator==(const Value &a, const Value &b)

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 read

To fill a whole subtree, build it and assign it: v["player"] = player;

get

const Value &get() const

Reading: the Value this names, or NONE where the path leads nowhere.

operator const Value &

operator const Value &() const

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

bool as_bool(bool fallback = false) const
int as_int(int fallback = 0) const
float as_float(float fallback = 0) const
std::string_view as_string(std::string_view fallback = "") const
Type type() const
int size() const
bool contains(std::string_view key) const
std::string_view key(int index) const

Value's reads, answered from get(): they create nothing.

operator[]

Ref operator[](std::string_view key) const
Ref operator[](int index) const

Deeper, still creating nothing.

operator=

Ref &operator=(Value value)

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

void push(Value value)
bool erase(std::string_view key)

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.