diff --git a/docs/flatten_structs.md b/docs/flatten_structs.md index c1827b9d..a13ad43d 100644 --- a/docs/flatten_structs.md +++ b/docs/flatten_structs.md @@ -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} +``` + diff --git a/docs/literals.md b/docs/literals.md index 7d047607..6671a490 100644 --- a/docs/literals.md +++ b/docs/literals.md @@ -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", ...>; diff --git a/docs/structs.md b/docs/structs.md index cc4ab153..09e17399 100644 --- a/docs/structs.md +++ b/docs/structs.md @@ -70,3 +70,18 @@ And you can parse it back into a struct: ```cpp const auto homer = rfl::json::read(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). diff --git a/docs/variants_and_tagged_unions.md b/docs/variants_and_tagged_unions.md index d00a1545..a0d25d41 100644 --- a/docs/variants_and_tagged_unions.md +++ b/docs/variants_and_tagged_unions.md @@ -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` instead of `std::variant`. 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()) { + using Name = typename std::decay_t(decltype(field))::Name; + if constexpr (std::is_same>()) { std::cout << is circle, radius: << field.value().radius() << std::endl; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same>()) { std::cout << is rectangle, width: << field.value().width() << ", height: " << field.value().height() << std::endl; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same>()) { std::cout << is square, width: << field.value().width() << std::endl; } else { // reflect-cpp also provides this very useful helper that ensures