mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
This commit is contained in:
@@ -60,6 +60,8 @@
|
||||
|
||||
5.3) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
|
||||
|
||||
5.4) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
|
||||
|
||||
## 6) Advanced topics
|
||||
|
||||
6.1) [Supporting your own format](https://github.com/getml/reflect-cpp/blob/main/docs/supporting_your_own_format.md) - For supporting your own serialization and deserialization formats.
|
||||
|
||||
@@ -73,29 +73,82 @@ struct Writer {
|
||||
using OutputObjectType = ...;
|
||||
using OutputVarType = ...;
|
||||
|
||||
/// Appends `_var` to the end of `_arr`, thus mutating it.
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {...}
|
||||
/// Sets an empty array as the root element of the document.
|
||||
/// Some serialization formats require you to pass the expected size in
|
||||
/// advance. If you are not working with such a format, you can ignore the
|
||||
/// parameter `_size`. Returns the new array for further modification.
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept;
|
||||
|
||||
/// Returns an empty OutputVarType, the NULL type of the format.
|
||||
OutputVarType empty_var() const noexcept {...}
|
||||
/// Sets an empty object as the root element of the document.
|
||||
/// Some serialization formats require you to pass the expected size in
|
||||
/// advance. If you are not working with such a format, you can ignore the
|
||||
/// parameter `_size`.
|
||||
/// Returns the new object for further modification.
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept;
|
||||
|
||||
/// Generates an OutputVarType from a basic type
|
||||
/// (std::string, bool, floating point or integral).
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {...}
|
||||
/// Sets a null as the root element of the document. Returns OutputVarType
|
||||
/// containing the null value.
|
||||
OutputVarType null_as_root() const noexcept;
|
||||
|
||||
/// Generates a new, empty array.
|
||||
OutputArrayType new_array() const noexcept {...}
|
||||
/// Sets a basic value (bool, numeric, string) as the root element of the
|
||||
/// document. Returns an OutputVarType containing the new value.
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept;
|
||||
|
||||
/// Generates a new, empty object.
|
||||
OutputObjectType new_object() const noexcept {...}
|
||||
/// Adds an empty array to an existing array. Returns the new
|
||||
/// array for further modification.
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
/// Determines whether the var is empty (whether it is the NULL type).
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {...}
|
||||
/// Adds an empty array to an existing object. The key or name of the field is
|
||||
/// signified by `_name`. Returns the new array for further modification.
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
/// Adds a new field to obj, thus mutating it.
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {...}
|
||||
/// Adds an empty object to an existing array. Returns the new
|
||||
/// object for further modification.
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept;
|
||||
|
||||
/// Adds an empty object to an existing object. The key or name of the field
|
||||
/// is signified by `_name`. Returns the new object for further modification.
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
/// Adds a basic value (bool, numeric, string) to an array. Returns an
|
||||
/// OutputVarType containing the new value.
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept;
|
||||
|
||||
/// Adds a basic value (bool, numeric, string) to an existing object. The key
|
||||
/// or name of the field is signified by `name`. Returns an
|
||||
/// OutputVarType containing the new value.
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
/// Adds a null value to an array. Returns an
|
||||
/// OutputVarType containing the null value.
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept;
|
||||
|
||||
/// Adds a null value to an existing object. The key
|
||||
/// or name of the field is signified by `name`. Returns an
|
||||
/// OutputVarType containing the null value.
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept;
|
||||
|
||||
/// Signifies to the writer that we do not want to add any further elements to
|
||||
/// this array. Some serialization formats require this. If you are working
|
||||
/// with a serialization format that doesn't, just leave the function empty.
|
||||
void end_array(OutputArrayType* _arr) const noexcept;
|
||||
|
||||
/// Signifies to the writer that we do not want to add any further elements to
|
||||
/// this object. Some serialization formats require this. If you are working
|
||||
/// with a serialization format that doesn't, just leave the function empty.
|
||||
void end_object(OutputObjectType* _obj) const noexcept;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -145,12 +198,12 @@ struct Reader {
|
||||
/// Returns an rfl::Error if `_var` cannot be cast as an array.
|
||||
rfl::Result<InputArrayType> to_array(const InputVarType& _var) const noexcept {...}
|
||||
|
||||
/// fct is a function that turns the field name into the field index of the
|
||||
/// _fct is a function that turns the field name into the field index of the
|
||||
/// struct. It returns -1, if the fields does not exist on the struct. This
|
||||
/// returns an std::array that can be used to build up the struct.
|
||||
/// See below for a more comprehensive explanation.
|
||||
template <size_t size, class FunctionType>
|
||||
std::array<InputVarType, size> to_fields_array(
|
||||
std::array<std::optional<InputVarType>, size> to_fields_array(
|
||||
const FunctionType _fct, const InputObjectType& _obj) const noexcept {...}
|
||||
|
||||
/// Iterates through an object and writes the contained key-value pairs into
|
||||
@@ -187,15 +240,15 @@ Consider the following struct:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
This struct contains four fields. `rfl::parsing::Parser` expects `to_fields_array`
|
||||
to return an `std::array<InputVarType, 4>` containing the field "firstName" in the
|
||||
to return an `std::array<std::optional<InputVarType>, 4>` containing the field "firstName" in the
|
||||
first position, "lastName" in the second position, "birthday" in the third position
|
||||
and "children" in the fourth position.
|
||||
|
||||
@@ -214,6 +267,4 @@ Your job is to implement the following:
|
||||
1. Iterate through `_obj`.
|
||||
2. Identify the required index of the field name using `_fct`.
|
||||
3. Set the corresponding field in `std::array` to the field value associated with the field name.
|
||||
4. Any field that could not be set in steps 1-3 must be set to the NULL value,
|
||||
such that `Reader.is_empty(...)` would return `true`.
|
||||
|
||||
4. Any field that could not be set in steps 1-3 must be set to `std::nullopt`.
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
# YAML
|
||||
|
||||
For YAML support, you must also include the header `<rfl/yaml.hpp>` and include the yaml-cpp library (https://github.com/jbeder/yaml-cpp) in your project.
|
||||
|
||||
## Reading and writing
|
||||
|
||||
Suppose you have a struct like this:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
```
|
||||
|
||||
You can parse YAML strings like this:
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::yaml::read<Person>(yaml_string);
|
||||
```
|
||||
|
||||
A person can be turned into a YAML string like this:
|
||||
|
||||
```cpp
|
||||
const auto person = Person{...};
|
||||
const std::string yaml_string = rfl::yaml::write(person);
|
||||
```
|
||||
|
||||
## Loading and saving
|
||||
|
||||
You can also load and save to disc using a very similar syntax:
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::yaml::load<Person>("/path/to/file.yml");
|
||||
|
||||
const auto person = Person{...};
|
||||
rfl::yaml::save("/path/to/file.yml", person);
|
||||
```
|
||||
|
||||
## Reading from and writing into streams
|
||||
|
||||
You can also read from and write into any `std::istream` and `std::ostream` respectively.
|
||||
|
||||
```cpp
|
||||
const rfl::Result<Person> result = rfl::yaml::read<Person>(my_istream);
|
||||
|
||||
const auto person = Person{...};
|
||||
rfl::yaml::write(person, my_ostream);
|
||||
```
|
||||
|
||||
Note that `std::cout` is also an ostream, so this works as well:
|
||||
|
||||
```cpp
|
||||
rfl::yaml::write(person, std::cout) << std::endl;
|
||||
```
|
||||
|
||||
## Custom constructors
|
||||
|
||||
One of the great things about C++ is that it gives you control over
|
||||
when and how you code is compiled.
|
||||
|
||||
For large and complex systems of structs, it is often a good idea to split up
|
||||
your code into smaller compilation units. You can do so using custom constructors.
|
||||
|
||||
For the YAML format, these must be a static function on your struct or class called
|
||||
`from_yaml_obj` that take a `rfl::yaml::Reader::InputVarType` as input and return
|
||||
the class or the class wrapped in `rfl::Result`.
|
||||
|
||||
In your header file you can write something like this:
|
||||
|
||||
```cpp
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
|
||||
using YAMLVar = typename rfl::yaml::Reader::InputVarType;
|
||||
static rfl::Result<Person> from_yaml_obj(const YAMLVar& _obj);
|
||||
};
|
||||
```
|
||||
|
||||
And in your source file, you implement `from_yaml_obj` as follows:
|
||||
|
||||
```cpp
|
||||
rfl::Result<Person> Person::from_yaml_obj(const YAMLVar& _obj) {
|
||||
const auto from_nt = [](auto&& _nt) {
|
||||
return rfl::from_named_tuple<Person>(std::move(_nt));
|
||||
};
|
||||
return rfl::yaml::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt);
|
||||
}
|
||||
```
|
||||
|
||||
This will force the compiler to only compile the YAML parsing when the
|
||||
source file is compiled.
|
||||
|
||||
Reference in New Issue
Block a user