You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/processors.md
+84Lines changed: 84 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,6 +35,7 @@ reflect-cpp currently supports the following processors:
35
35
36
36
-`rfl::AddStructName`
37
37
-`rfl::AddTagsToVariants`
38
+
-`rfl::AddNamespacedTagsToVariants`
38
39
-`rfl::AllowRawPtrs`
39
40
-`rfl::DefaultIfMissing`
40
41
-`rfl::NoExtraFields`
@@ -117,6 +118,89 @@ struct key_pressed_t {
117
118
Note that there are other ways to address problems like this, for instance `rfl::TaggedUnion`.
118
119
Please refer to the relevant sections of the documentation.
119
120
121
+
### `rfl::AddNamespacedTagsToVariants`
122
+
123
+
This processor is similar to `rfl::AddTagsToVariants`, but instead of using just the struct name as the tag, it uses the full namespaced type name. This is particularly useful when you have:
124
+
125
+
1. Structs with the same name in different namespaces
126
+
2. Structs with `rfl::Generic` fields that would otherwise create naming conflicts
127
+
128
+
#### Use-case: Structs with the same name in different namespaces
129
+
130
+
Consider this example where `rfl::AddTagsToVariants` would fail:
131
+
132
+
```cpp
133
+
namespaceResult {
134
+
struct Message {
135
+
std::string result;
136
+
};
137
+
}
138
+
139
+
namespace Error {
140
+
struct Message {
141
+
std::string error;
142
+
int error_id;
143
+
};
144
+
}
145
+
146
+
using Messages = std::variant<Result::Message, Error::Message>;
Copy file name to clipboardExpand all lines: docs/variants_and_tagged_unions.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,6 +57,44 @@ const auto r2 = rfl::json::read<Shapes, rfl::AddTagsToVariants>(json_string);
57
57
58
58
Please refer to the section on processors in this documentation for more information.
59
59
60
+
## Automatic tags with full namespaces
61
+
62
+
In some cases, you might have struct name conflicts between different namespaces, or types that would generate identical tags when namespaces are removed. For these situations, you can use `rfl::AddNamespacedTagsToVariants` instead:
63
+
64
+
```cpp
65
+
namespaceResult {
66
+
struct Message {
67
+
std::string result;
68
+
};
69
+
}
70
+
71
+
namespace Error {
72
+
struct Message {
73
+
std::string error;
74
+
int error_id;
75
+
};
76
+
}
77
+
78
+
using Messages = std::variant<Result::Message, Error::Message>;
const auto json_string = rfl::json::write<rfl::AddNamespacedTagsToVariants>(msg);
83
+
```
84
+
85
+
This generates JSON with fully qualified type names:
86
+
87
+
```json
88
+
{"Error::Message":{"error":"Something went wrong","error_id":404}}
89
+
```
90
+
91
+
The key differences between `rfl::AddTagsToVariants` and `rfl::AddNamespacedTagsToVariants`:
92
+
93
+
-`rfl::AddTagsToVariants` uses just the struct name (e.g., `"Message"`)
94
+
-`rfl::AddNamespacedTagsToVariants` uses the full namespaced name (e.g., `"Error::Message"`)
95
+
- Both respect custom tags defined with `using Tag = rfl::Literal<"custom_name">`
96
+
- Use the namespaced version when you are using `rfl::Generic` or need to distinguish between types in different namespaces and don't want to manually tag them as above
97
+
60
98
## `rfl::TaggedUnion` (internally tagged)
61
99
62
100
Another way to solve this problem is to add a tag inside the class. That is why we have provided a helper class for these purposes: `rfl::TaggedUnion`.
0 commit comments