Merge branch 'main' of github.com:getml/reflect-cpp

This commit is contained in:
Patrick Urbanke
2023-10-04 21:29:19 +02:00
4 changed files with 32 additions and 12 deletions
+8 -3
View File
@@ -9,7 +9,7 @@ all fields will be flattened to a single JSON object.
If there are duplicate field names, you will get a compile-time error.
## Example: Every Employee is a person.
## Example: Every employee is a person.
```cpp
struct Person {
@@ -28,7 +28,12 @@ const auto employee = Employee{
.salary = 60000.0
};
// Flattens all fields into a single JSON object:
// {"firstName":"Homer","lastName":"Simpson","age":45,"salary":60000.0}
const auto json_str = rfl::json::write(employee);
```
This flattens all fields into a single JSON object:
```json
{"firstName":"Homer","lastName":"Simpson","age":45,"salary":60000.0}
```
+4 -4
View File
@@ -22,18 +22,18 @@ Literals can be used inside switches:
```cpp
switch(my_literal.value()) {
case MyLiteral::value_of<"option1">:
case MyLiteral::value_of<"option1">():
...
case MyLiteral::value_of<"option2">:
case MyLiteral::value_of<"option2">():
...
...
}
```
Literals are stored as `uint8_t` or `uint16_t` under-the-hood. But you can extract their name using `.name()` as a string
or the underlying value using `.value`.
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", ...>;
+15
View File
@@ -70,3 +70,18 @@ And you can parse it back into a struct:
```cpp
const auto homer = rfl::json::read<Person>(json_string).value();
```
## Important note
**Do not create custom constructors on the structs.**
reflect-cpp needs to be able to create the structs like this:
```cpp
Person{rfl::Field<"firstName", std::string>("Bart"), rfl::Field<"lastName", std::string>("Simpson"), ...};
```
But if you create a custom constructor, then C++ will no longer allow this kind of constructions.
If you want to create the struct from one of your classes (the most like reason, you want to create custom constructors in the first place),
you might want to check out the section on [custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) or [custom parsers](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md).
+5 -5
View File
@@ -36,7 +36,7 @@ several problems with this:
2) It leads to confusing error messages: If none of the alternatives can be matched, you will get an error message telling you why each of the alternatives couldn't be matched. Such error messages are very long-winding and hard to read.
3) It is dangerous. Imagine we had written `std::variant<Circle, Square, Rectangle>` instead of `std::variant<Circle, Rectangle, Square>`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs.
## Tagging `std::variant` (externally tagged)
## `std::variant` (externally tagged)
From a functional programming point-of-view, the most straightforward way to handle these problems is to add tags.
@@ -136,12 +136,12 @@ const Shapes my_shape =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
const auto handle_shapes = [](const auto& field) {
using Type = typename std::decay_t(decltype(field))::Type;
if constexpr (std::is_same<Type, Circle>()) {
using Name = typename std::decay_t(decltype(field))::Name;
if constexpr (std::is_same<Name, rfl::Literal<"circle">>()) {
std::cout << is circle, radius: << field.value().radius() << std::endl;
} else if constexpr (std::is_same<Type, Rectangle>()) {
} else if constexpr (std::is_same<Name, rfl::Literal<"rectangle">>()) {
std::cout << is rectangle, width: << field.value().width() << ", height: " << field.value().height() << std::endl;
} else if constexpr (std::is_same<Type, Square>()) {
} else if constexpr (std::is_same<Name, rfl::Literal<"square">>()) {
std::cout << is square, width: << field.value().width() << std::endl;
} else {
// reflect-cpp also provides this very useful helper that ensures