File: object.md

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (65 lines) | stat: -rw-r--r-- 2,257 bytes parent folder | download | duplicates (2)
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
# `rfl::Object` 

`rfl::Object<...>` behaves similarly to `std::map<std::string, ...>` and `std::unordered_map<std::string, ...>`.

It will be represented in JSON or other formats as an object:

```cpp
auto bart = rfl::Object<std::string>();
bart["first_name"] = "Bart";
bart["last_name"] = "Simpson";
bart["town"] = "Springfield";
```

This is results in the following JSON string:

```cpp
{"first_name":"Bart","last_name":"Simpson","town":"Springfield"}
```

It supports almost all of the normal features you would expect from such a container. For instance, 
you can iterate through it just like any other map:

```cpp
for (const auto& [k, v]: bart) {
    std::cout << k << ": " << v << std::endl;
}
```

However, unlike these containers, the order of fields is preserved. It is also possible to have duplicate keys:


```cpp
auto bart = rfl::Object<std::string>();
bart["first_name"] = "Bart";
bart["last_name"] = "Simpson";
bart["town"] = "Springfield";

// Note that you need the .insert(...) method,
// because operator[] would overwrite the
// existing field.
bart.insert("first_name", "Lisa");
```

This results in the following JSON string:

```cpp
{"first_name":"Bart","last_name":"Simpson","town":"Springfield","first_name":"Lisa"}
```

There are three different ways of inserting fields:

- `operator[...]` creates a new field, if a field of this name doesn't already exist, and then assigns it.
- `.insert(...)` inserts a new field at the end, potentially creating duplicate field names. Much like `std::map`, it supports several types of inputs:
    - `.insert(std::pair(key, value))`
    - `.insert(key, value)`
    - `.insert(begin, end)`, where `begin` and `end` are iterators of a container of key-value-pairs
- `.emplace(...)` is an alias for `.insert` that exists primarily for reasons of compatability with standard containers.

There are three different ways of accessing fields:

- `operator[...]` creates a new field, if a field of this name doesn't already exist.
- `.at(...)` throws an exception, if field of this name doesn't exist.
- `.get(...)` returns an `rfl::Result` wrapping the field, or an `rfl::Error` if the field doesn't exist.

Note that it is most efficient, if you access fields in the order that they were placed.