mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
This commit is contained in:
@@ -18,11 +18,12 @@ jobs:
|
||||
- name: Run test
|
||||
run: |
|
||||
g++ --version
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j 4
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/xml/reflect-cpp-xml-tests
|
||||
./build/tests/yaml/reflect-cpp-yaml-tests
|
||||
|
||||
linux-clang:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -46,11 +47,12 @@ jobs:
|
||||
CXX: clang++
|
||||
run: |
|
||||
clang --version
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j 4
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/xml/reflect-cpp-xml-tests
|
||||
./build/tests/yaml/reflect-cpp-yaml-tests
|
||||
|
||||
|
||||
# The latest MSVC version on GitHub Actions has a bug, and it's difficult to switch to another version.
|
||||
|
||||
@@ -44,5 +44,7 @@
|
||||
*.json
|
||||
*.fb
|
||||
*.xml
|
||||
*.yml
|
||||
*.yaml
|
||||
|
||||
!vcpkg.json
|
||||
|
||||
+7
-1
@@ -3,11 +3,12 @@ cmake_minimum_required(VERSION 3.15)
|
||||
option(REFLECTCPP_BUILD_SHARED "Build shared library" OFF)
|
||||
option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF)
|
||||
option(REFLECTCPP_XML "Enable XML support" OFF)
|
||||
option(REFLECTCPP_YAML "Enable YAML support" OFF)
|
||||
|
||||
option(REFLECTCPP_BUILD_TESTS "Build tests" OFF)
|
||||
|
||||
# enable vcpkg if require features other than JSON
|
||||
if (REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML)
|
||||
if (REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML)
|
||||
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")
|
||||
endif ()
|
||||
|
||||
@@ -33,6 +34,11 @@ if (REFLECTCPP_XML)
|
||||
target_link_libraries(reflectcpp INTERFACE pugixml::pugixml)
|
||||
endif ()
|
||||
|
||||
if (REFLECTCPP_YAML)
|
||||
find_package(yaml-cpp CONFIG REQUIRED)
|
||||
target_link_libraries(reflectcpp INTERFACE yaml-cpp::yaml-cpp)
|
||||
endif ()
|
||||
|
||||
target_compile_options(reflectcpp PRIVATE -Wall)
|
||||
|
||||
if (REFLECTCPP_BUILD_TESTS)
|
||||
|
||||
@@ -23,20 +23,22 @@ Design principles for reflect-cpp include:
|
||||
- Simple extendability to custom classes
|
||||
- Standard C++ only, no compiler-specific macros
|
||||
|
||||
## Why do we need this?
|
||||
## Serialization formats
|
||||
|
||||
Suppose your C++ program has complex data structures it needs to save and load. Or maybe it needs to interact with some kind of external API. If you do this the traditional way, you will have a lot of boilerplate code. This is annoying and error-prone.
|
||||
reflect-cpp provides a unified reflection-based interface across different serialization formats. It is deliberately designed in a very modular way, using [concepts](https://en.cppreference.com/w/cpp/language/constraints), to make it as easy as possible to interface various C or C++ libraries related to serialization. Refer to the [documentation](https://github.com/getml/reflect-cpp/tree/main/docs) for details.
|
||||
|
||||
reflect-cpp is not just a reflection library, it is for **serialization, deserialization and validation** through reflection.
|
||||
The following table lists the serialization formats currently supported by reflect-cpp and the underlying libraries used:
|
||||
|
||||
That means that you can encode your requirements about the input data in the type system and have them validated upfront. This is why the library also includes algebraic data types like tagged unions and numerous validation routines. Having your requirements encoded in the type system is the most reliable way of ensuring they are met. If your requirements are not met, the user of your software gets a very clear error message. Encoding your requirements in the type system also makes it a lot easier for anyone reading your code.
|
||||
| Format | Library | Version | License | Remarks |
|
||||
|--------------|------------------------------------------------------|--------------|------------| -----------------------------------------------------|
|
||||
| JSON | [YYJSON](https://github.com/ibireme/yyjson) | >= 0.8.0 | MIT | out-of-the-box support, included in this repository |
|
||||
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | |
|
||||
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | |
|
||||
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | |
|
||||
|
||||
This increases user experience and developer experience, it makes your code safer (fewer bugs) and more secure (less prone to malicious attacks).
|
||||
Support for more serialization formats is in development. Refer to the [issues](https://github.com/getml/reflect-cpp/issues) for details.
|
||||
|
||||
For a more in-depth theoretical discussions of these topics, the following books are warmly recommended:
|
||||
|
||||
- *Category Theory for Programmers* by Bartosz Milewski (https://github.com/hmemcpy/milewski-ctfp-pdf/releases)
|
||||
- *Domain Modeling Made Functional* by Scott Wlaschin
|
||||
Please also refer to the *vcpkg.json* in this repository.
|
||||
|
||||
## Simple Example
|
||||
|
||||
@@ -66,6 +68,26 @@ The resulting JSON string looks like this:
|
||||
{"first_name":"Homer","last_name":"Simpson","age":45}
|
||||
```
|
||||
|
||||
Or you can use another format, such as YAML. This will work for just about
|
||||
any example in the entire documentation or any supported format.
|
||||
|
||||
```cpp
|
||||
#include <rfl/yaml.hpp>
|
||||
|
||||
// ... (same as above)
|
||||
|
||||
const std::string yaml_string = rfl::yaml::write(homer);
|
||||
auto homer2 = rfl::yaml::read<Person>(yaml_string).value();
|
||||
```
|
||||
|
||||
The resulting YAML string looks like this:
|
||||
|
||||
```yaml
|
||||
first_name: Homer
|
||||
last_name: Simpson
|
||||
age: 45
|
||||
```
|
||||
|
||||
## More Comprehensive Example
|
||||
|
||||
```cpp
|
||||
@@ -361,19 +383,20 @@ In addition, it supports the following custom containers:
|
||||
|
||||
Finally, it is very easy to extend full support to your own classes, refer to the [documentation](https://github.com/getml/reflect-cpp/tree/main/docs) for details.
|
||||
|
||||
## Serialization formats
|
||||
## Why do we need this?
|
||||
|
||||
reflect-cpp is deliberately designed in a very modular way, using [concepts](https://en.cppreference.com/w/cpp/language/constraints), to make it as easy as possible to interface C or C++ libraries for various serialization formats. Refer to the [documentation](https://github.com/getml/reflect-cpp/tree/main/docs) for details. PRs related to serialization formats are welcome.
|
||||
Suppose your C++ program has complex data structures it needs to save and load. Or maybe it needs to interact with some kind of external API. If you do this the traditional way, you will have a lot of boilerplate code. This is annoying and error-prone.
|
||||
|
||||
The following table lists the serialization formats currently supported by reflect-cpp and the underlying libraries used:
|
||||
reflect-cpp is not just a reflection library, it is for **serialization, deserialization and validation** through reflection.
|
||||
|
||||
| Format | Library | Version | License | Remarks |
|
||||
|--------------|------------------------------------------------------|--------------|------------| -----------------------------------------------------|
|
||||
| JSON | [YYJSON](https://github.com/ibireme/yyjson) | >= 0.8.0 | MIT | out-of-the-box support, included in this repository |
|
||||
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26#1 | Apache 2.0 | |
|
||||
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | |
|
||||
That means that you can encode your requirements about the input data in the type system and have them validated upfront. This is why the library also includes algebraic data types like tagged unions and numerous validation routines. Having your requirements encoded in the type system is the most reliable way of ensuring they are met. If your requirements are not met, the user of your software gets a very clear error message. Encoding your requirements in the type system also makes it a lot easier for anyone reading your code.
|
||||
|
||||
Please also refer to the *vcpkg.json* in this repository.
|
||||
This increases user experience and developer experience, it makes your code safer (fewer bugs) and more secure (less prone to malicious attacks).
|
||||
|
||||
For a more in-depth theoretical discussions of these topics, the following books are warmly recommended:
|
||||
|
||||
- *Category Theory for Programmers* by Bartosz Milewski (https://github.com/hmemcpy/milewski-ctfp-pdf/releases)
|
||||
- *Domain Modeling Made Functional* by Scott Wlaschin
|
||||
|
||||
## Documentation
|
||||
|
||||
@@ -427,6 +450,7 @@ add_subdirectory(reflect-cpp) # Add this project as a subdirectory
|
||||
|
||||
set(REFLECTCPP_FLEXBUFFERS ON) # Optional
|
||||
set(REFLECTCPP_XML ON) # Optional
|
||||
set(REFLECTCPP_YAML ON) # Optional
|
||||
|
||||
target_link_libraries(your_project PRIVATE reflectcpp) # Link against the library
|
||||
```
|
||||
@@ -470,7 +494,7 @@ git submodule update --init
|
||||
./vcpkg/bootstrap-vcpkg.bat # Windows
|
||||
# You may be prompted to install additional dependencies.
|
||||
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
|
||||
cmake --build build -j 4 # gcc, clang
|
||||
cmake --build build --config Release -j 4 # MSVC
|
||||
```
|
||||
@@ -481,6 +505,7 @@ To run the tests, do the following:
|
||||
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
|
||||
./build/tests/json/reflect-cpp-json-tests
|
||||
./build/tests/xml/reflect-cpp-xml-tests
|
||||
./build/tests/xml/reflect-cpp-yaml-tests
|
||||
```
|
||||
|
||||
## How to contribute
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTARRAY_HPP_
|
||||
#define FLEXBUF_OUTPUTARRAY_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputArray : public OutputVar {
|
||||
public:
|
||||
OutputArray() {}
|
||||
|
||||
~OutputArray() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
/// We have to catch the edge case of an empty vector,
|
||||
/// because flexbuf ignores empty vectors/maps.
|
||||
if (vars_.size() == 0) {
|
||||
if (_key) {
|
||||
_fbb->Null(_key->c_str());
|
||||
} else {
|
||||
_fbb->Null();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const auto start =
|
||||
_key ? _fbb->StartVector(_key->c_str()) : _fbb->StartVector();
|
||||
|
||||
for (const auto& var : vars_) {
|
||||
var->insert(std::nullopt, _fbb);
|
||||
}
|
||||
|
||||
_fbb->EndVector(start, false, false);
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
/// Adds a new element to the vector.
|
||||
void push_back(const Ref<OutputVar>& _var) { vars_.push_back(_var); }
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<Ref<OutputVar>> vars_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,38 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTNULL_HPP_
|
||||
#define FLEXBUF_OUTPUTNULL_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputNull : public OutputVar {
|
||||
public:
|
||||
OutputNull() {}
|
||||
|
||||
~OutputNull() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
if (_key) {
|
||||
_fbb->Null(_key->c_str());
|
||||
} else {
|
||||
_fbb->Null();
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return true; }
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,61 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTOBJECT_HPP_
|
||||
#define FLEXBUF_OUTPUTOBJECT_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputObject : public OutputVar {
|
||||
public:
|
||||
OutputObject() {}
|
||||
|
||||
~OutputObject() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
/// We have to catch the edge case of an empty map,
|
||||
/// because flexbuf ignores empty vectors/maps.
|
||||
if (vars_.size() == 0) {
|
||||
if (_key) {
|
||||
_fbb->Null(_key->c_str());
|
||||
} else {
|
||||
_fbb->Null();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const auto start = _key ? _fbb->StartMap(_key->c_str()) : _fbb->StartMap();
|
||||
|
||||
for (const auto& [name, elem] : vars_) {
|
||||
elem->insert(name, _fbb);
|
||||
}
|
||||
|
||||
_fbb->EndMap(start);
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
/// Adds a new element to the vector.
|
||||
void push_back(const std::string& _name, const Ref<OutputVar>& _var) {
|
||||
vars_.push_back(std::make_pair(_name, _var));
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<std::pair<std::string, Ref<OutputVar>>> vars_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,67 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTVALUE_HPP_
|
||||
#define FLEXBUF_OUTPUTVALUE_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class T>
|
||||
class OutputValue : public OutputVar {
|
||||
public:
|
||||
OutputValue(T _val) : val_(_val) {}
|
||||
|
||||
~OutputValue() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if (_key) {
|
||||
_fbb->String(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->String(val_);
|
||||
}
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
if (_key) {
|
||||
_fbb->Bool(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Bool(val_);
|
||||
}
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
if (_key) {
|
||||
_fbb->Double(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Double(val_);
|
||||
}
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
if (_key) {
|
||||
_fbb->Int(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Int(val_);
|
||||
}
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
T val_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,27 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTVAR_HPP_
|
||||
#define FLEXBUF_OUTPUTVAR_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputVar {
|
||||
public:
|
||||
virtual ~OutputVar() = default;
|
||||
|
||||
/// Inserts the value into the buffer.
|
||||
virtual void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) = 0;
|
||||
|
||||
/// Whether this is null.
|
||||
virtual bool is_null() const = 0;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef FLEXBUF_PARSER_HPP_
|
||||
#define FLEXBUF_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
#include "../parsing/Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
@@ -105,10 +105,6 @@ struct Reader {
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
// Necessary, because we write empty vectors as null.
|
||||
if (_var.IsNull()) {
|
||||
return flexbuffers::Vector::EmptyVector();
|
||||
}
|
||||
if (!_var.IsVector()) {
|
||||
return rfl::Error("Could not cast to Vector.");
|
||||
}
|
||||
@@ -133,10 +129,6 @@ struct Reader {
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
// Necessary, because we write empty maps as null.
|
||||
if (_var.IsNull()) {
|
||||
return flexbuffers::Map::EmptyMap();
|
||||
}
|
||||
if (!_var.IsMap()) {
|
||||
return rfl::Error("Could not cast to Map!");
|
||||
}
|
||||
|
||||
+130
-24
@@ -16,51 +16,157 @@
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "OutputArray.hpp"
|
||||
#include "OutputNull.hpp"
|
||||
#include "OutputObject.hpp"
|
||||
#include "OutputValue.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
struct Writer {
|
||||
using OutputArrayType = Ref<OutputArray>;
|
||||
using OutputObjectType = Ref<OutputObject>;
|
||||
using OutputVarType = Ref<OutputVar>;
|
||||
struct OutputArray {
|
||||
size_t start_;
|
||||
};
|
||||
|
||||
Writer() {}
|
||||
struct OutputObject {
|
||||
size_t start_;
|
||||
};
|
||||
|
||||
struct OutputVar {};
|
||||
|
||||
using OutputArrayType = OutputArray;
|
||||
using OutputObjectType = OutputObject;
|
||||
using OutputVarType = OutputVar;
|
||||
|
||||
Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType& _var, OutputArrayType* _arr) const noexcept {
|
||||
(*_arr)->push_back(_var);
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept { return Ref<OutputNull>::make(); }
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
return Ref<OutputValue<T>>::make(_var);
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
fbb_->Null(_name.c_str());
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
fbb_->EndVector(_arr->start_, false, false);
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
fbb_->EndMap(_obj->start_);
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string& _name,
|
||||
const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
fbb_->String(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
fbb_->Bool(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
fbb_->Double(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
fbb_->Int(_name.c_str(), _var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
fbb_->String(_var);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
fbb_->Bool(_var);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
fbb_->Double(_var);
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
fbb_->Int(_var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string& _name) const noexcept {
|
||||
const auto start = fbb_->StartVector(_name.c_str());
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return Ref<OutputArray>::make();
|
||||
const auto start = fbb_->StartVector();
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const std::string& _name) const noexcept {
|
||||
const auto start = fbb_->StartMap(_name.c_str());
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return Ref<OutputObject>::make();
|
||||
const auto start = fbb_->StartMap();
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return _var->is_null();
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
(*_obj)->push_back(_name, _var);
|
||||
}
|
||||
private:
|
||||
Ref<flexbuffers::Builder> fbb_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -15,12 +17,12 @@ namespace flexbuf {
|
||||
|
||||
template <class T>
|
||||
std::vector<uint8_t> to_buffer(const T& _obj) {
|
||||
auto w = Writer();
|
||||
const auto flexbuf_obj = Parser<T>::write(w, _obj);
|
||||
flexbuffers::Builder fbb;
|
||||
flexbuf_obj->insert(std::nullopt, &fbb);
|
||||
fbb.Finish();
|
||||
return fbb.GetBuffer();
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto fbb = Ref<flexbuffers::Builder>::make();
|
||||
auto w = Writer(fbb);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
fbb->Finish();
|
||||
return fbb->GetBuffer();
|
||||
}
|
||||
|
||||
/// Writes an object to flexbuf.
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -42,24 +43,10 @@ struct Reader {
|
||||
using InputObjectType = YYJSONInputObject;
|
||||
using InputVarType = YYJSONInputVar;
|
||||
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_json_obj<
|
||||
T,
|
||||
std::enable_if_t<std::is_invocable_r<
|
||||
T, decltype(T::from_json_obj), typename Reader::InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_json_obj<T, std::enable_if_t<std::is_invocable_r<
|
||||
rfl::Result<T>, decltype(T::from_json_obj),
|
||||
typename Reader::InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = has_from_json_obj<T>::value;
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_json_obj(var);
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
|
||||
+92
-23
@@ -18,7 +18,8 @@
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
struct Writer {
|
||||
class Writer {
|
||||
public:
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
@@ -47,14 +48,99 @@ struct Writer {
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_doc_set_root(doc_, val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_arr_add_val(_parent->val_, val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
@@ -70,24 +156,7 @@ struct Writer {
|
||||
}
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
public:
|
||||
yyjson_mut_doc* doc_;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -15,9 +16,9 @@ namespace json {
|
||||
/// Returns a JSON string.
|
||||
template <class T>
|
||||
std::string write(const T& _obj) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
@@ -28,9 +29,9 @@ std::string write(const T& _obj) {
|
||||
/// Writes a JSON into an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
_stream << json_c_str;
|
||||
free((void*)json_c_str);
|
||||
|
||||
@@ -14,31 +14,33 @@ namespace parsing {
|
||||
template <class ReaderType, class WriterType, class OriginalClass,
|
||||
class HelperStruct>
|
||||
struct CustomParser {
|
||||
static Result<OriginalClass> read(const ReaderType& _r,
|
||||
const auto& _var) noexcept {
|
||||
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
|
||||
try {
|
||||
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
|
||||
return _h.to_class();
|
||||
} else {
|
||||
auto ptr_field_tuple = internal::to_ptr_field_tuple(_h);
|
||||
const auto class_from_ptrs = [](auto&... _ptrs) {
|
||||
return OriginalClass(std::move(*_ptrs.value_)...);
|
||||
};
|
||||
return std::apply(class_from_ptrs, ptr_field_tuple);
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
return Parser<ReaderType, WriterType, HelperStruct>::read(_r, _var)
|
||||
.and_then(to_class);
|
||||
}
|
||||
static Result<OriginalClass> read(const ReaderType& _r,
|
||||
const auto& _var) noexcept {
|
||||
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
|
||||
try {
|
||||
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
|
||||
return _h.to_class();
|
||||
} else {
|
||||
auto ptr_field_tuple = internal::to_ptr_field_tuple(_h);
|
||||
const auto class_from_ptrs = [](auto&... _ptrs) {
|
||||
return OriginalClass(std::move(*_ptrs.value_)...);
|
||||
};
|
||||
return std::apply(class_from_ptrs, ptr_field_tuple);
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
return Parser<ReaderType, WriterType, HelperStruct>::read(_r, _var)
|
||||
.and_then(to_class);
|
||||
}
|
||||
|
||||
static auto write(const WriterType& _w, const OriginalClass& _p) noexcept {
|
||||
return Parser<ReaderType, WriterType, HelperStruct>::write(
|
||||
_w, HelperStruct::from_class(_p));
|
||||
}
|
||||
template <class P>
|
||||
static auto write(const WriterType& _w, const OriginalClass& _p,
|
||||
const P& _parent) noexcept {
|
||||
Parser<ReaderType, WriterType, HelperStruct>::write(
|
||||
_w, HelperStruct::from_class(_p), _parent);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -51,8 +51,9 @@ struct FieldVariantParser {
|
||||
return _r.to_object(_var).transform(to_map).and_then(to_result);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::variant<FieldTypes...>& _v) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::variant<FieldTypes...>& _v,
|
||||
const P& _parent) noexcept {
|
||||
static_assert(
|
||||
internal::no_duplicate_field_names<std::tuple<FieldTypes...>>(),
|
||||
"Externally tagged variants cannot have duplicate field "
|
||||
@@ -61,10 +62,10 @@ struct FieldVariantParser {
|
||||
const auto handle = [&](const auto& _field) {
|
||||
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
|
||||
using NamedTupleType = std::decay_t<decltype(named_tuple)>;
|
||||
return Parser<R, W, NamedTupleType>::write(_w, named_tuple);
|
||||
Parser<R, W, NamedTupleType>::write(_w, named_tuple, _parent);
|
||||
};
|
||||
|
||||
return std::visit(handle, _v);
|
||||
std::visit(handle, _v);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -43,7 +43,7 @@ concept IsReader = requires(R r, std::string name,
|
||||
/// floating point, std::string)
|
||||
{ r.template to_basic_type<T>(var) } -> std::same_as<rfl::Result<T>>;
|
||||
|
||||
/// 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.
|
||||
{
|
||||
|
||||
@@ -9,39 +9,88 @@ namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class W, class T>
|
||||
concept IsWriter = requires(W w, T t, std::string name,
|
||||
concept IsWriter = requires(W w, T t, std::string name, std::string basic_value,
|
||||
typename W::OutputArrayType arr,
|
||||
typename W::OutputObjectType obj,
|
||||
typename W::OutputVarType var) {
|
||||
/// Any Writer needs to define the following:
|
||||
///
|
||||
/// 1) An OutputArrayType, which must be an array-like data structure.
|
||||
/// 2) An OutputObjectType, which must contain key-value pairs.
|
||||
/// 3) An OutputVarType, which must be able to represent either
|
||||
/// OutputArrayType, OutputObjectType or a basic type (bool, integral,
|
||||
/// floating point, std::string).
|
||||
typename W::OutputVarType var, size_t size) {
|
||||
/// 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.
|
||||
{ w.array_as_root(size) } -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Appends var to the end of arr, thus mutating it.
|
||||
{ w.add(var, &arr) } -> std::same_as<void>;
|
||||
/// 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.
|
||||
{ w.object_as_root(size) } -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
/// Returns an empty var (usually the NULL type).
|
||||
{ w.empty_var() } -> std::same_as<typename W::OutputVarType>;
|
||||
/// Sets a null as the root element of the document. Returns OutputVarType
|
||||
/// containing the null value.
|
||||
{ w.null_as_root() } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Generates a var from a basic type (std::string, bool, floating point or
|
||||
/// integral).
|
||||
{ w.from_basic_type(t) } -> std::same_as<typename W::OutputVarType>;
|
||||
/// Sets a basic value (bool, numeric, string) as the root element of the
|
||||
/// document. Returns an OutputVarType containing the new value.
|
||||
{ w.value_as_root(basic_value) } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Generates a new, empty array.
|
||||
{ w.new_array() } -> std::same_as<typename W::OutputArrayType>;
|
||||
/// Adds an empty array to an existing array. Returns the new
|
||||
/// array for further modification.
|
||||
{
|
||||
w.add_array_to_array(size, &arr)
|
||||
} -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Generates a new, empty object.
|
||||
{ w.new_object() } -> std::same_as<typename W::OutputObjectType>;
|
||||
/// Adds an empty object to an existing array. Returns the new
|
||||
/// object for further modification.
|
||||
{
|
||||
w.add_object_to_array(size, &arr)
|
||||
} -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
/// Determines whether the var is empty (the NULL type).
|
||||
{ w.is_empty(var) } -> std::same_as<bool>;
|
||||
/// 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.
|
||||
{
|
||||
w.add_array_to_object(name, size, &obj)
|
||||
} -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Adds a new field to obj, thus mutating it.
|
||||
{ w.set_field(name, var, &obj) } -> std::same_as<void>;
|
||||
/// 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.
|
||||
{
|
||||
w.add_object_to_object(name, size, &obj)
|
||||
} -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
/// Adds a basic value (bool, numeric, string) to an array. Returns an
|
||||
/// OutputVarType containing the new value.
|
||||
{
|
||||
w.add_value_to_array(basic_value, &arr)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// 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.
|
||||
{
|
||||
w.add_value_to_object(name, basic_value, &obj)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Adds a null value to an array. Returns an
|
||||
/// OutputVarType containing the null value.
|
||||
{ w.add_null_to_array(&arr) } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// 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.
|
||||
{
|
||||
w.add_null_to_object(name, &obj)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// 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.
|
||||
{ w.end_array(&arr) } -> std::same_as<void>;
|
||||
|
||||
/// 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.
|
||||
{ w.end_object(&obj) } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -25,34 +26,42 @@ struct MapParser {
|
||||
using KeyType = std::decay_t<typename MapType::value_type::first_type>;
|
||||
using ValueType = std::decay_t<typename MapType::value_type::second_type>;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<MapType> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_map = [&](const auto& _obj) { return make_map(_r, _obj); };
|
||||
return _r.to_object(_var).and_then(to_map);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const MapType& _m) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
template <class P>
|
||||
static void write(const W& _w, const MapType& _m, const P& _parent) noexcept {
|
||||
auto obj = ParentType::add_object(_w, _m.size(), _parent);
|
||||
for (const auto& [k, v] : _m) {
|
||||
auto parsed_val = Parser<R, W, std::decay_t<ValueType>>::write(_w, v);
|
||||
if constexpr (internal::has_reflection_type_v<KeyType>) {
|
||||
using ReflT = typename KeyType::ReflectionType;
|
||||
|
||||
if constexpr (std::is_integral_v<ReflT> ||
|
||||
std::is_floating_point_v<ReflT>) {
|
||||
_w.set_field(std::to_string(k.reflection()), std::move(parsed_val),
|
||||
&obj);
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{std::to_string(k.reflection()), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
} else {
|
||||
_w.set_field(k.reflection(), std::move(parsed_val), &obj);
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{k.reflection(), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_integral_v<KeyType> ||
|
||||
std::is_floating_point_v<KeyType>) {
|
||||
_w.set_field(std::to_string(k), std::move(parsed_val), &obj);
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{std::to_string(k), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
} else {
|
||||
_w.set_field(k, std::move(parsed_val), &obj);
|
||||
const auto new_parent = typename ParentType::Object{k, &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
_w.end_object(&obj);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/Memoization.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/strings/replace_all.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "is_empty.hpp"
|
||||
#include "is_required.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -27,8 +30,9 @@ struct NamedTupleParser {
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
public:
|
||||
/// Generates a NamedTuple from a JSON Object.
|
||||
static Result<NamedTuple<FieldTypes...>> read(
|
||||
const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto& indices = field_indices();
|
||||
@@ -45,12 +49,12 @@ struct NamedTupleParser {
|
||||
return _r.to_object(_var).transform(to_fields_array).and_then(build);
|
||||
}
|
||||
|
||||
/// Transforms a NamedTuple into a JSON object.
|
||||
static OutputVarType write(const W& _w,
|
||||
const NamedTuple<FieldTypes...>& _tup) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
template <class P>
|
||||
static void write(const W& _w, const NamedTuple<FieldTypes...>& _tup,
|
||||
const P& _parent) noexcept {
|
||||
auto obj = ParentType::add_object(_w, _tup.size(), _parent);
|
||||
build_object_recursively(_w, _tup, &obj);
|
||||
return OutputVarType(obj);
|
||||
_w.end_object(&obj);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -160,14 +164,24 @@ struct NamedTupleParser {
|
||||
using FieldType =
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type;
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
auto value = Parser<R, W, ValueType>::write(_w, rfl::get<_i>(_tup));
|
||||
const auto& value = rfl::get<_i>(_tup);
|
||||
const auto name = FieldType::name_.str();
|
||||
const auto new_parent = typename ParentType::Object{name, _ptr};
|
||||
if constexpr (!is_required<ValueType, _ignore_empty_containers>()) {
|
||||
if (!_w.is_empty(value)) {
|
||||
_w.set_field(name, value, _ptr);
|
||||
if (!is_empty(value)) {
|
||||
if constexpr (internal::is_attribute_v<ValueType>) {
|
||||
Parser<R, W, ValueType>::write(_w, value,
|
||||
new_parent.as_attribute());
|
||||
} else {
|
||||
Parser<R, W, ValueType>::write(_w, value, new_parent);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_w.set_field(name, value, _ptr);
|
||||
if constexpr (internal::is_attribute_v<ValueType>) {
|
||||
Parser<R, W, ValueType>::write(_w, value, new_parent.as_attribute());
|
||||
} else {
|
||||
Parser<R, W, ValueType>::write(_w, value, new_parent);
|
||||
}
|
||||
}
|
||||
return build_object_recursively<_i + 1>(_w, _tup, _ptr);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
#ifndef RFL_PARSING_PARENT_HPP_
|
||||
#define RFL_PARSING_PARENT_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../always_false.hpp"
|
||||
#include "supports_attributes.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class W>
|
||||
struct Parent {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
struct Array {
|
||||
OutputArrayType* arr_;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
std::string name_;
|
||||
OutputObjectType* obj_;
|
||||
bool is_attribute_ = false;
|
||||
Object as_attribute() const { return Object{name_, obj_, true}; }
|
||||
};
|
||||
|
||||
struct Root {};
|
||||
|
||||
template <class ParentType>
|
||||
static OutputArrayType add_array(const W& _w, const size_t _size,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_array_to_array(_size, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
return _w.add_array_to_object(_parent.name_, _size, _parent.obj_);
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.array_as_root(_size);
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ParentType>
|
||||
static OutputObjectType add_object(const W& _w, const size_t _size,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_object_to_array(_size, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
return _w.add_object_to_object(_parent.name_, _size, _parent.obj_);
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.object_as_root(_size);
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ParentType>
|
||||
static OutputVarType add_null(const W& _w, const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_null_to_array(_parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
if constexpr (supports_attributes<std::decay_t<W>>) {
|
||||
return _w.add_null_to_object(_parent.name_, _parent.obj_,
|
||||
_parent.is_attribute_);
|
||||
} else {
|
||||
return _w.add_null_to_object(_parent.name_, _parent.obj_);
|
||||
}
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.null_as_root();
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ParentType, class T>
|
||||
static OutputVarType add_value(const W& _w, const T& _var,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_value_to_array(_var, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
if constexpr (supports_attributes<std::decay_t<W>>) {
|
||||
return _w.add_value_to_object(_parent.name_, _var, _parent.obj_,
|
||||
_parent.is_attribute_);
|
||||
} else {
|
||||
return _w.add_value_to_object(_parent.name_, _var, _parent.obj_);
|
||||
}
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.value_as_root(_var);
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -21,6 +22,8 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::array<T, _size>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_vec = [&](const auto& _arr) { return _r.to_vec(_arr); };
|
||||
@@ -43,13 +46,15 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
.and_then(extract);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::array<T, _size>& _arr) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::array<T, _size>& _arr,
|
||||
const P& _parent) noexcept {
|
||||
auto arr = ParentType::add_array(_w, _size, _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (auto it = _arr.begin(); it != _arr.end(); ++it) {
|
||||
_w.add(Parser<R, W, std::decay_t<T>>::write(_w, *it), &arr);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *it, new_parent);
|
||||
}
|
||||
return OutputVarType(arr);
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -21,8 +21,10 @@ struct Parser<R, W, Box<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_box);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const Box<T>& _box) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_box);
|
||||
template <class P>
|
||||
static void write(const W& _w, const Box<T>& _box,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_box, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -10,12 +10,11 @@
|
||||
#include "../internal/enums/StringConverter.hpp"
|
||||
#include "../internal/has_reflection_method_v.hpp"
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/to_ptr_named_tuple.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "supports_attributes.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -27,6 +26,8 @@ struct Parser {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
if constexpr (R::template has_custom_constructor<T>) {
|
||||
@@ -68,28 +69,26 @@ struct Parser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the variable to a JSON type.
|
||||
static auto write(const W& _w, const T& _var) noexcept {
|
||||
if constexpr (supports_attributes<W> && internal::is_attribute_v<T>) {
|
||||
return _w.from_basic_type(resolve_reflection_type(_var), true);
|
||||
} else if constexpr (internal::has_reflection_type_v<T>) {
|
||||
template <class P>
|
||||
static void write(const W& _w, const T& _var, const P& _parent) noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::decay_t<typename T::ReflectionType>;
|
||||
if constexpr (internal::has_reflection_method_v<T>) {
|
||||
return Parser<R, W, ReflectionType>::write(_w, _var.reflection());
|
||||
Parser<R, W, ReflectionType>::write(_w, _var.reflection(), _parent);
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return Parser<R, W, ReflectionType>::write(_w, r);
|
||||
Parser<R, W, ReflectionType>::write(_w, r, _parent);
|
||||
}
|
||||
} else if constexpr (std::is_class_v<T> && std::is_aggregate_v<T>) {
|
||||
const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var);
|
||||
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
|
||||
return Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple);
|
||||
Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple, _parent);
|
||||
} else if constexpr (std::is_enum_v<T>) {
|
||||
using StringConverter = internal::enums::StringConverter<T>;
|
||||
const auto str = StringConverter::enum_to_string(_var);
|
||||
return _w.from_basic_type(str);
|
||||
ParentType::add_value(_w, str, _parent);
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _w.from_basic_type(_var);
|
||||
ParentType::add_value(_w, _var, _parent);
|
||||
} else {
|
||||
static_assert(always_false_v<T>,
|
||||
"Unsupported type. Please refer to the sections on custom "
|
||||
@@ -97,20 +96,6 @@ struct Parser {
|
||||
"support for your own classes.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class V>
|
||||
static const auto resolve_reflection_type(const V& _var) {
|
||||
if constexpr (internal::has_reflection_type_v<V>) {
|
||||
if constexpr (internal::has_reflection_method_v<V>) {
|
||||
return resolve_reflection_type(_var.reflection());
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return resolve_reflection_type(r);
|
||||
}
|
||||
} else {
|
||||
return _var;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,6 +18,8 @@ struct Parser<R, W, std::optional<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::optional<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
@@ -26,11 +29,14 @@ struct Parser<R, W, std::optional<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_opt);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const std::optional<T>& _o) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::optional<T>& _o,
|
||||
const P& _parent) noexcept {
|
||||
if (!_o) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_o);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_o, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -29,12 +29,12 @@ struct Parser<R, W, std::pair<FirstType, SecondType>> {
|
||||
.transform(to_pair);
|
||||
}
|
||||
|
||||
/// Transform a std::pair into an array
|
||||
static OutputVarType write(
|
||||
const W& _w, const std::pair<FirstType, SecondType>& _p) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::pair<FirstType, SecondType>& _p,
|
||||
const P& _parent) noexcept {
|
||||
const auto tup = std::make_tuple(&_p.first, &_p.second);
|
||||
return Parser<R, W, std::tuple<const FirstType*, const SecondType*>>::write(
|
||||
_w, tup);
|
||||
Parser<R, W, std::tuple<const FirstType*, const SecondType*>>::write(
|
||||
_w, tup, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -16,6 +17,8 @@ struct Parser<R, W, T*> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T*> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
static_assert(always_false_v<T>,
|
||||
@@ -27,12 +30,13 @@ struct Parser<R, W, T*> {
|
||||
return Error("Unsupported.");
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w, const T* _ptr) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const T* _ptr, const P& _parent) noexcept {
|
||||
if (!_ptr) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_ptr);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_ptr, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -17,15 +17,15 @@ struct Parser<R, W, Ref<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(std::move(_t)); };
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ref);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w, const Ref<T>& _ref) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_ref);
|
||||
template <class P>
|
||||
static void write(const W& _w, const Ref<T>& _ref,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_ref, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -28,9 +28,10 @@ struct Parser<R, W, std::reference_wrapper<T>> {
|
||||
return Error("Unsupported.");
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::reference_wrapper<T> _ref) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _ref.get());
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::reference_wrapper<T> _ref,
|
||||
const P& _p) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _ref.get(), _p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -26,9 +26,10 @@ struct Parser<R, W, Rename<_name, T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_rename);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const Rename<_name, T>& _rename) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _rename.value());
|
||||
template <class P>
|
||||
static void write(const W& _w, const Rename<_name, T>& _rename,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _rename.value(), _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -39,17 +39,21 @@ struct Parser<R, W, Result<T>> {
|
||||
Parser<R, W, VariantType>::read(_r, _var).transform(to_res));
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const Result<T>& _r) noexcept {
|
||||
const auto write_t = [&](const auto& _t) -> OutputVarType {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _t);
|
||||
template <class P>
|
||||
static void write(const W& _w, const Result<T>& _r,
|
||||
const P& _parent) noexcept {
|
||||
const auto write_t = [&](const auto& _t) -> Nothing {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _t, _parent);
|
||||
return Nothing{};
|
||||
};
|
||||
|
||||
const auto write_err = [&](const auto& _err) -> OutputVarType {
|
||||
return Parser<R, W, ErrorType>::write(
|
||||
_w, ErrorType(make_field<"error">(_err.what())));
|
||||
const auto write_err = [&](const auto& _err) -> Nothing {
|
||||
Parser<R, W, ErrorType>::write(
|
||||
_w, ErrorType(make_field<"error">(_err.what())), _parent);
|
||||
return Nothing{};
|
||||
};
|
||||
|
||||
return *_r.transform(write_t).or_else(write_err);
|
||||
_r.transform(write_t).or_else(write_err);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -30,10 +30,11 @@ struct Parser<R, W, rfl::Variant<FieldTypes...>> {
|
||||
to_rfl_variant);
|
||||
}
|
||||
|
||||
static OutputVarType write(
|
||||
const W& _w, const rfl::Variant<FieldTypes...>& _variant) noexcept {
|
||||
return Parser<R, W, std::variant<FieldTypes...>>::write(_w,
|
||||
_variant.variant());
|
||||
template <class P>
|
||||
static void write(const W& _w, const rfl::Variant<FieldTypes...>& _variant,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::variant<FieldTypes...>>::write(_w, _variant.variant(),
|
||||
_parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,7 +18,8 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::shared_ptr<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(*_var)) {
|
||||
@@ -29,13 +31,14 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::shared_ptr<T>& _s) noexcept {
|
||||
template <class P>
|
||||
static OutputVarType write(const W& _w, const std::shared_ptr<T>& _s,
|
||||
const P& _parent) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_s);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -38,13 +38,15 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
return _r.to_object(_var).and_then(get_disc).and_then(to_result);
|
||||
}
|
||||
|
||||
static OutputVarType write(
|
||||
const W& _w, const TaggedUnion<_discriminator, AlternativeTypes...>&
|
||||
_tagged_union) noexcept {
|
||||
const auto handle = [&](const auto& _val) -> OutputVarType {
|
||||
return write_wrapped(_w, _val);
|
||||
template <class P>
|
||||
static void write(
|
||||
const W& _w,
|
||||
const TaggedUnion<_discriminator, AlternativeTypes...>& _tagged_union,
|
||||
const P& _parent) noexcept {
|
||||
const auto handle = [&](const auto& _val) {
|
||||
write_wrapped(_w, _val, _parent);
|
||||
};
|
||||
return std::visit(handle, _tagged_union.variant_);
|
||||
std::visit(handle, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -109,20 +111,21 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
}
|
||||
|
||||
/// Writes a wrapped version of the original object, which contains the tag.
|
||||
template <class T>
|
||||
static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept {
|
||||
template <class T, class P>
|
||||
static void write_wrapped(const W& _w, const T& _val,
|
||||
const P& _parent) noexcept {
|
||||
const auto tag = internal::make_tag<T>();
|
||||
using TagType = std::decay_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::decay_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
Parser<R, W, WrapperType>::write(_w, wrapper, _parent);
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
Parser<R, W, WrapperType>::write(_w, wrapper, _parent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,6 +18,8 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::unique_ptr<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
@@ -28,12 +31,14 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::unique_ptr<T>& _s) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::unique_ptr<T>& _s,
|
||||
const P& _parent) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_s);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -49,14 +49,15 @@ struct Parser<R, W, std::variant<FieldTypes...>> {
|
||||
}
|
||||
}
|
||||
|
||||
static OutputVarType write(
|
||||
const W& _w, const std::variant<FieldTypes...>& _variant) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::variant<FieldTypes...>& _variant,
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
|
||||
return FieldVariantParser<R, W, FieldTypes...>::write(_w, _variant);
|
||||
FieldVariantParser<R, W, FieldTypes...>::write(_w, _variant, _parent);
|
||||
} else {
|
||||
const auto handle = [&](const auto& _v) {
|
||||
using Type = std::decay_t<decltype(_v)>;
|
||||
return Parser<R, W, Type>::write(_w, _v);
|
||||
Parser<R, W, Type>::write(_w, _v, _parent);
|
||||
};
|
||||
return std::visit(handle, _variant);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -20,6 +21,8 @@ struct TupleParser {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::tuple<Ts...>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_vec = [&](auto _arr) { return _r.to_vec(_arr); };
|
||||
@@ -42,11 +45,13 @@ struct TupleParser {
|
||||
.and_then(extract);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::tuple<Ts...>& _tup) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
to_array<0>(_w, _tup, &arr);
|
||||
return arr;
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::tuple<Ts...>& _tup,
|
||||
const P& _parent) noexcept {
|
||||
auto arr = ParentType::add_array(_w, sizeof...(Ts), _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
to_array<0>(_w, _tup, new_parent);
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -89,17 +94,14 @@ struct TupleParser {
|
||||
return Parser<R, W, NewFieldType>::read(_r, _vec[_i]);
|
||||
}
|
||||
|
||||
template <int _i>
|
||||
template <int _i, class P>
|
||||
static void to_array(const W& _w, const std::tuple<Ts...>& _tup,
|
||||
OutputArrayType* _ptr) noexcept {
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (_i < sizeof...(Ts)) {
|
||||
using NewFieldType = std::decay_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
|
||||
const auto val =
|
||||
Parser<R, W, NewFieldType>::write(_w, std::get<_i>(_tup));
|
||||
_w.add(val, _ptr);
|
||||
to_array<_i + 1>(_w, _tup, _ptr);
|
||||
Parser<R, W, NewFieldType>::write(_w, std::get<_i>(_tup), _parent);
|
||||
to_array<_i + 1>(_w, _tup, _parent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_VECTORPARSER_HPP_
|
||||
#define RFL_PARSING_VECTORPARSER_HPP_
|
||||
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
@@ -8,6 +9,7 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "MapParser.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "is_forward_list.hpp"
|
||||
#include "is_map_like.hpp"
|
||||
@@ -31,6 +33,8 @@ struct VectorParser {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
using T = typename VecType::value_type;
|
||||
|
||||
static Result<VecType> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
@@ -44,15 +48,19 @@ struct VectorParser {
|
||||
}
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const VecType& _vec) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const VecType& _vec,
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (treat_as_map()) {
|
||||
return MapParser<R, W, VecType>::write(_w, _vec);
|
||||
MapParser<R, W, VecType>::write(_w, _vec, _parent);
|
||||
} else {
|
||||
auto arr = _w.new_array();
|
||||
auto arr = ParentType::add_array(
|
||||
_w, std::distance(_vec.begin(), _vec.end()), _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (const auto& v : _vec) {
|
||||
_w.add(Parser<R, W, std::decay_t<T>>::write(_w, v), &arr);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, v, new_parent);
|
||||
}
|
||||
return OutputVarType(arr);
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef RFL_PARSING_IS_EMPTY_HPP_
|
||||
#define RFL_PARSING_IS_EMPTY_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../internal/has_reflection_method_v.hpp"
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../internal/is_rename.hpp"
|
||||
#include "is_map_like.hpp"
|
||||
#include "is_vector_like.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T>
|
||||
static const bool is_empty(const T& _var) {
|
||||
using Type = std::decay_t<T>;
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
return !_var || is_empty(*_var);
|
||||
} else if constexpr (internal::has_reflection_type_v<Type>) {
|
||||
if constexpr (internal::has_reflection_method_v<Type>) {
|
||||
return is_empty(_var.reflection());
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return is_empty(r);
|
||||
}
|
||||
} else if constexpr (internal::is_rename_v<Type>) {
|
||||
return is_empty(_var.value());
|
||||
} else if constexpr (is_map_like_v<Type> || is_vector_like_v<Type>) {
|
||||
return _var.begin() == _var.end();
|
||||
} else {
|
||||
return !_var;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,7 @@
|
||||
#define RFL_PARSING_SUPPORTSATTRIBUTES_HPP_
|
||||
|
||||
#include <concepts>
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
|
||||
@@ -10,9 +11,15 @@ namespace parsing {
|
||||
|
||||
/// Determines whether a writer supports attributes.
|
||||
template <class W>
|
||||
concept supports_attributes = requires(W w, int val, bool _is_attribute) {
|
||||
concept supports_attributes = requires(W w, std::string name,
|
||||
typename W::OutputObjectType obj,
|
||||
bool is_attribute) {
|
||||
{
|
||||
w.from_basic_type(val, _is_attribute)
|
||||
w.add_value_to_object(name, name, &obj, is_attribute)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
{
|
||||
w.add_null_to_object(name, &obj, is_attribute)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
#ifndef FLEXBUF_XML_HPP_
|
||||
#define FLEXBUF_XML_HPP_
|
||||
#ifndef RFL_XML_HPP_
|
||||
#define RFL_XML_HPP_
|
||||
|
||||
#include "xml/Parser.hpp"
|
||||
#include "xml/Reader.hpp"
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTARRAY_HPP_
|
||||
#define RFL_XML_OUTPUTARRAY_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputArray : public OutputVar {
|
||||
public:
|
||||
OutputArray() {}
|
||||
|
||||
~OutputArray() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
for (auto& v : vars_) {
|
||||
v->insert(_key, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
/// Adds a new element to the vector.
|
||||
void push_back(const Ref<OutputVar>& _var) { vars_.push_back(_var); }
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<Ref<OutputVar>> vars_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,32 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTNULL_HPP_
|
||||
#define RFL_XML_OUTPUTNULL_HPP_
|
||||
|
||||
#include <optional>
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputNull : public OutputVar {
|
||||
public:
|
||||
OutputNull() {}
|
||||
|
||||
~OutputNull() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
_parent->append_child(_key.c_str());
|
||||
}
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return true; }
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,44 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTOBJECT_HPP_
|
||||
#define RFL_XML_OUTPUTOBJECT_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputObject : public OutputVar {
|
||||
public:
|
||||
OutputObject() {}
|
||||
|
||||
~OutputObject() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
auto node_child = _parent->append_child(_key.c_str());
|
||||
for (auto& v : vars_) {
|
||||
v.second->insert(v.first, &node_child);
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
/// Adds a new element to the vector.
|
||||
void push_back(const std::string& _name, const Ref<OutputVar>& _var) {
|
||||
vars_.push_back(std::make_pair(_name, _var));
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<std::pair<std::string, Ref<OutputVar>>> vars_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,67 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTVALUE_HPP_
|
||||
#define RFL_XML_OUTPUTVALUE_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../always_false.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
template <class T>
|
||||
class OutputValue : public OutputVar {
|
||||
public:
|
||||
static constexpr const char* XML_CONTENT = "xml_content";
|
||||
|
||||
public:
|
||||
OutputValue(T _val, bool _is_attribute)
|
||||
: is_attribute_(_is_attribute), val_(_val) {}
|
||||
|
||||
~OutputValue() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
const auto str = to_string();
|
||||
if (is_attribute_) {
|
||||
_parent->append_attribute(_key.c_str()) = str.c_str();
|
||||
} else if (_key == XML_CONTENT) {
|
||||
_parent->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
} else {
|
||||
auto node_child = _parent->append_child(_key.c_str());
|
||||
node_child.append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
private:
|
||||
/// Transforms the underlying value into a string.
|
||||
std::string to_string() {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return val_;
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return val_ ? "true" : "false";
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>() ||
|
||||
std::is_integral<std::decay_t<T>>()) {
|
||||
return std::to_string(val_);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// Whether you want this encoded as an attribute.
|
||||
bool is_attribute_;
|
||||
|
||||
/// The underlying value.
|
||||
T val_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTVAR_HPP_
|
||||
#define RFL_XML_OUTPUTVAR_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputVar {
|
||||
public:
|
||||
virtual ~OutputVar() = default;
|
||||
|
||||
/// Inserts the value into the _parent.
|
||||
virtual void insert(const std::string& _key, pugi::xml_node* _parent) = 0;
|
||||
|
||||
/// Whether this is null.
|
||||
virtual bool is_null() const = 0;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -3,13 +3,11 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "is_required_for_xml.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../parsing/NamedTupleParser.hpp"
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
#include "is_required_for_xml.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
+138
-23
@@ -14,49 +14,164 @@
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "OutputArray.hpp"
|
||||
#include "OutputNull.hpp"
|
||||
#include "OutputObject.hpp"
|
||||
#include "OutputValue.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
struct Writer {
|
||||
using OutputArrayType = Ref<OutputArray>;
|
||||
using OutputObjectType = Ref<OutputObject>;
|
||||
using OutputVarType = Ref<OutputVar>;
|
||||
static constexpr const char* XML_CONTENT = "xml_content";
|
||||
|
||||
Writer() {}
|
||||
struct XMLOutputArray {
|
||||
XMLOutputArray(const std::string& _name, const Ref<pugi::xml_node>& _node)
|
||||
: name_(_name), node_(_node) {}
|
||||
std::string name_;
|
||||
Ref<pugi::xml_node> node_;
|
||||
};
|
||||
|
||||
struct XMLOutputObject {
|
||||
XMLOutputObject(const Ref<pugi::xml_node>& _node) : node_(_node) {}
|
||||
Ref<pugi::xml_node> node_;
|
||||
};
|
||||
|
||||
struct XMLOutputVar {
|
||||
XMLOutputVar(const Ref<pugi::xml_node>& _node) : node_(_node) {}
|
||||
Ref<pugi::xml_node> node_;
|
||||
};
|
||||
|
||||
using OutputArrayType = XMLOutputArray;
|
||||
using OutputObjectType = XMLOutputObject;
|
||||
using OutputVarType = XMLOutputVar;
|
||||
|
||||
Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
|
||||
: root_(_root), root_name_(_root_name) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
(*_arr)->push_back(_var);
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputArrayType(root_name_, node_child);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept { return make_ref<OutputNull>(); }
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(
|
||||
const T& _var, const bool _is_attribute = false) const noexcept {
|
||||
return make_ref<OutputValue<T>>(_var, _is_attribute);
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept { return make_ref<OutputArray>(); }
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return make_ref<OutputObject>();
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return *_parent;
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return _var->is_null();
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return OutputArrayType(_name, _parent->node_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
(*_obj)->push_back(_name, _var);
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(
|
||||
const std::string& _name, const T& _var, OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
if (_is_attribute) {
|
||||
_parent->node_->append_attribute(_name.c_str()) = str.c_str();
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
_parent->node_->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_name.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string& _name, OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept {
|
||||
if (_is_attribute) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_name.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
std::string to_string(const T& _val) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
return _val;
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
return _val ? "true" : "false";
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>() ||
|
||||
std::is_integral<std::decay_t<T>>()) {
|
||||
return std::to_string(_val);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
Ref<pugi::xml_node> root_;
|
||||
|
||||
std::string root_name_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef RFL_XML_IS_REQUIRED_FOR_XML_HPP_
|
||||
#define RFL_XML_IS_REQUIRED_FOR_XML_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../parsing/is_required.hpp"
|
||||
#include "../parsing/is_vector_like.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
// Empty vectors are simply not represented in XML. Therefore, XML requires more
|
||||
// lax rules when things are required.
|
||||
template <class T>
|
||||
consteval bool is_required_for_xml() {
|
||||
using Type = std::decay_t<std::remove_pointer_t<T>>;
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return is_required_for_xml<typename T::ReflectionType>();
|
||||
} else {
|
||||
return parsing::is_required<Type>() && !parsing::is_vector_like_v<Type>;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "../internal/StringLiteral.hpp"
|
||||
#include "../internal/get_type_name.hpp"
|
||||
#include "../internal/remove_namespaces.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -29,6 +30,8 @@ consteval auto get_root_name() {
|
||||
template <internal::StringLiteral _root = internal::StringLiteral(""), class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream,
|
||||
const std::string& _indent = " ") {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
|
||||
constexpr auto root_name = get_root_name<_root, T>();
|
||||
|
||||
static_assert(root_name.string_view().find("<") == std::string_view::npos &&
|
||||
@@ -38,18 +41,17 @@ std::ostream& write(const T& _obj, std::ostream& _stream,
|
||||
"explicit root name to rfl::xml::write(...) like this: "
|
||||
"rfl::xml::write<\"root_name\">(...).");
|
||||
|
||||
auto w = Writer();
|
||||
const auto xml_obj = Parser<T>::write(w, _obj);
|
||||
const auto doc = rfl::Ref<pugi::xml_document>::make();
|
||||
|
||||
pugi::xml_document doc;
|
||||
|
||||
auto declaration_node = doc.append_child(pugi::node_declaration);
|
||||
auto declaration_node = doc->append_child(pugi::node_declaration);
|
||||
declaration_node.append_attribute("version") = "1.0";
|
||||
declaration_node.append_attribute("encoding") = "UTF-8";
|
||||
|
||||
xml_obj->insert(root_name.str(), &doc);
|
||||
auto w = Writer(doc, root_name.str());
|
||||
|
||||
doc.save(_stream, _indent.c_str());
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
|
||||
doc->save(_stream, _indent.c_str());
|
||||
|
||||
return _stream;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RFL_YAML_HPP_
|
||||
#define RFL_YAML_HPP_
|
||||
|
||||
#include "yaml/Parser.hpp"
|
||||
#include "yaml/Reader.hpp"
|
||||
#include "yaml/Writer.hpp"
|
||||
#include "yaml/load.hpp"
|
||||
#include "yaml/read.hpp"
|
||||
#include "yaml/save.hpp"
|
||||
#include "yaml/write.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_YAML_PARSER_HPP_
|
||||
#define RFL_YAML_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
template <class T>
|
||||
using Parser = parsing::Parser<Reader, Writer, T>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,151 @@
|
||||
#ifndef RFL_YAML_READER_HPP_
|
||||
#define RFL_YAML_READER_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
struct Reader {
|
||||
struct YAMLInputArray {
|
||||
YAMLInputArray(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
struct YAMLInputObject {
|
||||
YAMLInputObject(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
struct YAMLInputVar {
|
||||
YAMLInputVar(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
using InputArrayType = YAMLInputArray;
|
||||
using InputObjectType = YAMLInputObject;
|
||||
using InputVarType = YAMLInputVar;
|
||||
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_yaml_obj(var);
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = InputVarType(_obj.node_[_name]);
|
||||
if (!var.node_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.node_ && true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>() ||
|
||||
std::is_same<std::decay_t<T>, bool>() ||
|
||||
std::is_floating_point<std::decay_t<T>>() ||
|
||||
std::is_integral<std::decay_t<T>>()) {
|
||||
return _var.node_.as<std::decay_t<T>>();
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsSequence()) {
|
||||
return rfl::Error("Could not cast to sequence!");
|
||||
}
|
||||
return InputArrayType(_var.node_);
|
||||
}
|
||||
|
||||
template <size_t size, class FunctionType>
|
||||
std::array<std::optional<InputVarType>, size> to_fields_array(
|
||||
const FunctionType& _fct, const InputObjectType& _obj) const noexcept {
|
||||
std::array<std::optional<InputVarType>, size> f_arr;
|
||||
for (const auto& p : _obj.node_) {
|
||||
try {
|
||||
const auto k = p.first.as<std::string>();
|
||||
const auto ix = _fct(std::string_view(k));
|
||||
if (ix != -1) {
|
||||
f_arr[ix] = InputVarType(p.second);
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return f_arr;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, InputVarType>> to_map(
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
std::vector<std::pair<std::string, InputVarType>> m;
|
||||
for (const auto& p : _obj.node_) {
|
||||
try {
|
||||
auto k = p.first.as<std::string>();
|
||||
m.emplace_back(std::make_pair(k, p.second));
|
||||
} catch (std::exception& e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsMap()) {
|
||||
return rfl::Error("Could not cast to map!");
|
||||
}
|
||||
return InputObjectType(_var.node_);
|
||||
}
|
||||
|
||||
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
|
||||
std::vector<InputVarType> vec;
|
||||
for (size_t i = 0; i < _arr.node_.size(); ++i) {
|
||||
vec.push_back(InputVarType(_arr.node_[i]));
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType _var) const noexcept {
|
||||
try {
|
||||
return T::from_yaml_obj(_var);
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
#ifndef RFL_YAML_WRITER_HPP_
|
||||
#define RFL_YAML_WRITER_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
struct YAMLArray {};
|
||||
|
||||
struct YAMLObject {};
|
||||
|
||||
struct YAMLVar {};
|
||||
|
||||
using OutputArrayType = YAMLArray;
|
||||
using OutputObjectType = YAMLObject;
|
||||
using OutputVarType = YAMLVar;
|
||||
|
||||
Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, YAML::Null);
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
(*out_) << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
(*out_) << YAML::EndMap;
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string& _name,
|
||||
const T& _var) const noexcept {
|
||||
(*out_) << YAML::Key << _name << YAML::Value << _var;
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
(*out_) << _var;
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
(*out_) << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const std::string& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
(*out_) << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
public:
|
||||
const Ref<YAML::Emitter> out_;
|
||||
};
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_YAML_LOAD_HPP_
|
||||
#define RFL_YAML_LOAD_HPP_
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/load_string.hpp"
|
||||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
template <class T>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_string = [](const auto& _str) { return read<T>(_str); };
|
||||
return rfl::io::load_string(_fname).and_then(read_string);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef RFL_YAML_READ_HPP_
|
||||
#define RFL_YAML_READ_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a YAML var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _var);
|
||||
}
|
||||
|
||||
/// Parses an object from YAML using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _yaml_str) {
|
||||
try {
|
||||
const auto var = InputVarType(YAML::Load(_yaml_str));
|
||||
return read<T>(var);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(yaml_str);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_YAML_SAVE_HPP_
|
||||
#define RFL_YAML_SAVE_HPP_
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/save_string.hpp"
|
||||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
template <class T>
|
||||
Result<Nothing> save(const std::string& _fname, const T& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef RFL_YAML_WRITE_HPP_
|
||||
#define RFL_YAML_WRITE_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
/// Writes a XML into an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto out = Ref<YAML::Emitter>::make();
|
||||
auto w = Writer(out);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
_stream << out->c_str();
|
||||
return _stream;
|
||||
}
|
||||
|
||||
/// Returns a XML string.
|
||||
template <class T>
|
||||
std::string write(const T& _obj) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto out = Ref<YAML::Emitter>::make();
|
||||
auto w = Writer(out);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
return out->c_str();
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // XML_PARSER_HPP_
|
||||
@@ -15,3 +15,7 @@ endif()
|
||||
if (REFLECTCPP_XML)
|
||||
add_subdirectory(xml)
|
||||
endif()
|
||||
|
||||
if (REFLECTCPP_YAML)
|
||||
add_subdirectory(yaml)
|
||||
endif()
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace test_monster_example1 {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
enum class Color { Red, Green, Blue };
|
||||
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
@@ -33,7 +33,7 @@ struct Monster {
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<std::uint8_t> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
Color color = Color::Blue;
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
@@ -57,7 +57,7 @@ void test() {
|
||||
.hp = 80,
|
||||
.name = "MyMonster",
|
||||
.inventory = inventory,
|
||||
.color = Color::make<"Red">(),
|
||||
.color = Color::Red,
|
||||
.weapons = weapons,
|
||||
.equipped = rfl::make_field<"weapon">(axe)};
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
project(reflect-cpp-yaml-tests)
|
||||
|
||||
file(GLOB_RECURSE SOURCES "*.cpp")
|
||||
|
||||
add_executable(reflect-cpp-yaml-tests ${SOURCES})
|
||||
|
||||
target_link_libraries(reflect-cpp-yaml-tests PRIVATE reflectcpp)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "test_array.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl/yaml.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
// Make sure things still compile when
|
||||
// rfl.hpp is included after rfl/yaml.hpp.
|
||||
#include <rfl.hpp>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_array {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::array<Person, 3>> children = nullptr;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
auto lisa = Person{.first_name = "Lisa"};
|
||||
|
||||
auto maggie = Person{.first_name = "Maggie"};
|
||||
|
||||
const auto homer = Person{
|
||||
.first_name = "Homer",
|
||||
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
|
||||
std::move(bart), std::move(lisa), std::move(maggie)})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_array
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_array{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "test_box.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_box {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
using Tag = rfl::Literal<"Leaf">;
|
||||
double value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
using Tag = rfl::Literal<"Node">;
|
||||
rfl::Rename<"criticalValue", double> critical_value;
|
||||
rfl::Box<DecisionTree> lesser;
|
||||
rfl::Box<DecisionTree> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
|
||||
|
||||
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
|
||||
|
||||
auto node = DecisionTree::Node{
|
||||
.critical_value = 10.0,
|
||||
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
|
||||
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
|
||||
|
||||
const DecisionTree tree{.leaf_or_node = std::move(node)};
|
||||
|
||||
write_and_read(tree);
|
||||
|
||||
}
|
||||
} // namespace test_box
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_box{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "test_custom_class1.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class1 {
|
||||
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
using ReflectionType = PersonImpl;
|
||||
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person("Bart");
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class1
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_class1{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "test_custom_class3.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
const int _age)
|
||||
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
std::string last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name;
|
||||
int age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = _p.last_name(),
|
||||
.age = _p.age()};
|
||||
}
|
||||
|
||||
Person to_class() const { return Person(first_name(), last_name(), age); }
|
||||
};
|
||||
} // namespace test_custom_class3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
|
||||
test_custom_class3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_custom_class3 {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class3
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_class3{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "test_custom_class4.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_class4 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
const rfl::Box<std::string>& _last_name, int _age)
|
||||
: first_name_(_first_name),
|
||||
last_name_(rfl::make_box<std::string>(*_last_name)),
|
||||
age_(_age) {}
|
||||
|
||||
const auto& first_name() const { return first_name_; }
|
||||
|
||||
const auto& last_name() const { return last_name_; }
|
||||
|
||||
auto age() const { return age_; }
|
||||
|
||||
private:
|
||||
std::string first_name_;
|
||||
rfl::Box<std::string> last_name_;
|
||||
int age_;
|
||||
};
|
||||
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
|
||||
static PersonImpl from_class(const Person& _p) noexcept {
|
||||
return PersonImpl{.first_name = _p.first_name(),
|
||||
.last_name = rfl::make_box<std::string>(*_p.last_name()),
|
||||
.age = _p.age()};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace test_custom_class4
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, test_custom_class4::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class4::Person,
|
||||
test_custom_class4::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
namespace test_custom_class4 {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = test_custom_class4::Person(
|
||||
"Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class4
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_class4{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "test_custom_constructor.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/yaml.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_custom_constructor {
|
||||
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_yaml_obj(
|
||||
typename rfl::yaml::Reader::InputVarType _obj) {
|
||||
/// This only exists for the purpose of the test.
|
||||
const auto change_first_name = [](auto&& _person) {
|
||||
return rfl::replace(std::move(_person),
|
||||
rfl::Field<"firstName", std::string>("Bart"));
|
||||
};
|
||||
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)
|
||||
.transform(change_first_name);
|
||||
}
|
||||
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name;
|
||||
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto res = rfl::yaml::read<Person>(
|
||||
R"(firstName: Homer
|
||||
lastName: Simpson
|
||||
birthday: 1987-04-19)");
|
||||
|
||||
if (!res) {
|
||||
std::cout << "Test failed on read. Error: " << res.error().value().what()
|
||||
<< std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.value().first_name() != "Bart") {
|
||||
std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'"
|
||||
<< std::endl
|
||||
<< std::endl;
|
||||
} else {
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace test_custom_constructor
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_custom_constructor{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "test_default_values.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_default_values {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
const auto lisa = Person{.first_name = "Lisa"};
|
||||
const auto maggie = Person{.first_name = "Maggie"};
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer",
|
||||
.children = std::vector<Person>({bart, lisa, maggie})};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_default_values
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_default_values{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_deque.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_deque {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::deque<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::deque<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
|
||||
}
|
||||
} // namespace test_deque
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_deque{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "test_enum.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_enum {
|
||||
|
||||
enum class Color { red, green, blue, yellow };
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto circle = Circle{.radius = 2.0, .color = Color::green};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_enum
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_enum {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "test_field_variant.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_field_variant {
|
||||
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const Shapes r =
|
||||
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_field_variant
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_field_variant{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_enum.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum {
|
||||
|
||||
enum class Color {
|
||||
red = 256,
|
||||
green = 512,
|
||||
blue = 1024,
|
||||
yellow = 2048,
|
||||
orange = (256 | 2048) // red + yellow = orange
|
||||
};
|
||||
|
||||
inline Color operator|(Color c1, Color c2) {
|
||||
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto circle =
|
||||
Circle{.radius = 2.0, .color = Color::blue | Color::orange};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_flag_enum
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flag_enum {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "test_enum.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flag_enum_with_int {
|
||||
|
||||
enum class Color {
|
||||
red = 256,
|
||||
green = 512,
|
||||
blue = 1024,
|
||||
yellow = 2048,
|
||||
orange = (256 | 2048) // red + yellow = orange
|
||||
};
|
||||
|
||||
inline Color operator|(Color c1, Color c2) {
|
||||
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
|
||||
}
|
||||
|
||||
struct Circle {
|
||||
float radius;
|
||||
Color color;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto circle = Circle{.radius = 2.0, .color = static_cast<Color>(10000)};
|
||||
|
||||
write_and_read(circle);
|
||||
}
|
||||
|
||||
} // namespace test_flag_enum_with_int
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flag_enum_with_int {
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include "test_flatten.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flatten {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
|
||||
rfl::Field<"age", int> age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Field<"employer", rfl::Box<std::string>> employer;
|
||||
rfl::Field<"salary", float> salary;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
} // namespace test_flatten
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flatten{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "test_flatten_anonymous.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_flatten_anonymous {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
|
||||
struct Employee {
|
||||
rfl::Flatten<Person> person;
|
||||
rfl::Box<std::string> employer;
|
||||
float salary;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto employee = Employee{
|
||||
.person = Person{.first_name = "Homer",
|
||||
.last_name = rfl::make_box<std::string>("Simpson"),
|
||||
.age = 45},
|
||||
.employer = rfl::make_box<std::string>("Mr. Burns"),
|
||||
.salary = 60000.0};
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
|
||||
} // namespace test_flatten_anonymous
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_flatten_anonymous{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include "test_forward_list.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_forward_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", std::string> first_name;
|
||||
rfl::Rename<"lastName", std::string> last_name = "Simpson";
|
||||
std::unique_ptr<std::forward_list<Person>> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
children->emplace_front(Person{.first_name = "Maggie"});
|
||||
children->emplace_front(Person{.first_name = "Lisa"});
|
||||
children->emplace_front(Person{.first_name = "Bart"});
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
|
||||
}
|
||||
} // namespace test_forward_list
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_forward_list{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "test_literal.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_literal {
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Rename<"firstName", FirstName> first_name;
|
||||
rfl::Rename<"lastName", LastName> last_name;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_literal
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_literal{
|
||||
void test();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "test_literal_map.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_literal_map {
|
||||
|
||||
using FieldName = rfl::Literal<"firstName", "lastName">;
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
std::map<FieldName, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(std::make_pair(FieldName::make<"firstName">(),
|
||||
std::make_unique<std::string>("Homer")));
|
||||
homer.insert(std::make_pair(FieldName::make<"lastName">(),
|
||||
std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_literal_map
|
||||
@@ -0,0 +1,4 @@
|
||||
namespace test_literal_map{
|
||||
void test();
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user