From 5c81e40f097e0c91fddded584ba8f2d9bf7a49cd Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Wed, 3 Jan 2024 07:22:36 +0100 Subject: [PATCH] Redesigned the writer (#36) and added YAML support based on the new writer (#23) --- .github/workflows/test.yaml | 6 +- .gitignore | 2 + CMakeLists.txt | 8 +- README.md | 63 ++++--- docs/README.md | 2 + docs/supporting_your_own_format.md | 105 +++++++++--- docs/yaml.md | 98 +++++++++++ include/rfl/flexbuf/OutputArray.hpp | 59 ------- include/rfl/flexbuf/OutputNull.hpp | 38 ----- include/rfl/flexbuf/OutputObject.hpp | 61 ------- include/rfl/flexbuf/OutputValue.hpp | 67 -------- include/rfl/flexbuf/OutputVar.hpp | 27 --- include/rfl/flexbuf/Parser.hpp | 2 +- include/rfl/flexbuf/Reader.hpp | 8 - include/rfl/flexbuf/Writer.hpp | 154 ++++++++++++++--- include/rfl/flexbuf/write.hpp | 14 +- include/rfl/json/Reader.hpp | 21 +-- include/rfl/json/Writer.hpp | 115 ++++++++++--- include/rfl/json/write.hpp | 9 +- include/rfl/parsing/CustomParser.hpp | 50 +++--- include/rfl/parsing/FieldVariantParser.hpp | 9 +- include/rfl/parsing/IsReader.hpp | 2 +- include/rfl/parsing/IsWriter.hpp | 97 ++++++++--- include/rfl/parsing/MapParser.hpp | 27 ++- include/rfl/parsing/NamedTupleParser.hpp | 34 ++-- include/rfl/parsing/Parent.hpp | 105 ++++++++++++ include/rfl/parsing/Parser_array.hpp | 15 +- include/rfl/parsing/Parser_box.hpp | 6 +- include/rfl/parsing/Parser_default.hpp | 37 ++-- include/rfl/parsing/Parser_optional.hpp | 12 +- include/rfl/parsing/Parser_pair.hpp | 10 +- include/rfl/parsing/Parser_ptr.hpp | 12 +- include/rfl/parsing/Parser_ref.hpp | 8 +- .../rfl/parsing/Parser_reference_wrapper.hpp | 7 +- include/rfl/parsing/Parser_rename.hpp | 7 +- include/rfl/parsing/Parser_result.hpp | 18 +- include/rfl/parsing/Parser_rfl_variant.hpp | 9 +- include/rfl/parsing/Parser_shared_ptr.hpp | 15 +- include/rfl/parsing/Parser_tagged_union.hpp | 23 +-- include/rfl/parsing/Parser_unique_ptr.hpp | 13 +- include/rfl/parsing/Parser_variant.hpp | 9 +- include/rfl/parsing/TupleParser.hpp | 26 +-- include/rfl/parsing/VectorParser.hpp | 18 +- include/rfl/parsing/is_empty.hpp | 39 +++++ include/rfl/parsing/supports_attributes.hpp | 11 +- include/rfl/xml.hpp | 4 +- include/rfl/xml/OutputArray.hpp | 41 ----- include/rfl/xml/OutputNull.hpp | 32 ---- include/rfl/xml/OutputObject.hpp | 44 ----- include/rfl/xml/OutputValue.hpp | 67 -------- include/rfl/xml/OutputVar.hpp | 24 --- include/rfl/xml/Parser.hpp | 2 - include/rfl/xml/Writer.hpp | 161 +++++++++++++++--- include/rfl/xml/is_required_for_xml.hpp | 28 --- include/rfl/xml/write.hpp | 16 +- include/rfl/yaml.hpp | 12 ++ include/rfl/yaml/Parser.hpp | 17 ++ include/rfl/yaml/Reader.hpp | 151 ++++++++++++++++ include/rfl/yaml/Writer.hpp | 147 ++++++++++++++++ include/rfl/yaml/load.hpp | 20 +++ include/rfl/yaml/read.hpp | 46 +++++ include/rfl/yaml/save.hpp | 26 +++ include/rfl/yaml/write.hpp | 41 +++++ tests/CMakeLists.txt | 4 + tests/flexbuffers/test_monster_example1.cpp | 6 +- tests/yaml/CMakeLists.txt | 8 + tests/yaml/test_array.cpp | 40 +++++ tests/yaml/test_array.hpp | 4 + tests/yaml/test_box.cpp | 50 ++++++ tests/yaml/test_box.hpp | 4 + tests/yaml/test_custom_class1.cpp | 42 +++++ tests/yaml/test_custom_class1.hpp | 4 + tests/yaml/test_custom_class3.cpp | 68 ++++++++ tests/yaml/test_custom_class3.hpp | 4 + tests/yaml/test_custom_class4.cpp | 69 ++++++++ tests/yaml/test_custom_class4.hpp | 4 + tests/yaml/test_custom_constructor.cpp | 57 +++++++ tests/yaml/test_custom_constructor.hpp | 4 + tests/yaml/test_default_values.cpp | 32 ++++ tests/yaml/test_default_values.hpp | 4 + tests/yaml/test_deque.cpp | 33 ++++ tests/yaml/test_deque.hpp | 4 + tests/yaml/test_enum.cpp | 29 ++++ tests/yaml/test_enum.hpp | 4 + tests/yaml/test_field_variant.cpp | 39 +++++ tests/yaml/test_field_variant.hpp | 4 + tests/yaml/test_flag_enum.cpp | 39 +++++ tests/yaml/test_flag_enum.hpp | 4 + tests/yaml/test_flag_enum_with_int.cpp | 38 +++++ tests/yaml/test_flag_enum_with_int.hpp | 4 + tests/yaml/test_flatten.cpp | 38 +++++ tests/yaml/test_flatten.hpp | 4 + tests/yaml/test_flatten_anonymous.cpp | 39 +++++ tests/yaml/test_flatten_anonymous.hpp | 4 + tests/yaml/test_forward_list.cpp | 33 ++++ tests/yaml/test_forward_list.hpp | 4 + tests/yaml/test_literal.cpp | 30 ++++ tests/yaml/test_literal.hpp | 4 + tests/yaml/test_literal_map.cpp | 28 +++ tests/yaml/test_literal_map.hpp | 4 + tests/yaml/test_map.cpp | 32 ++++ tests/yaml/test_map.hpp | 4 + tests/yaml/test_map_with_key_validation.cpp | 33 ++++ tests/yaml/test_map_with_key_validation.hpp | 4 + tests/yaml/test_monster_example.cpp | 64 +++++++ tests/yaml/test_monster_example.hpp | 4 + tests/yaml/test_readme_example.cpp | 51 ++++++ tests/yaml/test_readme_example.hpp | 4 + tests/yaml/test_readme_example2.cpp | 26 +++ tests/yaml/test_readme_example2.hpp | 4 + tests/yaml/test_ref.cpp | 49 ++++++ tests/yaml/test_ref.hpp | 4 + tests/yaml/test_save_load.cpp | 70 ++++++++ tests/yaml/test_save_load.hpp | 4 + tests/yaml/test_set.cpp | 30 ++++ tests/yaml/test_set.hpp | 4 + tests/yaml/test_size.cpp | 42 +++++ tests/yaml/test_size.hpp | 4 + tests/yaml/test_string_map.cpp | 24 +++ tests/yaml/test_string_map.hpp | 4 + tests/yaml/test_tagged_union.cpp | 36 ++++ tests/yaml/test_tagged_union.hpp | 4 + tests/yaml/test_timestamp.cpp | 36 ++++ tests/yaml/test_timestamp.hpp | 4 + tests/yaml/test_unique_ptr.cpp | 33 ++++ tests/yaml/test_unique_ptr.hpp | 4 + tests/yaml/test_unique_ptr2.cpp | 48 ++++++ tests/yaml/test_unique_ptr2.hpp | 4 + tests/yaml/test_variant.cpp | 36 ++++ tests/yaml/test_variant.hpp | 4 + tests/yaml/tests.cpp | 67 ++++++++ tests/yaml/write_and_read.hpp | 32 ++++ vcpkg.json | 4 + 133 files changed, 3082 insertions(+), 838 deletions(-) create mode 100644 docs/yaml.md delete mode 100644 include/rfl/flexbuf/OutputArray.hpp delete mode 100644 include/rfl/flexbuf/OutputNull.hpp delete mode 100644 include/rfl/flexbuf/OutputObject.hpp delete mode 100644 include/rfl/flexbuf/OutputValue.hpp delete mode 100644 include/rfl/flexbuf/OutputVar.hpp create mode 100644 include/rfl/parsing/Parent.hpp create mode 100644 include/rfl/parsing/is_empty.hpp delete mode 100644 include/rfl/xml/OutputArray.hpp delete mode 100644 include/rfl/xml/OutputNull.hpp delete mode 100644 include/rfl/xml/OutputObject.hpp delete mode 100644 include/rfl/xml/OutputValue.hpp delete mode 100644 include/rfl/xml/OutputVar.hpp delete mode 100644 include/rfl/xml/is_required_for_xml.hpp create mode 100644 include/rfl/yaml.hpp create mode 100644 include/rfl/yaml/Parser.hpp create mode 100644 include/rfl/yaml/Reader.hpp create mode 100644 include/rfl/yaml/Writer.hpp create mode 100644 include/rfl/yaml/load.hpp create mode 100644 include/rfl/yaml/read.hpp create mode 100644 include/rfl/yaml/save.hpp create mode 100644 include/rfl/yaml/write.hpp create mode 100644 tests/yaml/CMakeLists.txt create mode 100644 tests/yaml/test_array.cpp create mode 100644 tests/yaml/test_array.hpp create mode 100644 tests/yaml/test_box.cpp create mode 100644 tests/yaml/test_box.hpp create mode 100644 tests/yaml/test_custom_class1.cpp create mode 100644 tests/yaml/test_custom_class1.hpp create mode 100644 tests/yaml/test_custom_class3.cpp create mode 100644 tests/yaml/test_custom_class3.hpp create mode 100644 tests/yaml/test_custom_class4.cpp create mode 100644 tests/yaml/test_custom_class4.hpp create mode 100644 tests/yaml/test_custom_constructor.cpp create mode 100644 tests/yaml/test_custom_constructor.hpp create mode 100644 tests/yaml/test_default_values.cpp create mode 100644 tests/yaml/test_default_values.hpp create mode 100644 tests/yaml/test_deque.cpp create mode 100644 tests/yaml/test_deque.hpp create mode 100644 tests/yaml/test_enum.cpp create mode 100644 tests/yaml/test_enum.hpp create mode 100644 tests/yaml/test_field_variant.cpp create mode 100644 tests/yaml/test_field_variant.hpp create mode 100644 tests/yaml/test_flag_enum.cpp create mode 100644 tests/yaml/test_flag_enum.hpp create mode 100644 tests/yaml/test_flag_enum_with_int.cpp create mode 100644 tests/yaml/test_flag_enum_with_int.hpp create mode 100644 tests/yaml/test_flatten.cpp create mode 100644 tests/yaml/test_flatten.hpp create mode 100644 tests/yaml/test_flatten_anonymous.cpp create mode 100644 tests/yaml/test_flatten_anonymous.hpp create mode 100644 tests/yaml/test_forward_list.cpp create mode 100644 tests/yaml/test_forward_list.hpp create mode 100644 tests/yaml/test_literal.cpp create mode 100644 tests/yaml/test_literal.hpp create mode 100644 tests/yaml/test_literal_map.cpp create mode 100644 tests/yaml/test_literal_map.hpp create mode 100644 tests/yaml/test_map.cpp create mode 100644 tests/yaml/test_map.hpp create mode 100644 tests/yaml/test_map_with_key_validation.cpp create mode 100644 tests/yaml/test_map_with_key_validation.hpp create mode 100644 tests/yaml/test_monster_example.cpp create mode 100644 tests/yaml/test_monster_example.hpp create mode 100644 tests/yaml/test_readme_example.cpp create mode 100644 tests/yaml/test_readme_example.hpp create mode 100644 tests/yaml/test_readme_example2.cpp create mode 100644 tests/yaml/test_readme_example2.hpp create mode 100644 tests/yaml/test_ref.cpp create mode 100644 tests/yaml/test_ref.hpp create mode 100644 tests/yaml/test_save_load.cpp create mode 100644 tests/yaml/test_save_load.hpp create mode 100644 tests/yaml/test_set.cpp create mode 100644 tests/yaml/test_set.hpp create mode 100644 tests/yaml/test_size.cpp create mode 100644 tests/yaml/test_size.hpp create mode 100644 tests/yaml/test_string_map.cpp create mode 100644 tests/yaml/test_string_map.hpp create mode 100644 tests/yaml/test_tagged_union.cpp create mode 100644 tests/yaml/test_tagged_union.hpp create mode 100644 tests/yaml/test_timestamp.cpp create mode 100644 tests/yaml/test_timestamp.hpp create mode 100644 tests/yaml/test_unique_ptr.cpp create mode 100644 tests/yaml/test_unique_ptr.hpp create mode 100644 tests/yaml/test_unique_ptr2.cpp create mode 100644 tests/yaml/test_unique_ptr2.hpp create mode 100644 tests/yaml/test_variant.cpp create mode 100644 tests/yaml/test_variant.hpp create mode 100644 tests/yaml/tests.cpp create mode 100644 tests/yaml/write_and_read.hpp diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index bbdc0c31..e31d9fbb 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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. diff --git a/.gitignore b/.gitignore index 9fb66da0..e5c9e168 100644 --- a/.gitignore +++ b/.gitignore @@ -44,5 +44,7 @@ *.json *.fb *.xml +*.yml +*.yaml !vcpkg.json diff --git a/CMakeLists.txt b/CMakeLists.txt index ffb2bf6e..b9d7c640 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index 728c92d8..cbdd9763 100644 --- a/README.md +++ b/README.md @@ -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 + +// ... (same as above) + +const std::string yaml_string = rfl::yaml::write(homer); +auto homer2 = rfl::yaml::read(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 diff --git a/docs/README.md b/docs/README.md index afeef020..96a40c68 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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. diff --git a/docs/supporting_your_own_format.md b/docs/supporting_your_own_format.md index 1b7d7537..5ef7f896 100644 --- a/docs/supporting_your_own_format.md +++ b/docs/supporting_your_own_format.md @@ -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 - 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 + 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 + 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 + 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 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 - std::array to_fields_array( + std::array, 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> children; + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + std::vector children; }; ``` This struct contains four fields. `rfl::parsing::Parser` expects `to_fields_array` -to return an `std::array` containing the field "firstName" in the +to return an `std::array, 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`. diff --git a/docs/yaml.md b/docs/yaml.md new file mode 100644 index 00000000..c180d681 --- /dev/null +++ b/docs/yaml.md @@ -0,0 +1,98 @@ +# YAML + +For YAML support, you must also include the header `` 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 children; +}; +``` + +You can parse YAML strings like this: + +```cpp +const rfl::Result result = rfl::yaml::read(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 result = rfl::yaml::load("/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 result = rfl::yaml::read(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 from_yaml_obj(const YAMLVar& _obj); +}; +``` + +And in your source file, you implement `from_yaml_obj` as follows: + +```cpp +rfl::Result Person::from_yaml_obj(const YAMLVar& _obj) { + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::yaml::read>(_obj) + .transform(from_nt); +} +``` + +This will force the compiler to only compile the YAML parsing when the +source file is compiled. + diff --git a/include/rfl/flexbuf/OutputArray.hpp b/include/rfl/flexbuf/OutputArray.hpp deleted file mode 100644 index 8a7b1f6a..00000000 --- a/include/rfl/flexbuf/OutputArray.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef FLEXBUF_OUTPUTARRAY_HPP_ -#define FLEXBUF_OUTPUTARRAY_HPP_ - -#include - -#include -#include - -#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& _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& _var) { vars_.push_back(_var); } - - private: - /// The underlying variables. - std::vector> vars_; -}; - -} // namespace flexbuf -} // namespace rfl - -#endif diff --git a/include/rfl/flexbuf/OutputNull.hpp b/include/rfl/flexbuf/OutputNull.hpp deleted file mode 100644 index ff29cc0c..00000000 --- a/include/rfl/flexbuf/OutputNull.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef FLEXBUF_OUTPUTNULL_HPP_ -#define FLEXBUF_OUTPUTNULL_HPP_ - -#include - -#include -#include - -#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& _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 diff --git a/include/rfl/flexbuf/OutputObject.hpp b/include/rfl/flexbuf/OutputObject.hpp deleted file mode 100644 index cb804bb3..00000000 --- a/include/rfl/flexbuf/OutputObject.hpp +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef FLEXBUF_OUTPUTOBJECT_HPP_ -#define FLEXBUF_OUTPUTOBJECT_HPP_ - -#include - -#include -#include -#include - -#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& _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& _var) { - vars_.push_back(std::make_pair(_name, _var)); - } - - private: - /// The underlying variables. - std::vector>> vars_; -}; - -} // namespace flexbuf -} // namespace rfl - -#endif diff --git a/include/rfl/flexbuf/OutputValue.hpp b/include/rfl/flexbuf/OutputValue.hpp deleted file mode 100644 index 3413c7b1..00000000 --- a/include/rfl/flexbuf/OutputValue.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef FLEXBUF_OUTPUTVALUE_HPP_ -#define FLEXBUF_OUTPUTVALUE_HPP_ - -#include - -#include -#include -#include - -#include "../Ref.hpp" -#include "../always_false.hpp" -#include "OutputVar.hpp" - -namespace rfl { -namespace flexbuf { - -template -class OutputValue : public OutputVar { - public: - OutputValue(T _val) : val_(_val) {} - - ~OutputValue() = default; - - /// Inserts all elements into the builder. - void insert(const std::optional& _key, - flexbuffers::Builder* _fbb) final { - if constexpr (std::is_same, std::string>()) { - if (_key) { - _fbb->String(_key->c_str(), val_); - } else { - _fbb->String(val_); - } - } else if constexpr (std::is_same, bool>()) { - if (_key) { - _fbb->Bool(_key->c_str(), val_); - } else { - _fbb->Bool(val_); - } - } else if constexpr (std::is_floating_point>()) { - if (_key) { - _fbb->Double(_key->c_str(), val_); - } else { - _fbb->Double(val_); - } - } else if constexpr (std::is_integral>()) { - if (_key) { - _fbb->Int(_key->c_str(), val_); - } else { - _fbb->Int(val_); - } - } else { - static_assert(always_false_v, "Unsupported type"); - } - }; - - /// Whether this is null. - bool is_null() const final { return false; } - - private: - /// The underlying value. - T val_; -}; - -} // namespace flexbuf -} // namespace rfl - -#endif diff --git a/include/rfl/flexbuf/OutputVar.hpp b/include/rfl/flexbuf/OutputVar.hpp deleted file mode 100644 index 9eb0acde..00000000 --- a/include/rfl/flexbuf/OutputVar.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef FLEXBUF_OUTPUTVAR_HPP_ -#define FLEXBUF_OUTPUTVAR_HPP_ - -#include - -#include -#include - -namespace rfl { -namespace flexbuf { - -class OutputVar { - public: - virtual ~OutputVar() = default; - - /// Inserts the value into the buffer. - virtual void insert(const std::optional& _key, - flexbuffers::Builder* _fbb) = 0; - - /// Whether this is null. - virtual bool is_null() const = 0; -}; - -} // namespace flexbuf -} // namespace rfl - -#endif diff --git a/include/rfl/flexbuf/Parser.hpp b/include/rfl/flexbuf/Parser.hpp index 2081a754..f57468e7 100644 --- a/include/rfl/flexbuf/Parser.hpp +++ b/include/rfl/flexbuf/Parser.hpp @@ -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 { diff --git a/include/rfl/flexbuf/Reader.hpp b/include/rfl/flexbuf/Reader.hpp index 284fd26e..1781b4f8 100644 --- a/include/rfl/flexbuf/Reader.hpp +++ b/include/rfl/flexbuf/Reader.hpp @@ -105,10 +105,6 @@ struct Reader { rfl::Result 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 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!"); } diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp index fd032507..edeed4ee 100644 --- a/include/rfl/flexbuf/Writer.hpp +++ b/include/rfl/flexbuf/Writer.hpp @@ -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; - using OutputObjectType = Ref; - using OutputVarType = Ref; + 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& _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::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 - OutputVarType from_basic_type(const T& _var) const noexcept { - return Ref>::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 + OutputVarType add_value_to_array(const T& _var, + OutputArrayType* _parent) const noexcept { + return insert_value(_var); + } + + template + 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 + OutputVarType insert_value(const std::string& _name, + const T& _var) const noexcept { + if constexpr (std::is_same, std::string>()) { + fbb_->String(_name.c_str(), _var); + } else if constexpr (std::is_same, bool>()) { + fbb_->Bool(_name.c_str(), _var); + } else if constexpr (std::is_floating_point>()) { + fbb_->Double(_name.c_str(), _var); + } else if constexpr (std::is_integral>()) { + fbb_->Int(_name.c_str(), _var); + } else { + static_assert(always_false_v, "Unsupported type"); + } + return OutputVarType{}; + } + + template + OutputVarType insert_value(const T& _var) const noexcept { + if constexpr (std::is_same, std::string>()) { + fbb_->String(_var); + } else if constexpr (std::is_same, bool>()) { + fbb_->Bool(_var); + } else if constexpr (std::is_floating_point>()) { + fbb_->Double(_var); + } else if constexpr (std::is_integral>()) { + fbb_->Int(_var); + } else { + static_assert(always_false_v, "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::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::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 fbb_; }; } // namespace flexbuf diff --git a/include/rfl/flexbuf/write.hpp b/include/rfl/flexbuf/write.hpp index ef4e7605..987e01d7 100644 --- a/include/rfl/flexbuf/write.hpp +++ b/include/rfl/flexbuf/write.hpp @@ -8,6 +8,8 @@ #include #include +#include "../Ref.hpp" +#include "../parsing/Parent.hpp" #include "Parser.hpp" namespace rfl { @@ -15,12 +17,12 @@ namespace flexbuf { template std::vector to_buffer(const T& _obj) { - auto w = Writer(); - const auto flexbuf_obj = Parser::write(w, _obj); - flexbuffers::Builder fbb; - flexbuf_obj->insert(std::nullopt, &fbb); - fbb.Finish(); - return fbb.GetBuffer(); + using ParentType = parsing::Parent; + const auto fbb = Ref::make(); + auto w = Writer(fbb); + Parser::write(w, _obj, typename ParentType::Root{}); + fbb->Finish(); + return fbb->GetBuffer(); } /// Writes an object to flexbuf. diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index bdc06e83..68442796 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -42,24 +43,10 @@ struct Reader { using InputObjectType = YYJSONInputObject; using InputVarType = YYJSONInputVar; - template - struct has_from_json_obj : std::false_type {}; - template - struct has_from_json_obj< - T, - std::enable_if_t::value>> - : std::true_type {}; - - template - struct has_from_json_obj, decltype(T::from_json_obj), - typename Reader::InputVarType>::value>> - : std::true_type {}; - - template - static constexpr bool has_custom_constructor = has_from_json_obj::value; + static constexpr bool has_custom_constructor = (requires(InputVarType var) { + T::from_json_obj(var); + }); rfl::Result get_field( const std::string& _name, const InputObjectType _obj) const noexcept { diff --git a/include/rfl/json/Writer.hpp b/include/rfl/json/Writer.hpp index 7a8490f5..ab3f32e5 100644 --- a/include/rfl/json/Writer.hpp +++ b/include/rfl/json/Writer.hpp @@ -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 + 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 + 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 + 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 OutputVarType from_basic_type(const T& _var) const noexcept { if constexpr (std::is_same, 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_; }; diff --git a/include/rfl/json/write.hpp b/include/rfl/json/write.hpp index 488c837c..9d108849 100644 --- a/include/rfl/json/write.hpp +++ b/include/rfl/json/write.hpp @@ -7,6 +7,7 @@ #include #include +#include "../parsing/Parent.hpp" #include "Parser.hpp" namespace rfl { @@ -15,9 +16,9 @@ namespace json { /// Returns a JSON string. template std::string write(const T& _obj) { + using ParentType = parsing::Parent; auto w = Writer(yyjson_mut_doc_new(NULL)); - const auto json_obj = Parser::write(w, _obj); - yyjson_mut_doc_set_root(w.doc_, json_obj.val_); + Parser::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 std::ostream& write(const T& _obj, std::ostream& _stream) { + using ParentType = parsing::Parent; auto w = Writer(yyjson_mut_doc_new(NULL)); - const auto json_obj = Parser::write(w, _obj); - yyjson_mut_doc_set_root(w.doc_, json_obj.val_); + Parser::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); diff --git a/include/rfl/parsing/CustomParser.hpp b/include/rfl/parsing/CustomParser.hpp index 7d29a3a2..0192b206 100644 --- a/include/rfl/parsing/CustomParser.hpp +++ b/include/rfl/parsing/CustomParser.hpp @@ -14,31 +14,33 @@ namespace parsing { template struct CustomParser { - static Result read(const ReaderType& _r, - const auto& _var) noexcept { - const auto to_class = [](auto&& _h) -> Result { - try { - if constexpr (internal::has_to_class_method_v) { - 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::read(_r, _var) - .and_then(to_class); - } + static Result read(const ReaderType& _r, + const auto& _var) noexcept { + const auto to_class = [](auto&& _h) -> Result { + try { + if constexpr (internal::has_to_class_method_v) { + 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::read(_r, _var) + .and_then(to_class); + } - static auto write(const WriterType& _w, const OriginalClass& _p) noexcept { - return Parser::write( - _w, HelperStruct::from_class(_p)); - } + template + static auto write(const WriterType& _w, const OriginalClass& _p, + const P& _parent) noexcept { + Parser::write( + _w, HelperStruct::from_class(_p), _parent); + } }; } // namespace parsing diff --git a/include/rfl/parsing/FieldVariantParser.hpp b/include/rfl/parsing/FieldVariantParser.hpp index c3c4ce56..a1aa6d90 100644 --- a/include/rfl/parsing/FieldVariantParser.hpp +++ b/include/rfl/parsing/FieldVariantParser.hpp @@ -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& _v) noexcept { + template + static void write(const W& _w, const std::variant& _v, + const P& _parent) noexcept { static_assert( internal::no_duplicate_field_names>(), "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; - return Parser::write(_w, named_tuple); + Parser::write(_w, named_tuple, _parent); }; - return std::visit(handle, _v); + std::visit(handle, _v); } private: diff --git a/include/rfl/parsing/IsReader.hpp b/include/rfl/parsing/IsReader.hpp index b8af6333..cd432303 100644 --- a/include/rfl/parsing/IsReader.hpp +++ b/include/rfl/parsing/IsReader.hpp @@ -43,7 +43,7 @@ concept IsReader = requires(R r, std::string name, /// floating point, std::string) { r.template to_basic_type(var) } -> std::same_as>; - /// 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. { diff --git a/include/rfl/parsing/IsWriter.hpp b/include/rfl/parsing/IsWriter.hpp index 7b70bb55..ee6223ec 100644 --- a/include/rfl/parsing/IsWriter.hpp +++ b/include/rfl/parsing/IsWriter.hpp @@ -9,39 +9,88 @@ namespace rfl { namespace parsing { template -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; - /// Appends var to the end of arr, thus mutating it. - { w.add(var, &arr) } -> std::same_as; + /// 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; - /// Returns an empty var (usually the NULL type). - { w.empty_var() } -> std::same_as; + /// Sets a null as the root element of the document. Returns OutputVarType + /// containing the null value. + { w.null_as_root() } -> std::same_as; - /// Generates a var from a basic type (std::string, bool, floating point or - /// integral). - { w.from_basic_type(t) } -> std::same_as; + /// 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; - /// Generates a new, empty array. - { w.new_array() } -> std::same_as; + /// 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; - /// Generates a new, empty object. - { w.new_object() } -> std::same_as; + /// 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; - /// Determines whether the var is empty (the NULL type). - { w.is_empty(var) } -> std::same_as; + /// 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; - /// Adds a new field to obj, thus mutating it. - { w.set_field(name, var, &obj) } -> std::same_as; + /// 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; + + /// 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; + + /// 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; + + /// Adds a null value to an array. Returns an + /// OutputVarType containing the null value. + { w.add_null_to_array(&arr) } -> std::same_as; + + /// 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; + + /// 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; + + /// 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; }; } // namespace parsing diff --git a/include/rfl/parsing/MapParser.hpp b/include/rfl/parsing/MapParser.hpp index 8b56095f..db0f33a9 100644 --- a/include/rfl/parsing/MapParser.hpp +++ b/include/rfl/parsing/MapParser.hpp @@ -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; using ValueType = std::decay_t; + using ParentType = Parent; + static Result 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 + 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>::write(_w, v); if constexpr (internal::has_reflection_type_v) { using ReflT = typename KeyType::ReflectionType; if constexpr (std::is_integral_v || std::is_floating_point_v) { - _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>::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>::write(_w, v, new_parent); } } else if constexpr (std::is_integral_v || std::is_floating_point_v) { - _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>::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>::write(_w, v, new_parent); } } - return obj; + _w.end_object(&obj); } private: diff --git a/include/rfl/parsing/NamedTupleParser.hpp b/include/rfl/parsing/NamedTupleParser.hpp index 863304d7..c6b97df6 100644 --- a/include/rfl/parsing/NamedTupleParser.hpp +++ b/include/rfl/parsing/NamedTupleParser.hpp @@ -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; + public: - /// Generates a NamedTuple from a JSON Object. static Result> 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& _tup) noexcept { - auto obj = _w.new_object(); + template + static void write(const W& _w, const NamedTuple& _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>::type; using ValueType = std::decay_t; - auto value = Parser::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()) { - if (!_w.is_empty(value)) { - _w.set_field(name, value, _ptr); + if (!is_empty(value)) { + if constexpr (internal::is_attribute_v) { + Parser::write(_w, value, + new_parent.as_attribute()); + } else { + Parser::write(_w, value, new_parent); + } } } else { - _w.set_field(name, value, _ptr); + if constexpr (internal::is_attribute_v) { + Parser::write(_w, value, new_parent.as_attribute()); + } else { + Parser::write(_w, value, new_parent); + } } return build_object_recursively<_i + 1>(_w, _tup, _ptr); } diff --git a/include/rfl/parsing/Parent.hpp b/include/rfl/parsing/Parent.hpp new file mode 100644 index 00000000..215c2a05 --- /dev/null +++ b/include/rfl/parsing/Parent.hpp @@ -0,0 +1,105 @@ +#ifndef RFL_PARSING_PARENT_HPP_ +#define RFL_PARSING_PARENT_HPP_ + +#include +#include + +#include "../always_false.hpp" +#include "supports_attributes.hpp" + +namespace rfl { +namespace parsing { + +template +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 + static OutputArrayType add_array(const W& _w, const size_t _size, + const ParentType& _parent) { + using Type = std::decay_t; + if constexpr (std::is_same()) { + return _w.add_array_to_array(_size, _parent.arr_); + } else if constexpr (std::is_same()) { + return _w.add_array_to_object(_parent.name_, _size, _parent.obj_); + } else if constexpr (std::is_same()) { + return _w.array_as_root(_size); + } else { + static_assert(always_false_v, "Unsupported option."); + } + } + + template + static OutputObjectType add_object(const W& _w, const size_t _size, + const ParentType& _parent) { + using Type = std::decay_t; + if constexpr (std::is_same()) { + return _w.add_object_to_array(_size, _parent.arr_); + } else if constexpr (std::is_same()) { + return _w.add_object_to_object(_parent.name_, _size, _parent.obj_); + } else if constexpr (std::is_same()) { + return _w.object_as_root(_size); + } else { + static_assert(always_false_v, "Unsupported option."); + } + } + + template + static OutputVarType add_null(const W& _w, const ParentType& _parent) { + using Type = std::decay_t; + if constexpr (std::is_same()) { + return _w.add_null_to_array(_parent.arr_); + } else if constexpr (std::is_same()) { + if constexpr (supports_attributes>) { + 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()) { + return _w.null_as_root(); + } else { + static_assert(always_false_v, "Unsupported option."); + } + } + + template + static OutputVarType add_value(const W& _w, const T& _var, + const ParentType& _parent) { + using Type = std::decay_t; + if constexpr (std::is_same()) { + return _w.add_value_to_array(_var, _parent.arr_); + } else if constexpr (std::is_same()) { + if constexpr (supports_attributes>) { + 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()) { + return _w.value_as_root(_var); + } else { + static_assert(always_false_v, "Unsupported option."); + } + } +}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser_array.hpp b/include/rfl/parsing/Parser_array.hpp index af5f666b..21cc8e17 100644 --- a/include/rfl/parsing/Parser_array.hpp +++ b/include/rfl/parsing/Parser_array.hpp @@ -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> { using OutputArrayType = typename W::OutputArrayType; using OutputVarType = typename W::OutputVarType; + using ParentType = Parent; + static Result> 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> { .and_then(extract); } - static OutputVarType write(const W& _w, - const std::array& _arr) noexcept { - auto arr = _w.new_array(); + template + static void write(const W& _w, const std::array& _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>::write(_w, *it), &arr); + Parser>::write(_w, *it, new_parent); } - return OutputVarType(arr); + _w.end_array(&arr); } private: diff --git a/include/rfl/parsing/Parser_box.hpp b/include/rfl/parsing/Parser_box.hpp index 3c391310..37541f63 100644 --- a/include/rfl/parsing/Parser_box.hpp +++ b/include/rfl/parsing/Parser_box.hpp @@ -21,8 +21,10 @@ struct Parser> { return Parser>::read(_r, _var).transform(to_box); } - static OutputVarType write(const W& _w, const Box& _box) noexcept { - return Parser>::write(_w, *_box); + template + static void write(const W& _w, const Box& _box, + const P& _parent) noexcept { + Parser>::write(_w, *_box, _parent); } }; diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 076d87b8..871ea997 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -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; + /// Expresses the variables as type T. static Result read(const R& _r, const InputVarType& _var) noexcept { if constexpr (R::template has_custom_constructor) { @@ -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 && internal::is_attribute_v) { - return _w.from_basic_type(resolve_reflection_type(_var), true); - } else if constexpr (internal::has_reflection_type_v) { + template + static void write(const W& _w, const T& _var, const P& _parent) noexcept { + if constexpr (internal::has_reflection_type_v) { using ReflectionType = std::decay_t; if constexpr (internal::has_reflection_method_v) { - return Parser::write(_w, _var.reflection()); + Parser::write(_w, _var.reflection(), _parent); } else { const auto& [r] = _var; - return Parser::write(_w, r); + Parser::write(_w, r, _parent); } } else if constexpr (std::is_class_v && std::is_aggregate_v) { const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var); using PtrNamedTupleType = std::decay_t; - return Parser::write(_w, ptr_named_tuple); + Parser::write(_w, ptr_named_tuple, _parent); } else if constexpr (std::is_enum_v) { using StringConverter = internal::enums::StringConverter; 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) { - return _w.from_basic_type(_var); + ParentType::add_value(_w, _var, _parent); } else { static_assert(always_false_v, "Unsupported type. Please refer to the sections on custom " @@ -97,20 +96,6 @@ struct Parser { "support for your own classes."); } } - - template - static const auto resolve_reflection_type(const V& _var) { - if constexpr (internal::has_reflection_type_v) { - if constexpr (internal::has_reflection_method_v) { - return resolve_reflection_type(_var.reflection()); - } else { - const auto& [r] = _var; - return resolve_reflection_type(r); - } - } else { - return _var; - } - } }; } // namespace parsing diff --git a/include/rfl/parsing/Parser_optional.hpp b/include/rfl/parsing/Parser_optional.hpp index 51b4da7a..28b62ca3 100644 --- a/include/rfl/parsing/Parser_optional.hpp +++ b/include/rfl/parsing/Parser_optional.hpp @@ -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> { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; + using ParentType = Parent; + static Result> read(const R& _r, const InputVarType& _var) noexcept { if (_r.is_empty(_var)) { @@ -26,11 +29,14 @@ struct Parser> { return Parser>::read(_r, _var).transform(to_opt); } - static OutputVarType write(const W& _w, const std::optional& _o) noexcept { + template + static void write(const W& _w, const std::optional& _o, + const P& _parent) noexcept { if (!_o) { - return _w.empty_var(); + ParentType::add_null(_w, _parent); + return; } - return Parser>::write(_w, *_o); + Parser>::write(_w, *_o, _parent); } }; diff --git a/include/rfl/parsing/Parser_pair.hpp b/include/rfl/parsing/Parser_pair.hpp index 669ec99e..64a53d70 100644 --- a/include/rfl/parsing/Parser_pair.hpp +++ b/include/rfl/parsing/Parser_pair.hpp @@ -29,12 +29,12 @@ struct Parser> { .transform(to_pair); } - /// Transform a std::pair into an array - static OutputVarType write( - const W& _w, const std::pair& _p) noexcept { + template + static void write(const W& _w, const std::pair& _p, + const P& _parent) noexcept { const auto tup = std::make_tuple(&_p.first, &_p.second); - return Parser>::write( - _w, tup); + Parser>::write( + _w, tup, _parent); } }; diff --git a/include/rfl/parsing/Parser_ptr.hpp b/include/rfl/parsing/Parser_ptr.hpp index a782c7a6..20981605 100644 --- a/include/rfl/parsing/Parser_ptr.hpp +++ b/include/rfl/parsing/Parser_ptr.hpp @@ -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 { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; + using ParentType = Parent; + /// Expresses the variables as type T. static Result read(const R& _r, const InputVarType& _var) noexcept { static_assert(always_false_v, @@ -27,12 +30,13 @@ struct Parser { return Error("Unsupported."); } - /// Expresses the variable a a JSON. - static OutputVarType write(const W& _w, const T* _ptr) noexcept { + template + 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>::write(_w, *_ptr); + Parser>::write(_w, *_ptr, _parent); } }; diff --git a/include/rfl/parsing/Parser_ref.hpp b/include/rfl/parsing/Parser_ref.hpp index f7a14e59..b3b06637 100644 --- a/include/rfl/parsing/Parser_ref.hpp +++ b/include/rfl/parsing/Parser_ref.hpp @@ -17,15 +17,15 @@ struct Parser> { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; - /// Expresses the variables as type T. static Result> read(const R& _r, const InputVarType& _var) noexcept { const auto to_ref = [&](auto&& _t) { return Ref::make(std::move(_t)); }; return Parser>::read(_r, _var).transform(to_ref); } - /// Expresses the variable a a JSON. - static OutputVarType write(const W& _w, const Ref& _ref) noexcept { - return Parser>::write(_w, *_ref); + template + static void write(const W& _w, const Ref& _ref, + const P& _parent) noexcept { + Parser>::write(_w, *_ref, _parent); } }; diff --git a/include/rfl/parsing/Parser_reference_wrapper.hpp b/include/rfl/parsing/Parser_reference_wrapper.hpp index efe6acee..fbc76dba 100644 --- a/include/rfl/parsing/Parser_reference_wrapper.hpp +++ b/include/rfl/parsing/Parser_reference_wrapper.hpp @@ -28,9 +28,10 @@ struct Parser> { return Error("Unsupported."); } - static OutputVarType write(const W& _w, - const std::reference_wrapper _ref) noexcept { - return Parser>::write(_w, _ref.get()); + template + static void write(const W& _w, const std::reference_wrapper _ref, + const P& _p) noexcept { + Parser>::write(_w, _ref.get(), _p); } }; diff --git a/include/rfl/parsing/Parser_rename.hpp b/include/rfl/parsing/Parser_rename.hpp index 3b7612da..ffa8257e 100644 --- a/include/rfl/parsing/Parser_rename.hpp +++ b/include/rfl/parsing/Parser_rename.hpp @@ -26,9 +26,10 @@ struct Parser> { return Parser>::read(_r, _var).transform(to_rename); } - static OutputVarType write(const W& _w, - const Rename<_name, T>& _rename) noexcept { - return Parser>::write(_w, _rename.value()); + template + static void write(const W& _w, const Rename<_name, T>& _rename, + const P& _parent) noexcept { + Parser>::write(_w, _rename.value(), _parent); } }; diff --git a/include/rfl/parsing/Parser_result.hpp b/include/rfl/parsing/Parser_result.hpp index db413533..e62afb71 100644 --- a/include/rfl/parsing/Parser_result.hpp +++ b/include/rfl/parsing/Parser_result.hpp @@ -39,17 +39,21 @@ struct Parser> { Parser::read(_r, _var).transform(to_res)); } - static OutputVarType write(const W& _w, const Result& _r) noexcept { - const auto write_t = [&](const auto& _t) -> OutputVarType { - return Parser>::write(_w, _t); + template + static void write(const W& _w, const Result& _r, + const P& _parent) noexcept { + const auto write_t = [&](const auto& _t) -> Nothing { + Parser>::write(_w, _t, _parent); + return Nothing{}; }; - const auto write_err = [&](const auto& _err) -> OutputVarType { - return Parser::write( - _w, ErrorType(make_field<"error">(_err.what()))); + const auto write_err = [&](const auto& _err) -> Nothing { + Parser::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); } }; diff --git a/include/rfl/parsing/Parser_rfl_variant.hpp b/include/rfl/parsing/Parser_rfl_variant.hpp index 85b68cef..d765a41a 100644 --- a/include/rfl/parsing/Parser_rfl_variant.hpp +++ b/include/rfl/parsing/Parser_rfl_variant.hpp @@ -30,10 +30,11 @@ struct Parser> { to_rfl_variant); } - static OutputVarType write( - const W& _w, const rfl::Variant& _variant) noexcept { - return Parser>::write(_w, - _variant.variant()); + template + static void write(const W& _w, const rfl::Variant& _variant, + const P& _parent) noexcept { + Parser>::write(_w, _variant.variant(), + _parent); } }; diff --git a/include/rfl/parsing/Parser_shared_ptr.hpp b/include/rfl/parsing/Parser_shared_ptr.hpp index 1268a570..942e1be1 100644 --- a/include/rfl/parsing/Parser_shared_ptr.hpp +++ b/include/rfl/parsing/Parser_shared_ptr.hpp @@ -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> { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; - /// Expresses the variables as type T. + using ParentType = Parent; + static Result> read(const R& _r, const InputVarType& _var) noexcept { if (_r.is_empty(*_var)) { @@ -29,13 +31,14 @@ struct Parser> { return Parser>::read(_r, _var).transform(to_ptr); } - /// Expresses the variable a a JSON. - static OutputVarType write(const W& _w, - const std::shared_ptr& _s) noexcept { + template + static OutputVarType write(const W& _w, const std::shared_ptr& _s, + const P& _parent) noexcept { if (!_s) { - return _w.empty_var(); + ParentType::add_null(_w, _parent); + return; } - return Parser>::write(_w, *_s); + Parser>::write(_w, *_s, _parent); } }; diff --git a/include/rfl/parsing/Parser_tagged_union.hpp b/include/rfl/parsing/Parser_tagged_union.hpp index ade04806..89a8ebbe 100644 --- a/include/rfl/parsing/Parser_tagged_union.hpp +++ b/include/rfl/parsing/Parser_tagged_union.hpp @@ -38,13 +38,15 @@ struct Parser> { 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 + 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> { } /// Writes a wrapped version of the original object, which contains the tag. - template - static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept { + template + static void write_wrapped(const W& _w, const T& _val, + const P& _parent) noexcept { const auto tag = internal::make_tag(); using TagType = std::decay_t; if constexpr (internal::has_fields>()) { using WrapperType = TaggedUnionWrapperWithFields; const auto wrapper = WrapperType{.tag = tag, .fields = &_val}; - return Parser::write(_w, wrapper); + Parser::write(_w, wrapper, _parent); } else { using WrapperType = TaggedUnionWrapperNoFields; const auto wrapper = WrapperType{.tag = tag, .fields = &_val}; - return Parser::write(_w, wrapper); + Parser::write(_w, wrapper, _parent); } } }; diff --git a/include/rfl/parsing/Parser_unique_ptr.hpp b/include/rfl/parsing/Parser_unique_ptr.hpp index 6a7d113e..ab23e66c 100644 --- a/include/rfl/parsing/Parser_unique_ptr.hpp +++ b/include/rfl/parsing/Parser_unique_ptr.hpp @@ -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> { using InputVarType = typename R::InputVarType; using OutputVarType = typename W::OutputVarType; + using ParentType = Parent; + static Result> read(const R& _r, const InputVarType& _var) noexcept { if (_r.is_empty(_var)) { @@ -28,12 +31,14 @@ struct Parser> { return Parser>::read(_r, _var).transform(to_ptr); } - static OutputVarType write(const W& _w, - const std::unique_ptr& _s) noexcept { + template + static void write(const W& _w, const std::unique_ptr& _s, + const P& _parent) noexcept { if (!_s) { - return _w.empty_var(); + ParentType::add_null(_w, _parent); + return; } - return Parser>::write(_w, *_s); + Parser>::write(_w, *_s, _parent); } }; diff --git a/include/rfl/parsing/Parser_variant.hpp b/include/rfl/parsing/Parser_variant.hpp index c341b767..f7264375 100644 --- a/include/rfl/parsing/Parser_variant.hpp +++ b/include/rfl/parsing/Parser_variant.hpp @@ -49,14 +49,15 @@ struct Parser> { } } - static OutputVarType write( - const W& _w, const std::variant& _variant) noexcept { + template + static void write(const W& _w, const std::variant& _variant, + const P& _parent) noexcept { if constexpr (internal::all_fields>()) { - return FieldVariantParser::write(_w, _variant); + FieldVariantParser::write(_w, _variant, _parent); } else { const auto handle = [&](const auto& _v) { using Type = std::decay_t; - return Parser::write(_w, _v); + Parser::write(_w, _v, _parent); }; return std::visit(handle, _variant); } diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index dd5e5905..54229212 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -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; + static Result> 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& _tup) noexcept { - auto arr = _w.new_array(); - to_array<0>(_w, _tup, &arr); - return arr; + template + static void write(const W& _w, const std::tuple& _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::read(_r, _vec[_i]); } - template + template static void to_array(const W& _w, const std::tuple& _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>::type>; - - const auto val = - Parser::write(_w, std::get<_i>(_tup)); - _w.add(val, _ptr); - to_array<_i + 1>(_w, _tup, _ptr); + Parser::write(_w, std::get<_i>(_tup), _parent); + to_array<_i + 1>(_w, _tup, _parent); } } }; diff --git a/include/rfl/parsing/VectorParser.hpp b/include/rfl/parsing/VectorParser.hpp index c1771fb7..19998196 100644 --- a/include/rfl/parsing/VectorParser.hpp +++ b/include/rfl/parsing/VectorParser.hpp @@ -1,6 +1,7 @@ #ifndef RFL_PARSING_VECTORPARSER_HPP_ #define RFL_PARSING_VECTORPARSER_HPP_ +#include #include #include #include @@ -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; + using T = typename VecType::value_type; static Result 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 + static void write(const W& _w, const VecType& _vec, + const P& _parent) noexcept { if constexpr (treat_as_map()) { - return MapParser::write(_w, _vec); + MapParser::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>::write(_w, v), &arr); + Parser>::write(_w, v, new_parent); } - return OutputVarType(arr); + _w.end_array(&arr); } } diff --git a/include/rfl/parsing/is_empty.hpp b/include/rfl/parsing/is_empty.hpp new file mode 100644 index 00000000..205f0d2d --- /dev/null +++ b/include/rfl/parsing/is_empty.hpp @@ -0,0 +1,39 @@ +#ifndef RFL_PARSING_IS_EMPTY_HPP_ +#define RFL_PARSING_IS_EMPTY_HPP_ + +#include + +#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 +static const bool is_empty(const T& _var) { + using Type = std::decay_t; + if constexpr (std::is_pointer_v) { + return !_var || is_empty(*_var); + } else if constexpr (internal::has_reflection_type_v) { + if constexpr (internal::has_reflection_method_v) { + return is_empty(_var.reflection()); + } else { + const auto& [r] = _var; + return is_empty(r); + } + } else if constexpr (internal::is_rename_v) { + return is_empty(_var.value()); + } else if constexpr (is_map_like_v || is_vector_like_v) { + return _var.begin() == _var.end(); + } else { + return !_var; + } +} + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/supports_attributes.hpp b/include/rfl/parsing/supports_attributes.hpp index 3ce55b44..ae0ae5c0 100644 --- a/include/rfl/parsing/supports_attributes.hpp +++ b/include/rfl/parsing/supports_attributes.hpp @@ -2,6 +2,7 @@ #define RFL_PARSING_SUPPORTSATTRIBUTES_HPP_ #include +#include #include "../Result.hpp" @@ -10,9 +11,15 @@ namespace parsing { /// Determines whether a writer supports attributes. template -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; + + { + w.add_null_to_object(name, &obj, is_attribute) } -> std::same_as; }; diff --git a/include/rfl/xml.hpp b/include/rfl/xml.hpp index 11ebeb89..dd3a8947 100644 --- a/include/rfl/xml.hpp +++ b/include/rfl/xml.hpp @@ -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" diff --git a/include/rfl/xml/OutputArray.hpp b/include/rfl/xml/OutputArray.hpp deleted file mode 100644 index 44296c2d..00000000 --- a/include/rfl/xml/OutputArray.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef RFL_XML_OUTPUTARRAY_HPP_ -#define RFL_XML_OUTPUTARRAY_HPP_ - -#include -#include -#include - -#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& _var) { vars_.push_back(_var); } - - private: - /// The underlying variables. - std::vector> vars_; -}; - -} // namespace xml -} // namespace rfl - -#endif diff --git a/include/rfl/xml/OutputNull.hpp b/include/rfl/xml/OutputNull.hpp deleted file mode 100644 index 347f0001..00000000 --- a/include/rfl/xml/OutputNull.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef RFL_XML_OUTPUTNULL_HPP_ -#define RFL_XML_OUTPUTNULL_HPP_ - -#include -#include -#include - -#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 diff --git a/include/rfl/xml/OutputObject.hpp b/include/rfl/xml/OutputObject.hpp deleted file mode 100644 index 1603b2ee..00000000 --- a/include/rfl/xml/OutputObject.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef RFL_XML_OUTPUTOBJECT_HPP_ -#define RFL_XML_OUTPUTOBJECT_HPP_ - -#include -#include -#include - -#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& _var) { - vars_.push_back(std::make_pair(_name, _var)); - } - - private: - /// The underlying variables. - std::vector>> vars_; -}; - -} // namespace xml -} // namespace rfl - -#endif diff --git a/include/rfl/xml/OutputValue.hpp b/include/rfl/xml/OutputValue.hpp deleted file mode 100644 index abd10e60..00000000 --- a/include/rfl/xml/OutputValue.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef RFL_XML_OUTPUTVALUE_HPP_ -#define RFL_XML_OUTPUTVALUE_HPP_ - -#include -#include -#include - -#include "../always_false.hpp" -#include "OutputVar.hpp" - -namespace rfl { -namespace xml { - -template -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::string>()) { - return val_; - } else if constexpr (std::is_same, bool>()) { - return val_ ? "true" : "false"; - } else if constexpr (std::is_floating_point>() || - std::is_integral>()) { - return std::to_string(val_); - } else { - static_assert(always_false_v, "Unsupported type"); - } - } - - private: - /// Whether you want this encoded as an attribute. - bool is_attribute_; - - /// The underlying value. - T val_; -}; - -} // namespace xml -} // namespace rfl - -#endif diff --git a/include/rfl/xml/OutputVar.hpp b/include/rfl/xml/OutputVar.hpp deleted file mode 100644 index a20c151b..00000000 --- a/include/rfl/xml/OutputVar.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef RFL_XML_OUTPUTVAR_HPP_ -#define RFL_XML_OUTPUTVAR_HPP_ - -#include -#include - -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 diff --git a/include/rfl/xml/Parser.hpp b/include/rfl/xml/Parser.hpp index 5a49d4b7..249ce166 100644 --- a/include/rfl/xml/Parser.hpp +++ b/include/rfl/xml/Parser.hpp @@ -3,13 +3,11 @@ #include -#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 { diff --git a/include/rfl/xml/Writer.hpp b/include/rfl/xml/Writer.hpp index cb3e5528..aa45737d 100644 --- a/include/rfl/xml/Writer.hpp +++ b/include/rfl/xml/Writer.hpp @@ -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; - using OutputObjectType = Ref; - using OutputVarType = Ref; + static constexpr const char* XML_CONTENT = "xml_content"; - Writer() {} + struct XMLOutputArray { + XMLOutputArray(const std::string& _name, const Ref& _node) + : name_(_name), node_(_node) {} + std::string name_; + Ref node_; + }; + + struct XMLOutputObject { + XMLOutputObject(const Ref& _node) : node_(_node) {} + Ref node_; + }; + + struct XMLOutputVar { + XMLOutputVar(const Ref& _node) : node_(_node) {} + Ref node_; + }; + + using OutputArrayType = XMLOutputArray; + using OutputObjectType = XMLOutputObject; + using OutputVarType = XMLOutputVar; + + Writer(const Ref& _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::make(root_->append_child(root_name_.c_str())); + return OutputArrayType(root_name_, node_child); } - OutputVarType empty_var() const noexcept { return make_ref(); } + OutputObjectType object_as_root(const size_t _size) const noexcept { + auto node_child = + Ref::make(root_->append_child(root_name_.c_str())); + return OutputObjectType(node_child); + } + + OutputVarType null_as_root() const noexcept { + auto node_child = + Ref::make(root_->append_child(root_name_.c_str())); + return OutputVarType(node_child); + } template - OutputVarType from_basic_type( - const T& _var, const bool _is_attribute = false) const noexcept { - return make_ref>(_var, _is_attribute); + OutputVarType value_as_root(const T& _var) const noexcept { + const auto str = to_string(_var); + auto node_child = + Ref::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(); } - - OutputObjectType new_object() const noexcept { - return make_ref(); + 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::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::make(_parent->node_->append_child(_name.c_str())); + return OutputObjectType(node_child); + } + + template + OutputVarType add_value_to_array(const T& _var, + OutputArrayType* _parent) const noexcept { + const auto str = to_string(_var); + auto node_child = Ref::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 + 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::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::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::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 + std::string to_string(const T& _val) const noexcept { + if constexpr (std::is_same, std::string>()) { + return _val; + } else if constexpr (std::is_same, bool>()) { + return _val ? "true" : "false"; + } else if constexpr (std::is_floating_point>() || + std::is_integral>()) { + return std::to_string(_val); + } else { + static_assert(always_false_v, "Unsupported type"); + } + } + + public: + Ref root_; + + std::string root_name_; }; } // namespace xml diff --git a/include/rfl/xml/is_required_for_xml.hpp b/include/rfl/xml/is_required_for_xml.hpp deleted file mode 100644 index d078a5f5..00000000 --- a/include/rfl/xml/is_required_for_xml.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef RFL_XML_IS_REQUIRED_FOR_XML_HPP_ -#define RFL_XML_IS_REQUIRED_FOR_XML_HPP_ - -#include - -#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 -consteval bool is_required_for_xml() { - using Type = std::decay_t>; - if constexpr (internal::has_reflection_type_v) { - return is_required_for_xml(); - } else { - return parsing::is_required() && !parsing::is_vector_like_v; - } -} - -} // namespace xml -} // namespace rfl - -#endif diff --git a/include/rfl/xml/write.hpp b/include/rfl/xml/write.hpp index 2b2011b2..8472844a 100644 --- a/include/rfl/xml/write.hpp +++ b/include/rfl/xml/write.hpp @@ -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 std::ostream& write(const T& _obj, std::ostream& _stream, const std::string& _indent = " ") { + using ParentType = parsing::Parent; + 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::write(w, _obj); + const auto doc = rfl::Ref::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::write(w, _obj, typename ParentType::Root{}); + + doc->save(_stream, _indent.c_str()); return _stream; } diff --git a/include/rfl/yaml.hpp b/include/rfl/yaml.hpp new file mode 100644 index 00000000..5b424013 --- /dev/null +++ b/include/rfl/yaml.hpp @@ -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 diff --git a/include/rfl/yaml/Parser.hpp b/include/rfl/yaml/Parser.hpp new file mode 100644 index 00000000..bb69f00c --- /dev/null +++ b/include/rfl/yaml/Parser.hpp @@ -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 +using Parser = parsing::Parser; + +} +} // namespace rfl + +#endif diff --git a/include/rfl/yaml/Reader.hpp b/include/rfl/yaml/Reader.hpp new file mode 100644 index 00000000..c9b0f18e --- /dev/null +++ b/include/rfl/yaml/Reader.hpp @@ -0,0 +1,151 @@ +#ifndef RFL_YAML_READER_HPP_ +#define RFL_YAML_READER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 + struct has_from_json_obj : std::false_type {}; + + template + static constexpr bool has_custom_constructor = (requires(InputVarType var) { + T::from_yaml_obj(var); + }); + + rfl::Result 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 + rfl::Result to_basic_type(const InputVarType& _var) const noexcept { + try { + if constexpr (std::is_same, std::string>() || + std::is_same, bool>() || + std::is_floating_point>() || + std::is_integral>()) { + return _var.node_.as>(); + } else { + static_assert(rfl::always_false_v, "Unsupported type."); + } + } catch (std::exception& e) { + return rfl::Error(e.what()); + } + } + + rfl::Result to_array( + const InputVarType& _var) const noexcept { + if (!_var.node_.IsSequence()) { + return rfl::Error("Could not cast to sequence!"); + } + return InputArrayType(_var.node_); + } + + template + std::array, size> to_fields_array( + const FunctionType& _fct, const InputObjectType& _obj) const noexcept { + std::array, size> f_arr; + for (const auto& p : _obj.node_) { + try { + const auto k = p.first.as(); + 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> to_map( + const InputObjectType& _obj) const noexcept { + std::vector> m; + for (const auto& p : _obj.node_) { + try { + auto k = p.first.as(); + m.emplace_back(std::make_pair(k, p.second)); + } catch (std::exception& e) { + continue; + } + } + return m; + } + + rfl::Result 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 to_vec(const InputArrayType& _arr) const noexcept { + std::vector vec; + for (size_t i = 0; i < _arr.node_.size(); ++i) { + vec.push_back(InputVarType(_arr.node_[i])); + } + return vec; + } + + template + rfl::Result 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 diff --git a/include/rfl/yaml/Writer.hpp b/include/rfl/yaml/Writer.hpp new file mode 100644 index 00000000..b056a6b2 --- /dev/null +++ b/include/rfl/yaml/Writer.hpp @@ -0,0 +1,147 @@ +#ifndef RFL_YAML_WRITER_HPP_ +#define RFL_YAML_WRITER_HPP_ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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& _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 + 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 + OutputVarType add_value_to_array(const T& _var, + OutputArrayType* _parent) const noexcept { + return insert_value(_var); + } + + template + 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 + OutputVarType insert_value(const std::string& _name, + const T& _var) const noexcept { + (*out_) << YAML::Key << _name << YAML::Value << _var; + return OutputVarType{}; + } + + template + 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 out_; +}; + +} // namespace yaml +} // namespace rfl + +#endif // JSON_PARSER_HPP_ diff --git a/include/rfl/yaml/load.hpp b/include/rfl/yaml/load.hpp new file mode 100644 index 00000000..255eee73 --- /dev/null +++ b/include/rfl/yaml/load.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 +Result load(const std::string& _fname) { + const auto read_string = [](const auto& _str) { return read(_str); }; + return rfl::io::load_string(_fname).and_then(read_string); +} + +} // namespace yaml +} // namespace rfl + +#endif diff --git a/include/rfl/yaml/read.hpp b/include/rfl/yaml/read.hpp new file mode 100644 index 00000000..c615460a --- /dev/null +++ b/include/rfl/yaml/read.hpp @@ -0,0 +1,46 @@ +#ifndef RFL_YAML_READ_HPP_ +#define RFL_YAML_READ_HPP_ + +#include + +#include +#include + +#include "Parser.hpp" +#include "Reader.hpp" + +namespace rfl { +namespace yaml { + +using InputVarType = typename Reader::InputVarType; + +/// Parses an object from a YAML var. +template +Result read(const InputVarType& _var) { + const auto r = Reader(); + return Parser::read(r, _var); +} + +/// Parses an object from YAML using reflection. +template +Result read(const std::string& _yaml_str) { + try { + const auto var = InputVarType(YAML::Load(_yaml_str)); + return read(var); + } catch (std::exception& e) { + return Error(e.what()); + } +} + +/// Parses an object from a stringstream. +template +Result read(std::istream& _stream) { + const auto yaml_str = std::string(std::istreambuf_iterator(_stream), + std::istreambuf_iterator()); + return read(yaml_str); +} + +} // namespace yaml +} // namespace rfl + +#endif diff --git a/include/rfl/yaml/save.hpp b/include/rfl/yaml/save.hpp new file mode 100644 index 00000000..2822be24 --- /dev/null +++ b/include/rfl/yaml/save.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_YAML_SAVE_HPP_ +#define RFL_YAML_SAVE_HPP_ + +#include +#include +#include + +#include "../Result.hpp" +#include "../io/save_string.hpp" +#include "write.hpp" + +namespace rfl { +namespace yaml { + +template +Result 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 diff --git a/include/rfl/yaml/write.hpp b/include/rfl/yaml/write.hpp new file mode 100644 index 00000000..e855e5ea --- /dev/null +++ b/include/rfl/yaml/write.hpp @@ -0,0 +1,41 @@ +#ifndef RFL_YAML_WRITE_HPP_ +#define RFL_YAML_WRITE_HPP_ + +#include + +#include +#include +#include +#include + +#include "../parsing/Parent.hpp" +#include "Parser.hpp" + +namespace rfl { +namespace yaml { + +/// Writes a XML into an ostream. +template +std::ostream& write(const T& _obj, std::ostream& _stream) { + using ParentType = parsing::Parent; + const auto out = Ref::make(); + auto w = Writer(out); + Parser::write(w, _obj, typename ParentType::Root{}); + _stream << out->c_str(); + return _stream; +} + +/// Returns a XML string. +template +std::string write(const T& _obj) { + using ParentType = parsing::Parent; + const auto out = Ref::make(); + auto w = Writer(out); + Parser::write(w, _obj, typename ParentType::Root{}); + return out->c_str(); +} + +} // namespace yaml +} // namespace rfl + +#endif // XML_PARSER_HPP_ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7fe72a52..cd8e0fb8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,3 +15,7 @@ endif() if (REFLECTCPP_XML) add_subdirectory(xml) endif() + +if (REFLECTCPP_YAML) + add_subdirectory(yaml) +endif() diff --git a/tests/flexbuffers/test_monster_example1.cpp b/tests/flexbuffers/test_monster_example1.cpp index 3701872c..b1db84b2 100644 --- a/tests/flexbuffers/test_monster_example1.cpp +++ b/tests/flexbuffers/test_monster_example1.cpp @@ -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 inventory; - Color color = Color::make<"Blue">(); + Color color = Color::Blue; std::vector weapons; Equipment equipped; std::vector 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)}; diff --git a/tests/yaml/CMakeLists.txt b/tests/yaml/CMakeLists.txt new file mode 100644 index 00000000..238ef474 --- /dev/null +++ b/tests/yaml/CMakeLists.txt @@ -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) + diff --git a/tests/yaml/test_array.cpp b/tests/yaml/test_array.cpp new file mode 100644 index 00000000..a4586d3b --- /dev/null +++ b/tests/yaml/test_array.cpp @@ -0,0 +1,40 @@ +#include "test_array.hpp" + +#include +#include +#include +#include +#include +#include + +// Make sure things still compile when +// rfl.hpp is included after rfl/yaml.hpp. +#include + +#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> 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{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read(homer); +} +} // namespace test_array diff --git a/tests/yaml/test_array.hpp b/tests/yaml/test_array.hpp new file mode 100644 index 00000000..502c3388 --- /dev/null +++ b/tests/yaml/test_array.hpp @@ -0,0 +1,4 @@ +namespace test_array{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_box.cpp b/tests/yaml/test_box.cpp new file mode 100644 index 00000000..04d88641 --- /dev/null +++ b/tests/yaml/test_box.cpp @@ -0,0 +1,50 @@ +#include "test_box.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#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 lesser; + rfl::Box 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{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); + +} +} // namespace test_box diff --git a/tests/yaml/test_box.hpp b/tests/yaml/test_box.hpp new file mode 100644 index 00000000..a564b9e1 --- /dev/null +++ b/tests/yaml/test_box.hpp @@ -0,0 +1,4 @@ +namespace test_box{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_class1.cpp b/tests/yaml/test_custom_class1.cpp new file mode 100644 index 00000000..35f4a1da --- /dev/null +++ b/tests/yaml/test_custom_class1.cpp @@ -0,0 +1,42 @@ +#include "test_custom_class1.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#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 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 diff --git a/tests/yaml/test_custom_class1.hpp b/tests/yaml/test_custom_class1.hpp new file mode 100644 index 00000000..eafe6cd0 --- /dev/null +++ b/tests/yaml/test_custom_class1.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class1{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_class3.cpp b/tests/yaml/test_custom_class3.cpp new file mode 100644 index 00000000..2ea105e4 --- /dev/null +++ b/tests/yaml/test_custom_class3.cpp @@ -0,0 +1,68 @@ +#include "test_custom_class3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#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 +struct Parser + : public CustomParser {}; + +} // 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 diff --git a/tests/yaml/test_custom_class3.hpp b/tests/yaml/test_custom_class3.hpp new file mode 100644 index 00000000..9a6fdab4 --- /dev/null +++ b/tests/yaml/test_custom_class3.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class3{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_class4.cpp b/tests/yaml/test_custom_class4.cpp new file mode 100644 index 00000000..e2d79a94 --- /dev/null +++ b/tests/yaml/test_custom_class4.cpp @@ -0,0 +1,69 @@ +#include "test_custom_class4.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class4 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_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 last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> 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(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class4 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // 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("Simpson"), 10); + + write_and_read(bart); +} +} // namespace test_custom_class4 diff --git a/tests/yaml/test_custom_class4.hpp b/tests/yaml/test_custom_class4.hpp new file mode 100644 index 00000000..2d3b151a --- /dev/null +++ b/tests/yaml/test_custom_class4.hpp @@ -0,0 +1,4 @@ +namespace test_custom_class4{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_custom_constructor.cpp b/tests/yaml/test_custom_constructor.cpp new file mode 100644 index 00000000..3bff855c --- /dev/null +++ b/tests/yaml/test_custom_constructor.cpp @@ -0,0 +1,57 @@ +#include "test_custom_constructor.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_constructor { + +struct Person { + static rfl::Result 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(std::move(_nt)); + }; + return rfl::yaml::read>(_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( + 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 diff --git a/tests/yaml/test_custom_constructor.hpp b/tests/yaml/test_custom_constructor.hpp new file mode 100644 index 00000000..013721d0 --- /dev/null +++ b/tests/yaml/test_custom_constructor.hpp @@ -0,0 +1,4 @@ +namespace test_custom_constructor{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_default_values.cpp b/tests/yaml/test_default_values.cpp new file mode 100644 index 00000000..f6af2352 --- /dev/null +++ b/tests/yaml/test_default_values.cpp @@ -0,0 +1,32 @@ +#include "test_default_values.hpp" + +#include +#include +#include +#include +#include +#include + +#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 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({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_default_values diff --git a/tests/yaml/test_default_values.hpp b/tests/yaml/test_default_values.hpp new file mode 100644 index 00000000..c8f8360e --- /dev/null +++ b/tests/yaml/test_default_values.hpp @@ -0,0 +1,4 @@ +namespace test_default_values{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_deque.cpp b/tests/yaml/test_deque.cpp new file mode 100644 index 00000000..6dfa5803 --- /dev/null +++ b/tests/yaml/test_deque.cpp @@ -0,0 +1,33 @@ +#include "test_deque.hpp" + +#include +#include +#include +#include +#include + +#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> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + 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 diff --git a/tests/yaml/test_deque.hpp b/tests/yaml/test_deque.hpp new file mode 100644 index 00000000..6781e880 --- /dev/null +++ b/tests/yaml/test_deque.hpp @@ -0,0 +1,4 @@ +namespace test_deque{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_enum.cpp b/tests/yaml/test_enum.cpp new file mode 100644 index 00000000..d4d56a8b --- /dev/null +++ b/tests/yaml/test_enum.cpp @@ -0,0 +1,29 @@ +#include "test_enum.hpp" + +#include +#include +#include +#include +#include +#include + +#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 diff --git a/tests/yaml/test_enum.hpp b/tests/yaml/test_enum.hpp new file mode 100644 index 00000000..2e2e0b3d --- /dev/null +++ b/tests/yaml/test_enum.hpp @@ -0,0 +1,4 @@ +namespace test_enum { +void test(); +} + diff --git a/tests/yaml/test_field_variant.cpp b/tests/yaml/test_field_variant.cpp new file mode 100644 index 00000000..8b17665f --- /dev/null +++ b/tests/yaml/test_field_variant.cpp @@ -0,0 +1,39 @@ +#include "test_field_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#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<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + +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 diff --git a/tests/yaml/test_field_variant.hpp b/tests/yaml/test_field_variant.hpp new file mode 100644 index 00000000..ba93e732 --- /dev/null +++ b/tests/yaml/test_field_variant.hpp @@ -0,0 +1,4 @@ +namespace test_field_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_flag_enum.cpp b/tests/yaml/test_flag_enum.cpp new file mode 100644 index 00000000..1d3f76af --- /dev/null +++ b/tests/yaml/test_flag_enum.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include + +#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(static_cast(c1) | static_cast(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 diff --git a/tests/yaml/test_flag_enum.hpp b/tests/yaml/test_flag_enum.hpp new file mode 100644 index 00000000..2f4dc7a0 --- /dev/null +++ b/tests/yaml/test_flag_enum.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum { +void test(); +} + diff --git a/tests/yaml/test_flag_enum_with_int.cpp b/tests/yaml/test_flag_enum_with_int.cpp new file mode 100644 index 00000000..4c7deefe --- /dev/null +++ b/tests/yaml/test_flag_enum_with_int.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#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(static_cast(c1) | static_cast(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(10000)}; + + write_and_read(circle); +} + +} // namespace test_flag_enum_with_int diff --git a/tests/yaml/test_flag_enum_with_int.hpp b/tests/yaml/test_flag_enum_with_int.hpp new file mode 100644 index 00000000..a7512b60 --- /dev/null +++ b/tests/yaml/test_flag_enum_with_int.hpp @@ -0,0 +1,4 @@ +namespace test_flag_enum_with_int { +void test(); +} + diff --git a/tests/yaml/test_flatten.cpp b/tests/yaml/test_flatten.cpp new file mode 100644 index 00000000..c7f1d9be --- /dev/null +++ b/tests/yaml/test_flatten.cpp @@ -0,0 +1,38 @@ +#include "test_flatten.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> 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("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} +} // namespace test_flatten diff --git a/tests/yaml/test_flatten.hpp b/tests/yaml/test_flatten.hpp new file mode 100644 index 00000000..24d60e11 --- /dev/null +++ b/tests/yaml/test_flatten.hpp @@ -0,0 +1,4 @@ +namespace test_flatten{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_flatten_anonymous.cpp b/tests/yaml/test_flatten_anonymous.cpp new file mode 100644 index 00000000..05e61464 --- /dev/null +++ b/tests/yaml/test_flatten_anonymous.cpp @@ -0,0 +1,39 @@ +#include "test_flatten_anonymous.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten_anonymous { + +struct Person { + std::string first_name; + rfl::Box last_name; + int age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Box 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("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} + +} // namespace test_flatten_anonymous diff --git a/tests/yaml/test_flatten_anonymous.hpp b/tests/yaml/test_flatten_anonymous.hpp new file mode 100644 index 00000000..7ffa2785 --- /dev/null +++ b/tests/yaml/test_flatten_anonymous.hpp @@ -0,0 +1,4 @@ +namespace test_flatten_anonymous{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_forward_list.cpp b/tests/yaml/test_forward_list.cpp new file mode 100644 index 00000000..dd8d9e56 --- /dev/null +++ b/tests/yaml/test_forward_list.cpp @@ -0,0 +1,33 @@ +#include "test_forward_list.hpp" + +#include +#include +#include +#include +#include + +#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> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + 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 diff --git a/tests/yaml/test_forward_list.hpp b/tests/yaml/test_forward_list.hpp new file mode 100644 index 00000000..9784a0c4 --- /dev/null +++ b/tests/yaml/test_forward_list.hpp @@ -0,0 +1,4 @@ +namespace test_forward_list{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_literal.cpp b/tests/yaml/test_literal.cpp new file mode 100644 index 00000000..00578178 --- /dev/null +++ b/tests/yaml/test_literal.cpp @@ -0,0 +1,30 @@ +#include "test_literal.hpp" + +#include +#include +#include +#include +#include +#include + +#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 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 diff --git a/tests/yaml/test_literal.hpp b/tests/yaml/test_literal.hpp new file mode 100644 index 00000000..ccd500ef --- /dev/null +++ b/tests/yaml/test_literal.hpp @@ -0,0 +1,4 @@ +namespace test_literal{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_literal_map.cpp b/tests/yaml/test_literal_map.cpp new file mode 100644 index 00000000..9bbebafb --- /dev/null +++ b/tests/yaml/test_literal_map.cpp @@ -0,0 +1,28 @@ +#include "test_literal_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#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> homer; + homer.insert(std::make_pair(FieldName::make<"firstName">(), + std::make_unique("Homer"))); + homer.insert(std::make_pair(FieldName::make<"lastName">(), + std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_literal_map diff --git a/tests/yaml/test_literal_map.hpp b/tests/yaml/test_literal_map.hpp new file mode 100644 index 00000000..cc05d0c8 --- /dev/null +++ b/tests/yaml/test_literal_map.hpp @@ -0,0 +1,4 @@ +namespace test_literal_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_map.cpp b/tests/yaml/test_map.cpp new file mode 100644 index 00000000..e37ce564 --- /dev/null +++ b/tests/yaml/test_map.cpp @@ -0,0 +1,32 @@ +#include "test_map.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::map children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::map(); + children.insert(std::make_pair("child1", Person{.first_name = "Bart"})); + children.insert(std::make_pair("child2", Person{.first_name = "Lisa"})); + children.insert(std::make_pair("child3", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map diff --git a/tests/yaml/test_map.hpp b/tests/yaml/test_map.hpp new file mode 100644 index 00000000..9ae49728 --- /dev/null +++ b/tests/yaml/test_map.hpp @@ -0,0 +1,4 @@ +namespace test_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_map_with_key_validation.cpp b/tests/yaml/test_map_with_key_validation.cpp new file mode 100644 index 00000000..e758219d --- /dev/null +++ b/tests/yaml/test_map_with_key_validation.cpp @@ -0,0 +1,33 @@ +#include "test_map_with_key_validation.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map_with_key_validation { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + + children->insert(std::make_pair("Bart", Person{.first_name = "Bart"})); + children->insert(std::make_pair("Lisa", Person{.first_name = "Lisa"})); + children->insert(std::make_pair("Maggie", Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_map_with_key_validation diff --git a/tests/yaml/test_map_with_key_validation.hpp b/tests/yaml/test_map_with_key_validation.hpp new file mode 100644 index 00000000..1372f926 --- /dev/null +++ b/tests/yaml/test_map_with_key_validation.hpp @@ -0,0 +1,4 @@ +namespace test_map_with_key_validation{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_monster_example.cpp b/tests/yaml/test_monster_example.cpp new file mode 100644 index 00000000..052d8390 --- /dev/null +++ b/tests/yaml/test_monster_example.cpp @@ -0,0 +1,64 @@ +#include "test_monster_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example { + +using Color = rfl::Literal<"Red", "Green", "Blue">; + +struct Weapon { + std::string name; + short damage; +}; + +using Equipment = rfl::Variant>; + +struct Vec3 { + float x; + float y; + float z; +}; + +struct Monster { + Vec3 pos; + short mana = 150; + short hp = 100; + std::string name; + bool friendly = false; + std::vector inventory; + Color color = Color::make<"Blue">(); + std::vector weapons; + Equipment equipped; + std::vector path; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto sword = Weapon{.name = "Sword", .damage = 3}; + const auto axe = Weapon{.name = "Axe", .damage = 5}; + + const auto weapons = std::vector({sword, axe}); + + const auto position = Vec3{1.0f, 2.0f, 3.0f}; + + const auto inventory = std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + + const auto orc = Monster{.pos = position, + .mana = 150, + .hp = 80, + .name = "MyMonster", + .inventory = inventory, + .color = Color::make<"Red">(), + .weapons = weapons, + .equipped = rfl::make_field<"weapon">(axe)}; + + write_and_read(orc); +} +} // namespace test_monster_example diff --git a/tests/yaml/test_monster_example.hpp b/tests/yaml/test_monster_example.hpp new file mode 100644 index 00000000..f2d959fc --- /dev/null +++ b/tests/yaml/test_monster_example.hpp @@ -0,0 +1,4 @@ +namespace test_monster_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_readme_example.cpp b/tests/yaml/test_readme_example.cpp new file mode 100644 index 00000000..4e6f8ee0 --- /dev/null +++ b/tests/yaml/test_readme_example.cpp @@ -0,0 +1,51 @@ +#include "test_readme_example.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example { + +using Age = rfl::Validator, rfl::Maximum<130>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::string town = "Springfield"; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector child; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = Person{.first_name = "Homer", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .child = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_readme_example diff --git a/tests/yaml/test_readme_example.hpp b/tests/yaml/test_readme_example.hpp new file mode 100644 index 00000000..68c6cf81 --- /dev/null +++ b/tests/yaml/test_readme_example.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_readme_example2.cpp b/tests/yaml/test_readme_example2.cpp new file mode 100644 index 00000000..a2d212a4 --- /dev/null +++ b/tests/yaml/test_readme_example2.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#include "test_readme_example.hpp" +#include "write_and_read.hpp" + +namespace test_readme_example2 { + +struct Person { + std::string first_name; + std::string last_name; + int age; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_readme_example2 diff --git a/tests/yaml/test_readme_example2.hpp b/tests/yaml/test_readme_example2.hpp new file mode 100644 index 00000000..5c6b011c --- /dev/null +++ b/tests/yaml/test_readme_example2.hpp @@ -0,0 +1,4 @@ +namespace test_readme_example2 { +void test(); +} + diff --git a/tests/yaml/test_ref.cpp b/tests/yaml/test_ref.cpp new file mode 100644 index 00000000..32a32e6c --- /dev/null +++ b/tests/yaml/test_ref.cpp @@ -0,0 +1,49 @@ +#include "test_ref.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_ref { + +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::Ref lesser; + rfl::Ref 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; + + const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_ref(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); + +} +} // namespace test_ref diff --git a/tests/yaml/test_ref.hpp b/tests/yaml/test_ref.hpp new file mode 100644 index 00000000..d289ba09 --- /dev/null +++ b/tests/yaml/test_ref.hpp @@ -0,0 +1,4 @@ +namespace test_ref{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_save_load.cpp b/tests/yaml/test_save_load.cpp new file mode 100644 index 00000000..c20fc181 --- /dev/null +++ b/tests/yaml/test_save_load.cpp @@ -0,0 +1,70 @@ +#include "test_save_load.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace test_save_load { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::yaml::save("homer.yaml", homer1); + + const auto homer2 = rfl::yaml::load("homer.yaml").value(); + + const auto string1 = rfl::yaml::write(homer1); + const auto string2 = rfl::yaml::write(homer2); + + if (string1 != string2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_save_load diff --git a/tests/yaml/test_save_load.hpp b/tests/yaml/test_save_load.hpp new file mode 100644 index 00000000..7bf10359 --- /dev/null +++ b/tests/yaml/test_save_load.hpp @@ -0,0 +1,4 @@ +namespace test_save_load { +void test(); +} + diff --git a/tests/yaml/test_set.cpp b/tests/yaml/test_set.cpp new file mode 100644 index 00000000..9f2516ce --- /dev/null +++ b/tests/yaml/test_set.cpp @@ -0,0 +1,30 @@ +#include "test_set.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_set { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_set diff --git a/tests/yaml/test_set.hpp b/tests/yaml/test_set.hpp new file mode 100644 index 00000000..142a663b --- /dev/null +++ b/tests/yaml/test_set.hpp @@ -0,0 +1,4 @@ +namespace test_set{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_size.cpp b/tests/yaml/test_size.cpp new file mode 100644 index 00000000..c37b9bd8 --- /dev/null +++ b/tests/yaml/test_size.cpp @@ -0,0 +1,42 @@ +#include "test_size.hpp" + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_size { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + rfl::Validator, + rfl::Size, rfl::EqualTo<3>>>> + children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{ + .first_name = "Bart", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto lisa = Person{ + .first_name = "Lisa", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto maggie = Person{ + .first_name = "Maggie", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_size diff --git a/tests/yaml/test_size.hpp b/tests/yaml/test_size.hpp new file mode 100644 index 00000000..be330df0 --- /dev/null +++ b/tests/yaml/test_size.hpp @@ -0,0 +1,4 @@ +namespace test_size{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_string_map.cpp b/tests/yaml/test_string_map.cpp new file mode 100644 index 00000000..cb4fb903 --- /dev/null +++ b/tests/yaml/test_string_map.cpp @@ -0,0 +1,24 @@ +#include "test_string_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_string_map diff --git a/tests/yaml/test_string_map.hpp b/tests/yaml/test_string_map.hpp new file mode 100644 index 00000000..94cb975a --- /dev/null +++ b/tests/yaml/test_string_map.hpp @@ -0,0 +1,4 @@ +namespace test_string_map{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_tagged_union.cpp b/tests/yaml/test_tagged_union.cpp new file mode 100644 index 00000000..a9dd1abc --- /dev/null +++ b/tests/yaml/test_tagged_union.cpp @@ -0,0 +1,36 @@ +#include "test_tagged_union.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_tagged_union diff --git a/tests/yaml/test_tagged_union.hpp b/tests/yaml/test_tagged_union.hpp new file mode 100644 index 00000000..5d522ff9 --- /dev/null +++ b/tests/yaml/test_tagged_union.hpp @@ -0,0 +1,4 @@ +namespace test_tagged_union{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_timestamp.cpp b/tests/yaml/test_timestamp.cpp new file mode 100644 index 00000000..8af261d1 --- /dev/null +++ b/tests/yaml/test_timestamp.cpp @@ -0,0 +1,36 @@ +#include "test_timestamp.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_timestamp { + +using TS = rfl::Timestamp<"%Y-%m-%d">; + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + TS birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto result = TS::from_string("nonsense"); + + if (result) { + std::cout << "Failed: Expected an error, but got none." << std::endl; + return; + } + + const auto bart = Person{.first_name = "Bart", .birthday = "1987-04-19"}; + + write_and_read(bart); +} +} // namespace test_timestamp diff --git a/tests/yaml/test_timestamp.hpp b/tests/yaml/test_timestamp.hpp new file mode 100644 index 00000000..891d89b9 --- /dev/null +++ b/tests/yaml/test_timestamp.hpp @@ -0,0 +1,4 @@ +namespace test_timestamp{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_unique_ptr.cpp b/tests/yaml/test_unique_ptr.cpp new file mode 100644 index 00000000..b1833533 --- /dev/null +++ b/tests/yaml/test_unique_ptr.cpp @@ -0,0 +1,33 @@ +#include "test_unique_ptr.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>(); + 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_unique_ptr diff --git a/tests/yaml/test_unique_ptr.hpp b/tests/yaml/test_unique_ptr.hpp new file mode 100644 index 00000000..428ea2a2 --- /dev/null +++ b/tests/yaml/test_unique_ptr.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_unique_ptr2.cpp b/tests/yaml/test_unique_ptr2.cpp new file mode 100644 index 00000000..e3007267 --- /dev/null +++ b/tests/yaml/test_unique_ptr2.cpp @@ -0,0 +1,48 @@ +#include "test_unique_ptr2.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr2 { + +struct DecisionTree { + struct Leaf { + using Tag = rfl::Literal<"Leaf">; + double value; + }; + + struct Node { + using Tag = rfl::Literal<"Node">; + rfl::Rename<"criticalValue", double> critical_value; + std::unique_ptr lesser; + std::unique_ptr 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 = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_unique_ptr2 diff --git a/tests/yaml/test_unique_ptr2.hpp b/tests/yaml/test_unique_ptr2.hpp new file mode 100644 index 00000000..74adc170 --- /dev/null +++ b/tests/yaml/test_unique_ptr2.hpp @@ -0,0 +1,4 @@ +namespace test_unique_ptr2{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/test_variant.cpp b/tests/yaml/test_variant.cpp new file mode 100644 index 00000000..cc3d6cbe --- /dev/null +++ b/tests/yaml/test_variant.cpp @@ -0,0 +1,36 @@ +#include "test_variant.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_variant { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +using Shapes = std::variant>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_variant diff --git a/tests/yaml/test_variant.hpp b/tests/yaml/test_variant.hpp new file mode 100644 index 00000000..0e58ce71 --- /dev/null +++ b/tests/yaml/test_variant.hpp @@ -0,0 +1,4 @@ +namespace test_variant{ + void test(); +} + \ No newline at end of file diff --git a/tests/yaml/tests.cpp b/tests/yaml/tests.cpp new file mode 100644 index 00000000..3a1bcf26 --- /dev/null +++ b/tests/yaml/tests.cpp @@ -0,0 +1,67 @@ +#include "test_array.hpp" +#include "test_box.hpp" +#include "test_custom_class1.hpp" +#include "test_custom_class3.hpp" +#include "test_custom_class4.hpp" +#include "test_custom_constructor.hpp" +#include "test_default_values.hpp" +#include "test_deque.hpp" +#include "test_enum.hpp" +#include "test_field_variant.hpp" +#include "test_flag_enum.hpp" +#include "test_flag_enum_with_int.hpp" +#include "test_flatten.hpp" +#include "test_flatten_anonymous.hpp" +#include "test_forward_list.hpp" +#include "test_literal.hpp" +#include "test_literal_map.hpp" +#include "test_map.hpp" +#include "test_map_with_key_validation.hpp" +#include "test_monster_example.hpp" +#include "test_readme_example.hpp" +#include "test_readme_example2.hpp" +#include "test_ref.hpp" +#include "test_save_load.hpp" +#include "test_set.hpp" +#include "test_size.hpp" +#include "test_tagged_union.hpp" +#include "test_timestamp.hpp" +#include "test_unique_ptr.hpp" +#include "test_unique_ptr2.hpp" +#include "test_variant.hpp" + +int main() { + test_readme_example::test(); + test_readme_example2::test(); + test_flatten::test(); + test_flatten_anonymous::test(); + test_enum::test(); + test_flag_enum::test(); + test_flag_enum_with_int::test(); + test_map::test(); + test_map_with_key_validation::test(); + test_variant::test(); + test_field_variant::test(); + test_tagged_union::test(); + test_deque::test(); + test_forward_list::test(); + test_literal_map::test(); + test_literal::test(); + test_monster_example::test(); + test_ref::test(); + test_set::test(); + test_size::test(); + test_timestamp::test(); + test_unique_ptr::test(); + test_unique_ptr2::test(); + test_array::test(); + test_box::test(); + test_custom_class1::test(); + test_custom_class3::test(); + test_custom_class4::test(); + test_default_values::test(); + test_custom_constructor::test(); + test_save_load::test(); + + return 0; +} diff --git a/tests/yaml/write_and_read.hpp b/tests/yaml/write_and_read.hpp new file mode 100644 index 00000000..6f6e2743 --- /dev/null +++ b/tests/yaml/write_and_read.hpp @@ -0,0 +1,32 @@ +#ifndef WRITE_AND_READ_ +#define WRITE_AND_READ_ + +#include +#include +#include + +template +void write_and_read(const T& _struct) { + const auto yaml_string1 = rfl::yaml::write(_struct); + const auto res = rfl::yaml::read(yaml_string1); + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl + << "Original string: " << yaml_string1 << std::endl + << std::endl; + return; + } + const auto yaml_string2 = rfl::yaml::write(res.value()); + if (yaml_string2 != yaml_string1) { + std::cout << "Test failed on read. Expected:" << std::endl + << yaml_string1 << std::endl + << "Got: " << std::endl + << yaml_string2 << std::endl + << std::endl; + return; + } + // std::cout << yaml_string1 << std::endl; + std::cout << "OK" << std::endl << std::endl; +} + +#endif diff --git a/vcpkg.json b/vcpkg.json index 85839dc5..b25ce3ae 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -11,6 +11,10 @@ "name": "pugixml", "version>=": "1.14" }, + { + "name": "yaml-cpp", + "version>=": "0.8.0#1" + }, { "name": "vcpkg-cmake", "version>=": "2023-05-04"