1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
# Literals
In many cases, when you have strings, you know in advance that these strings can only
assume a limited number of values. For reasons of efficiency and safety, it is a good
idea to use Literals instead of strings.
You can declare a Literal as follows:
```cpp
using MyLiteral = rfl::Literal<"option1", "option2", ...>;
```
You can construct literals as follows:
```cpp
const auto my_literal = MyLiteral::make<"option1">();
```
Literals that contain only one option also have a default constructor.
Literals can be used inside switches:
```cpp
switch(my_literal.value()) {
case MyLiteral::value_of<"option1">():
...
case MyLiteral::value_of<"option2">():
...
...
}
```
Literals are stored as `uint8_t` or `uint16_t` under-the-hood. But you can extract their name as a string using `.name()`
or the underlying value using `.value()`.
```cpp
using MyLiteral = rfl::Literal<"option1", "option2", ...>;
const auto my_literal = MyLiteral::make<"option1">();
// Prints "option1"
std::cout << my_literal.name() << std::endl;
// Prints "0"
std::cout << my_literal.value() << std::endl;
```
## Example
```cpp
// There are only five Simpsons.
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
// All Simpsons have the last name.
using LastName = rfl::Literal<"Simpson">;
struct Person {
rfl::Rename<"firstName", FirstName> first_name;
rfl::Rename<"lastName", LastName> last_name;
std::vector<Person> children;
};
// Leads to a runtime error, if the field "lastName" is not "Simpson"
// and the field "firstName" is not "Homer", "Marge", "Bart", "Lisa" or "Maggie".
const auto simpson_family_member = rfl::json::read<Person>(some_json_string).value();
```
|