Skip to content

ReadWrite variables

Julien SOYSOUVANH edited this page Oct 24, 2021 · 2 revisions

Index

All notions introduced in this section are pretty much applicable to fields and static fields as well.
For the example, we consider this reflected variable:

VARIABLE()
int variable;

Retrieve a variable

You can retrieve variables from the database by name, id or predicate.

rfk::Variable const* var = rfk::getDatabase().getFileLevelVariableByName("variable");

Read a variable value

//By copy
int valCopy = var.get<int>();

//By reference
int& valRef = var.get<int&>();

//By move
//variable content is moved to valMove, so variable is in an undefined state
int valMove = var.get<int&&>();

Write a variable value

//By copy
int const newValue = 42;
var.set(newValue);

//By rvalue
var.set(42);

WARNING: The set method uses template deduction to determine the type of the passed value. Sometimes, the template deduction will yield a different type than expected (for example const char (&)[] instead of std::string), in which case you should explicitely specify the template type.

var.set<int>(42);

Note: If you try to write into a const variable, a rfk::ConstViolation exception will be thrown.

Clone this wiki locally