Skip to content

Commit 400d78f

Browse files
committed
Document one use-case of rfl::num_fields
1 parent d0075f7 commit 400d78f

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

docs/custom_parser.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Custom parsers
22

3-
## `rfl::Reflector`
3+
## `rfl::Reflector`
44

5-
If you absolutely do not want to make any changes to your original classes whatsoever,
6-
You can create a Reflector template specialization for your type:
5+
If you absolutely do not want to (or are unable to) make any changes to your
6+
original classes whatsoever, you can create a Reflector template specialization
7+
for your type:
78

89
```cpp
910
namespace rfl {
@@ -13,7 +14,7 @@ struct Reflector<Person> {
1314
std::string first_name;
1415
std::string last_name;
1516
};
16-
17+
1718
static Person to(const ReflType& v) noexcept {
1819
return {v.first_name, v.last_name};
1920
}
@@ -25,7 +26,27 @@ struct Reflector<Person> {
2526
}
2627
```
2728
28-
It's also fine to define just the `from` method when the original class is
29+
One way to help make sure that your `ReflType` is kept up to date with your
30+
original class is to use the `rfl::num_fields<T>` utility to implement a compile-
31+
time assertion to verify that they have the same number of fields. The
32+
`rfl::num_fields<T>` utility can be used even in cases where the original
33+
class is too complex for `reflect-cpp`'s default reflection logic or
34+
`rfl::to_view()` to be able to handle.
35+
36+
```cpp
37+
namespace rfl {
38+
template <>
39+
struct Reflector<Person> {
40+
struct ReflType {
41+
std::string first_name;
42+
std::string last_name;
43+
};
44+
static_assert(rfl::num_fields<ReflType> == rfl::num_fields<Person>,
45+
"ReflType and actual type must have the same number of fields");
46+
// ...
47+
```
48+
49+
It's also fine to define just the `from` method when the original class is
2950
only written, or `to` when the original class is only read:
3051

3152
```cpp
@@ -46,7 +67,7 @@ struct Reflector<Person> {
4667
```
4768
4869
Note that the `ReflType` does not have to be a struct. For instance, if you have
49-
a custom type called `MyCustomType` that you want to be serialized as a string,
70+
a custom type called `MyCustomType` that you want to be serialized as a string,
5071
you can do the following:
5172
5273
```cpp
@@ -114,7 +135,7 @@ struct Person {
114135
};
115136
```
116137

117-
You can then write a helper struct:
138+
You can then write a helper struct:
118139

119140
```cpp
120141
struct PersonImpl {

docs/docs-readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929
[Standard containers](standard_containers.md) - Describes how reflect-cpp treats containers in the standard library.
3030

31-
[C arrays and inheritance](c_arrays_and_inheritance.md) - Describes how reflect-cpp handles C arrays and inheritance.
31+
[C arrays and inheritance](c_arrays_and_inheritance.md) - Describes how reflect-cpp handles C arrays and inheritance.
3232

33-
[rfl::Bytestring](bytestring.md) - Describes how reflect-cpp handles binary strings for formats that support them.
33+
[rfl::Bytestring](bytestring.md) - Describes how reflect-cpp handles binary strings for formats that support them.
3434

35-
[rfl::Binary, rfl::Hex and rfl::Oct](number_systems.md)- For expressing numbers in different formats.
35+
[rfl::Binary, rfl::Hex and rfl::Oct](number_systems.md)- For expressing numbers in different formats.
3636

3737
## Validation
3838

@@ -58,7 +58,7 @@
5858

5959
[Custom classes](concepts/custom_classes.md) - For custom classes with private fields.
6060

61-
[Custom parsers for your classes](custom_parser.md) - For custom classes with private fields that you want to leave absolutely untouched.
61+
[Custom parsers for your classes](custom_parser.md) - For custom classes with private fields that you want (or need) to leave absolutely untouched.
6262

6363
## Useful helper functions and classes
6464

0 commit comments

Comments
 (0)