Added TOML support

This commit is contained in:
Patrick Urbanke
2024-03-29 12:24:43 +01:00
parent 1f2e216c75
commit 1f0dcf591e
86 changed files with 2109 additions and 7 deletions
+4 -2
View File
@@ -20,12 +20,13 @@ jobs:
- name: Run test
run: |
g++ --version
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build -j 4
./build/tests/bson/reflect-cpp-bson-tests
./build/tests/cbor/reflect-cpp-cbor-tests
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
./build/tests/json/reflect-cpp-json-tests
./build/tests/toml/reflect-cpp-toml-tests
./build/tests/xml/reflect-cpp-xml-tests
./build/tests/yaml/reflect-cpp-yaml-tests
@@ -53,12 +54,13 @@ jobs:
CXX: clang++
run: |
clang --version
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build -j 4
./build/tests/bson/reflect-cpp-bson-tests
./build/tests/cbor/reflect-cpp-cbor-tests
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
./build/tests/json/reflect-cpp-json-tests
./build/tests/toml/reflect-cpp-toml-tests
./build/tests/xml/reflect-cpp-xml-tests
./build/tests/yaml/reflect-cpp-yaml-tests
+1
View File
@@ -45,6 +45,7 @@
*.cbor
*.json
*.fb
*.toml
*.xml
*.yml
*.yaml
+8 -1
View File
@@ -5,12 +5,13 @@ option(REFLECTCPP_BSON "Enable BSON support" OFF)
option(REFLECTCPP_CBOR "Enable CBOR support" OFF)
option(REFLECTCPP_FLEXBUFFERS "Enable flexbuffers support" OFF)
option(REFLECTCPP_XML "Enable XML support" OFF)
option(REFLECTCPP_TOML "Enable TOML 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_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_YAML)
if (REFLECTCPP_BSON OR REFLECTCPP_CBOR OR REFLECTCPP_FLEXBUFFERS OR REFLECTCPP_XML OR REFLECTCPP_TOML OR REFLECTCPP_YAML)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file")
endif ()
@@ -44,6 +45,12 @@ if (REFLECTCPP_FLEXBUFFERS)
target_link_libraries(reflectcpp INTERFACE flatbuffers::flatbuffers)
endif ()
if (REFLECTCPP_TOML)
find_package(PkgConfig REQUIRED)
pkg_check_modules(tomlplusplus REQUIRED IMPORTED_TARGET tomlplusplus)
target_link_libraries(reflectcpp INTERFACE PkgConfig::tomlplusplus)
endif()
if (REFLECTCPP_XML)
find_package(pugixml CONFIG REQUIRED)
target_link_libraries(reflectcpp INTERFACE pugixml::pugixml)
+6 -1
View File
@@ -34,6 +34,7 @@ The following table lists the serialization formats currently supported by refle
| BSON | [libbson](https://github.com/mongodb/libbson) | >= 1.25.1 | Apache 2.0 | JSON-like binary format |
| CBOR | [tinycbor](https://github.com/intel/tinycbor) | >= 0.6.0 | MIT | JSON-like binary format |
| flexbuffers | [flatbuffers](https://github.com/google/flatbuffers) | >= 23.5.26 | Apache 2.0 | Schema-less version of flatbuffers, binary format |
| TOML | [toml++](https://github.com/marzer/tomlplusplus) | >= 3.4.0 | MIT | Textual format with an emphasis on readability |
| XML | [pugixml](https://github.com/zeux/pugixml) | >= 1.14 | MIT | Textual format used in many legacy projects |
| YAML | [yaml-cpp](https://github.com/jbeder/yaml-cpp) | >= 0.8.0 | MIT | Textual format with an emphasis on readability |
@@ -95,11 +96,13 @@ and any supported format, except where explicitly noted otherwise:
rfl::bson::write(homer);
rfl::cbor::write(homer);
rfl::flexbuf::write(homer);
rfl::toml::write(homer);
rfl::xml::write(homer);
rfl::bson::read<Person>(bson_bytes);
rfl::cbor::read<Person>(cbor_bytes);
rfl::flexbuf::read<Person>(flexbuf_bytes);
rfl::toml::read<Person>(toml_string);
rfl::xml::read<Person>(xml_string);
```
@@ -504,6 +507,7 @@ add_subdirectory(reflect-cpp) # Add this project as a subdirectory
set(REFLECTCPP_BSON ON) # Optional
set(REFLECTCPP_CBOR ON) # Optional
set(REFLECTCPP_FLEXBUFFERS ON) # Optional
set(REFLECTCPP_TOML ON) # Optional
set(REFLECTCPP_XML ON) # Optional
set(REFLECTCPP_YAML ON) # Optional
@@ -549,7 +553,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_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
cmake -S . -B build -DREFLECTCPP_BUILD_TESTS=ON -DREFLECTCPP_BSON=ON -DREFLECTCPP_CBOR=ON -DREFLECTCPP_FLEXBUFFERS=ON -DREFLECTCPP_XML=ON -DREFLECTCPP_TOML=ON -DREFLECTCPP_YAML=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build -j 4 # gcc, clang
cmake --build build --config Release -j 4 # MSVC
```
@@ -561,6 +565,7 @@ To run the tests, do the following:
./build/tests/cbor/reflect-cpp-cbor-tests
./build/tests/flexbuffers/reflect-cpp-flexbuffers-tests
./build/tests/json/reflect-cpp-json-tests
./build/tests/xml/reflect-cpp-toml-tests
./build/tests/xml/reflect-cpp-xml-tests
./build/tests/xml/reflect-cpp-yaml-tests
```
+4 -2
View File
@@ -64,9 +64,11 @@
5.4) [flexbuffers](https://github.com/getml/reflect-cpp/blob/main/docs/flexbuffers.md)
5.5) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
5.5) [TOML](https://github.com/getml/reflect-cpp/blob/main/docs/toml.md)
5.6) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
5.6) [XML](https://github.com/getml/reflect-cpp/blob/main/docs/xml.md)
5.7) [YAML](https://github.com/getml/reflect-cpp/blob/main/docs/yaml.md)
## 6) Advanced topics
+98
View File
@@ -0,0 +1,98 @@
# TOML
For TOML support, you must also include the header `<rfl/toml.hpp>` and include the toml++ library (https://github.com/marzer/tomlplusplus) in your project.
## Reading and writing
Suppose you have a struct like this:
```cpp
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
rfl::Timestamp<"%Y-%m-%d"> birthday;
std::vector<Person> children;
};
```
You can parse TOML strings like this:
```cpp
const rfl::Result<Person> result = rfl::toml::read<Person>(toml_string);
```
A `person` can be serialized like this:
```cpp
const auto person = Person{...};
const std::string toml_string = rfl::toml::write(person);
```
## Loading and saving
You can also load and save to disc using a very similar syntax:
```cpp
const rfl::Result<Person> result = rfl::toml::load<Person>("/path/to/file.toml");
const auto person = Person{...};
rfl::toml::save("/path/to/file.toml", person);
```
## Reading from and writing into streams
You can also read from and write into any `std::istream` and `std::ostream` respectively.
```cpp
const rfl::Result<Person> result = rfl::toml::read<Person>(my_istream);
const auto person = Person{...};
rfl::toml::write(person, my_ostream);
```
Note that `std::cout` is also an ostream, so this works as well:
```cpp
rfl::toml::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 TOML format, these must be a static function on your struct or class called
`from_toml_obj` that take a `rfl::toml::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 TOMLVar = typename rfl::toml::Reader::InputVarType;
static rfl::Result<Person> from_toml_obj(const TOMLVar& _obj);
};
```
And in your source file, you implement `from_toml_obj` as follows:
```cpp
rfl::Result<Person> Person::from_toml_obj(const TOMLVar& _obj) {
const auto from_nt = [](auto&& _nt) {
return rfl::from_named_tuple<Person>(std::move(_nt));
};
return rfl::toml::read<rfl::named_tuple_t<Person>>(_obj)
.transform(from_nt);
}
```
This will force the compiler to only compile the TOML parsing when the
source file is compiled.
+1 -1
View File
@@ -21,7 +21,7 @@ You can parse YAML strings like this:
const rfl::Result<Person> result = rfl::yaml::read<Person>(yaml_string);
```
A person can be turned into a YAML string like this:
A `person` can be serialized like this:
```cpp
const auto person = Person{...};
+12
View File
@@ -0,0 +1,12 @@
#ifndef RFL_TOML_HPP_
#define RFL_TOML_HPP_
#include "toml/Parser.hpp"
#include "toml/Reader.hpp"
#include "toml/Writer.hpp"
#include "toml/load.hpp"
#include "toml/read.hpp"
#include "toml/save.hpp"
#include "toml/write.hpp"
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef RFL_TOML_PARSER_HPP_
#define RFL_TOML_PARSER_HPP_
#include "../parsing/Parser.hpp"
#include "Reader.hpp"
#include "Writer.hpp"
namespace rfl::toml {
template <class T>
using Parser = parsing::Parser<Reader, Writer, T>;
} // namespace rfl::toml
#endif
+124
View File
@@ -0,0 +1,124 @@
#ifndef RFL_TOML_READER_HPP_
#define RFL_TOML_READER_HPP_
#include <array>
#include <exception>
#include <map>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <toml++/toml.hpp>
#include <type_traits>
#include <unordered_map>
#include <vector>
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl::toml {
struct Reader {
using InputArrayType = ::toml::array*;
using InputObjectType = ::toml::table*;
using InputVarType = ::toml::node*;
template <class T>
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
T::from_toml_obj(var);
});
rfl::Result<InputVarType> get_field(
const std::string& _name, const InputObjectType& _obj) const noexcept {
auto var = (*_obj)[_name];
if (!var) {
return rfl::Error("Object contains no field named '" + _name + "'.");
}
return var.node();
}
bool is_empty(const InputVarType& _var) const noexcept {
return !_var && true;
}
template <class T>
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
const auto ptr = _var->as<std::string>();
if (!ptr) {
return Error("Could not cast the node to std::string!");
}
return **ptr;
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
const auto ptr = _var->as<bool>();
if (!ptr) {
return Error("Could not cast the node to bool!");
}
return **ptr;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<double>();
if (!ptr) {
return Error("Could not cast the node to double!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto ptr = _var->as<int64_t>();
if (!ptr) {
return Error("Could not cast the node to int64_t!");
}
return static_cast<std::remove_cvref_t<T>>(**ptr);
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
}
rfl::Result<InputArrayType> to_array(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_array();
if (!ptr) {
return rfl::Error("Could not cast to an array!");
}
return ptr;
}
template <class ObjectReader>
std::optional<Error> read_object(const ObjectReader& _object_reader,
InputObjectType _obj) const noexcept {
for (auto& [k, v] : *_obj) {
_object_reader.read(std::string_view(k), &v);
}
return std::nullopt;
}
rfl::Result<InputObjectType> to_object(
const InputVarType& _var) const noexcept {
const auto ptr = _var->as_table();
if (!ptr) {
return rfl::Error("Could not cast to a table!");
}
return ptr;
}
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
std::vector<InputVarType> vec;
for (auto& node : *_arr) {
vec.push_back(&node);
}
return vec;
}
template <class T>
rfl::Result<T> use_custom_constructor(
const InputVarType _var) const noexcept {
try {
return T::from_toml_obj(_var);
} catch (std::exception& e) {
return rfl::Error(e.what());
}
}
};
} // namespace rfl::toml
#endif
+124
View File
@@ -0,0 +1,124 @@
#ifndef RFL_TOML_WRITER_HPP_
#define RFL_TOML_WRITER_HPP_
#include <exception>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <toml++/toml.hpp>
#include <type_traits>
#include <vector>
#include "../Ref.hpp"
#include "../Result.hpp"
#include "../always_false.hpp"
namespace rfl::toml {
class Writer {
public:
struct TOMLArray {
::toml::array* val_;
};
struct TOMLObject {
::toml::table* val_;
};
struct TOMLVar {};
using OutputArrayType = TOMLArray;
using OutputObjectType = TOMLObject;
using OutputVarType = TOMLVar;
Writer(::toml::table* _root) : root_(_root) {}
~Writer() = default;
template <class T>
OutputArrayType array_as_root(const T _size) const noexcept {
static_assert(rfl::always_false_v<T>,
"TOML only allows tables as the root element.");
return OutputArrayType{nullptr};
}
OutputObjectType object_as_root(const size_t _size) const noexcept {
return OutputObjectType{root_};
}
OutputVarType null_as_root() const noexcept { return OutputVarType{}; }
template <class T>
OutputVarType value_as_root(const T& _var) const noexcept {
static_assert(rfl::always_false_v<T>,
"TOML only allows tables as the root element.");
return OutputVarType{};
}
OutputArrayType add_array_to_array(const size_t _size,
OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::array());
return OutputArrayType{_parent->val_->at(i).as_array()};
}
OutputArrayType add_array_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::array());
return OutputArrayType{_parent->val_->at_path(_name).as_array()};
}
OutputObjectType add_object_to_array(
const size_t _size, OutputArrayType* _parent) const noexcept {
const auto i = _parent->val_->size();
_parent->val_->push_back(::toml::table());
return OutputObjectType{_parent->val_->at(i).as_table()};
}
OutputObjectType add_object_to_object(
const std::string_view& _name, const size_t _size,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::table());
return OutputObjectType{_parent->val_->at_path(_name).as_table()};
}
template <class T>
OutputVarType add_value_to_array(const T& _var,
OutputArrayType* _parent) const noexcept {
_parent->val_->push_back(::toml::value(_var));
return OutputVarType{};
}
template <class T>
OutputVarType add_value_to_object(const std::string_view& _name,
const T& _var,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(_var));
return OutputVarType{};
}
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
_parent->val_->push_back(std::string(""));
return OutputVarType{};
}
OutputVarType add_null_to_object(const std::string_view& _name,
OutputObjectType* _parent) const noexcept {
_parent->val_->emplace(_name, ::toml::value(std::string("")));
return OutputVarType{};
}
void end_array(OutputArrayType* _arr) const noexcept {}
void end_object(OutputObjectType* _obj) const noexcept {}
private:
::toml::table* root_;
};
} // namespace rfl::toml
#endif // JSON_PARSER_HPP_
+18
View File
@@ -0,0 +1,18 @@
#ifndef RFL_TOML_LOAD_HPP_
#define RFL_TOML_LOAD_HPP_
#include "../Result.hpp"
#include "../io/load_string.hpp"
#include "read.hpp"
namespace rfl::toml {
template <class T>
Result<T> load(const std::string& _fname) {
const auto read_string = [](const auto& _str) { return read<T>(_str); };
return rfl::io::load_string(_fname).and_then(read_string);
}
} // namespace rfl::toml
#endif
+40
View File
@@ -0,0 +1,40 @@
#ifndef RFL_TOML_READ_HPP_
#define RFL_TOML_READ_HPP_
#include <istream>
#include <string>
#include <toml++/toml.hpp>
#include "../internal/wrap_in_rfl_array_t.hpp"
#include "Parser.hpp"
#include "Reader.hpp"
namespace rfl::toml {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from a TOML var.
template <class T>
auto read(InputVarType _var) {
const auto r = Reader();
return Parser<T>::read(r, _var);
}
/// Parses an object from TOML using reflection.
template <class T>
Result<internal::wrap_in_rfl_array_t<T>> read(const std::string& _toml_str) {
auto table = ::toml::parse(_toml_str);
return read<T>(&table);
}
/// Parses an object from a stringstream.
template <class T>
auto read(std::istream& _stream) {
const auto toml_str = std::string(std::istreambuf_iterator<char>(_stream),
std::istreambuf_iterator<char>());
return read<T>(toml_str);
}
} // namespace rfl::toml
#endif
+26
View File
@@ -0,0 +1,26 @@
#ifndef RFL_TOML_SAVE_HPP_
#define RFL_TOML_SAVE_HPP_
#include <fstream>
#include <iostream>
#include <string>
#include "../Result.hpp"
#include "../io/save_string.hpp"
#include "write.hpp"
namespace rfl {
namespace toml {
template <class T>
Result<Nothing> save(const std::string& _fname, const T& _obj) {
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
return write(_obj, _stream);
};
return rfl::io::save_string(_fname, _obj, write_func);
}
} // namespace toml
} // namespace rfl
#endif
+40
View File
@@ -0,0 +1,40 @@
#ifndef RFL_TOML_WRITE_HPP_
#define RFL_TOML_WRITE_HPP_
#include <ostream>
#include <sstream>
#include <string>
#include <toml++/toml.hpp>
#include <type_traits>
#include "../parsing/Parent.hpp"
#include "Parser.hpp"
namespace rfl::toml {
/// Writes a TOML into an ostream.
template <class T>
std::ostream& write(const T& _obj, std::ostream& _stream) {
using ParentType = parsing::Parent<Writer>;
::toml::table root;
auto w = Writer(&root);
Parser<T>::write(w, _obj, typename ParentType::Root{});
_stream << root;
return _stream;
}
/// Returns a TOML string.
template <class T>
std::string write(const T& _obj) {
using ParentType = parsing::Parent<Writer>;
std::stringstream sstream;
::toml::table root;
auto w = Writer(&root);
Parser<T>::write(w, _obj, typename ParentType::Root{});
sstream << root;
return sstream.str();
}
} // namespace rfl::toml
#endif
+4
View File
@@ -24,6 +24,10 @@ if (REFLECTCPP_XML)
add_subdirectory(xml)
endif()
if (REFLECTCPP_TOML)
add_subdirectory(toml)
endif()
if (REFLECTCPP_YAML)
add_subdirectory(yaml)
endif()
+8
View File
@@ -0,0 +1,8 @@
project(reflect-cpp-toml-tests)
file(GLOB_RECURSE SOURCES "*.cpp")
add_executable(reflect-cpp-toml-tests ${SOURCES})
target_link_libraries(reflect-cpp-toml-tests PRIVATE reflectcpp)
+40
View File
@@ -0,0 +1,40 @@
#include "test_array.hpp"
#include <array>
#include <iostream>
#include <memory>
#include <rfl/toml.hpp>
#include <source_location>
#include <string>
// Make sure things still compile when
// rfl.hpp is included after rfl/yaml.hpp.
#include <rfl.hpp>
#include "write_and_read.hpp"
namespace test_array {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::array<Person, 3>> children = nullptr;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto bart = Person{.first_name = "Bart"};
auto lisa = Person{.first_name = "Lisa"};
auto maggie = Person{.first_name = "Maggie"};
const auto homer = Person{
.first_name = "Homer",
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
std::move(bart), std::move(lisa), std::move(maggie)})};
write_and_read(homer);
}
} // namespace test_array
+4
View File
@@ -0,0 +1,4 @@
namespace test_array{
void test();
}
+50
View File
@@ -0,0 +1,50 @@
#include "test_box.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_box {
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
double value;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Rename<"criticalValue", double> critical_value;
rfl::Box<DecisionTree> lesser;
rfl::Box<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
}
} // namespace test_box
+4
View File
@@ -0,0 +1,4 @@
namespace test_box{
void test();
}
+42
View File
@@ -0,0 +1,42 @@
#include "test_custom_class1.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_custom_class1 {
struct Person {
struct PersonImpl {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::vector<Person> children;
};
using ReflectionType = PersonImpl;
Person(const PersonImpl& _impl) : impl(_impl) {}
Person(const std::string& _first_name)
: impl(PersonImpl{.first_name = _first_name}) {}
const ReflectionType& reflection() const { return impl; };
private:
PersonImpl impl;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto bart = Person("Bart");
write_and_read(bart);
}
} // namespace test_custom_class1
+4
View File
@@ -0,0 +1,4 @@
namespace test_custom_class1{
void test();
}
+68
View File
@@ -0,0 +1,68 @@
#include "test_custom_class3.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_custom_class3 {
struct Person {
Person(const std::string& _first_name, const std::string& _last_name,
const int _age)
: first_name_(_first_name), last_name_(_last_name), age_(_age) {}
const auto& first_name() const { return first_name_; }
const auto& last_name() const { return last_name_; }
auto age() const { return age_; }
private:
std::string first_name_;
std::string last_name_;
int age_;
};
struct PersonImpl {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name;
int age;
static PersonImpl from_class(const Person& _p) noexcept {
return PersonImpl{.first_name = _p.first_name(),
.last_name = _p.last_name(),
.age = _p.age()};
}
Person to_class() const { return Person(first_name(), last_name(), age); }
};
} // namespace test_custom_class3
namespace rfl {
namespace parsing {
template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
test_custom_class3::PersonImpl> {};
} // namespace parsing
} // namespace rfl
namespace test_custom_class3 {
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto bart = Person("Bart", "Simpson", 10);
write_and_read(bart);
}
} // namespace test_custom_class3
+4
View File
@@ -0,0 +1,4 @@
namespace test_custom_class3{
void test();
}
+69
View File
@@ -0,0 +1,69 @@
#include "test_custom_class4.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_custom_class4 {
struct Person {
Person(const std::string& _first_name,
const rfl::Box<std::string>& _last_name, int _age)
: first_name_(_first_name),
last_name_(rfl::make_box<std::string>(*_last_name)),
age_(_age) {}
const auto& first_name() const { return first_name_; }
const auto& last_name() const { return last_name_; }
auto age() const { return age_; }
private:
std::string first_name_;
rfl::Box<std::string> last_name_;
int age_;
};
struct PersonImpl {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
rfl::Field<"age", int> age;
static PersonImpl from_class(const Person& _p) noexcept {
return PersonImpl{.first_name = _p.first_name(),
.last_name = rfl::make_box<std::string>(*_p.last_name()),
.age = _p.age()};
}
};
} // namespace test_custom_class4
namespace rfl {
namespace parsing {
template <class ReaderType, class WriterType>
struct Parser<ReaderType, WriterType, test_custom_class4::Person>
: public CustomParser<ReaderType, WriterType, test_custom_class4::Person,
test_custom_class4::PersonImpl> {};
} // namespace parsing
} // namespace rfl
namespace test_custom_class4 {
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto bart = test_custom_class4::Person(
"Bart", rfl::make_box<std::string>("Simpson"), 10);
write_and_read(bart);
}
} // namespace test_custom_class4
+4
View File
@@ -0,0 +1,4 @@
namespace test_custom_class4{
void test();
}
+57
View File
@@ -0,0 +1,57 @@
#include "test_custom_constructor.hpp"
#include <iostream>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_custom_constructor {
struct Person {
static rfl::Result<Person> from_toml_obj(
typename rfl::toml::Reader::InputVarType _obj) {
/// This only exists for the purpose of the test.
const auto change_first_name = [](auto&& _person) {
return rfl::replace(std::move(_person),
rfl::Field<"firstName", std::string>("Bart"));
};
const auto from_nt = [](auto&& _nt) {
return rfl::from_named_tuple<Person>(std::move(_nt));
};
return rfl::toml::read<rfl::named_tuple_t<Person>>(_obj)
.transform(from_nt)
.transform(change_first_name);
}
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto res = rfl::toml::read<Person>(
R"(firstName = 'Homer'
lastName = 'Simpson'
birthday = '1987-04-19')");
if (!res) {
std::cout << "Test failed on read. Error: " << res.error().value().what()
<< std::endl;
return;
}
if (res.value().first_name() != "Bart") {
std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'"
<< std::endl
<< std::endl;
} else {
std::cout << "OK" << std::endl << std::endl;
}
}
} // namespace test_custom_constructor
+4
View File
@@ -0,0 +1,4 @@
namespace test_custom_constructor {
void test();
}
+32
View File
@@ -0,0 +1,32 @@
#include "test_default_values.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_default_values {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::vector<Person> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto bart = Person{.first_name = "Bart"};
const auto lisa = Person{.first_name = "Lisa"};
const auto maggie = Person{.first_name = "Maggie"};
const auto homer =
Person{.first_name = "Homer",
.children = std::vector<Person>({bart, lisa, maggie})};
write_and_read(homer);
}
} // namespace test_default_values
+4
View File
@@ -0,0 +1,4 @@
namespace test_default_values{
void test();
}
+33
View File
@@ -0,0 +1,33 @@
#include "test_deque.hpp"
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_deque {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::deque<Person>> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto children = std::make_unique<std::deque<Person>>();
children->emplace_back(Person{.first_name = "Bart"});
children->emplace_back(Person{.first_name = "Lisa"});
children->emplace_back(Person{.first_name = "Maggie"});
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
}
} // namespace test_deque
+4
View File
@@ -0,0 +1,4 @@
namespace test_deque{
void test();
}
+29
View File
@@ -0,0 +1,29 @@
#include "test_enum.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_enum {
enum class Color { red, green, blue, yellow };
struct Circle {
float radius;
Color color;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto circle = Circle{.radius = 2.0, .color = Color::green};
write_and_read(circle);
}
} // namespace test_enum
+4
View File
@@ -0,0 +1,4 @@
namespace test_enum {
void test();
}
+39
View File
@@ -0,0 +1,39 @@
#include "test_field_variant.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_field_variant {
struct Circle {
double radius;
};
struct Rectangle {
double height;
double width;
};
struct Square {
double width;
};
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
rfl::Field<"rectangle", Rectangle>,
rfl::Field<"square", rfl::Box<Square>>>;
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const Shapes r =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
write_and_read(r);
}
} // namespace test_field_variant
+4
View File
@@ -0,0 +1,4 @@
namespace test_field_variant{
void test();
}
+39
View File
@@ -0,0 +1,39 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "test_enum.hpp"
#include "write_and_read.hpp"
namespace test_flag_enum {
enum class Color {
red = 256,
green = 512,
blue = 1024,
yellow = 2048,
orange = (256 | 2048) // red + yellow = orange
};
inline Color operator|(Color c1, Color c2) {
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
}
struct Circle {
float radius;
Color color;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto circle =
Circle{.radius = 2.0, .color = Color::blue | Color::orange};
write_and_read(circle);
}
} // namespace test_flag_enum
+4
View File
@@ -0,0 +1,4 @@
namespace test_flag_enum {
void test();
}
+38
View File
@@ -0,0 +1,38 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "test_enum.hpp"
#include "write_and_read.hpp"
namespace test_flag_enum_with_int {
enum class Color {
red = 256,
green = 512,
blue = 1024,
yellow = 2048,
orange = (256 | 2048) // red + yellow = orange
};
inline Color operator|(Color c1, Color c2) {
return static_cast<Color>(static_cast<int>(c1) | static_cast<int>(c2));
}
struct Circle {
float radius;
Color color;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto circle = Circle{.radius = 2.0, .color = static_cast<Color>(10000)};
write_and_read(circle);
}
} // namespace test_flag_enum_with_int
+4
View File
@@ -0,0 +1,4 @@
namespace test_flag_enum_with_int {
void test();
}
+38
View File
@@ -0,0 +1,38 @@
#include "test_flatten.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_flatten {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
rfl::Field<"age", int> age;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Field<"employer", rfl::Box<std::string>> employer;
rfl::Field<"salary", float> salary;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto employee = Employee{
.person = Person{.first_name = "Homer",
.last_name = rfl::make_box<std::string>("Simpson"),
.age = 45},
.employer = rfl::make_box<std::string>("Mr. Burns"),
.salary = 60000.0};
write_and_read(employee);
}
} // namespace test_flatten
+4
View File
@@ -0,0 +1,4 @@
namespace test_flatten{
void test();
}
+39
View File
@@ -0,0 +1,39 @@
#include "test_flatten_anonymous.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_flatten_anonymous {
struct Person {
std::string first_name;
rfl::Box<std::string> last_name;
int age;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Box<std::string> employer;
float salary;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto employee = Employee{
.person = Person{.first_name = "Homer",
.last_name = rfl::make_box<std::string>("Simpson"),
.age = 45},
.employer = rfl::make_box<std::string>("Mr. Burns"),
.salary = 60000.0};
write_and_read(employee);
}
} // namespace test_flatten_anonymous
+4
View File
@@ -0,0 +1,4 @@
namespace test_flatten_anonymous{
void test();
}
+33
View File
@@ -0,0 +1,33 @@
#include "test_forward_list.hpp"
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_forward_list {
struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::forward_list<Person>> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto children = std::make_unique<std::forward_list<Person>>();
children->emplace_front(Person{.first_name = "Maggie"});
children->emplace_front(Person{.first_name = "Lisa"});
children->emplace_front(Person{.first_name = "Bart"});
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
}
} // namespace test_forward_list
+4
View File
@@ -0,0 +1,4 @@
namespace test_forward_list{
void test();
}
+30
View File
@@ -0,0 +1,30 @@
#include "test_literal.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_literal {
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
using LastName = rfl::Literal<"Simpson">;
struct Person {
rfl::Rename<"firstName", FirstName> first_name;
rfl::Rename<"lastName", LastName> last_name;
std::vector<Person> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
write_and_read(bart);
}
} // namespace test_literal
+4
View File
@@ -0,0 +1,4 @@
namespace test_literal{
void test();
}
+28
View File
@@ -0,0 +1,28 @@
#include "test_literal_map.hpp"
#include <iostream>
#include <map>
#include <memory>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <unordered_map>
#include "write_and_read.hpp"
namespace test_literal_map {
using FieldName = rfl::Literal<"firstName", "lastName">;
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
std::map<FieldName, std::unique_ptr<std::string>> homer;
homer.insert(std::make_pair(FieldName::make<"firstName">(),
std::make_unique<std::string>("Homer")));
homer.insert(std::make_pair(FieldName::make<"lastName">(),
std::make_unique<std::string>("Simpson")));
write_and_read(homer);
}
} // namespace test_literal_map
+4
View File
@@ -0,0 +1,4 @@
namespace test_literal_map{
void test();
}
+32
View File
@@ -0,0 +1,32 @@
#include "test_map.hpp"
#include <iostream>
#include <map>
#include <rfl.hpp>
#include <source_location>
#include <string>
#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<std::string, Person> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto children = std::map<std::string, Person>();
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
+4
View File
@@ -0,0 +1,4 @@
namespace test_map{
void test();
}
@@ -0,0 +1,33 @@
#include "test_map_with_key_validation.hpp"
#include <iostream>
#include <map>
#include <rfl.hpp>
#include <source_location>
#include <string>
#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<std::map<rfl::AlphaNumeric, Person>> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto children = std::make_unique<std::map<rfl::AlphaNumeric, Person>>();
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
@@ -0,0 +1,4 @@
namespace test_map_with_key_validation{
void test();
}
+64
View File
@@ -0,0 +1,64 @@
#include "test_monster_example.hpp"
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<rfl::Field<"weapon", Weapon>>;
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<int> inventory;
Color color = Color::make<"Blue">();
std::vector<Weapon> weapons;
Equipment equipped;
std::vector<Vec3> 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<Weapon>({sword, axe});
const auto position = Vec3{1.0f, 2.0f, 3.0f};
const auto inventory = std::vector<int>({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
+4
View File
@@ -0,0 +1,4 @@
namespace test_monster_example{
void test();
}
+51
View File
@@ -0,0 +1,51 @@
#include "test_readme_example.hpp"
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
namespace test_readme_example {
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, 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<Person> 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<Person>({bart, lisa, maggie})};
write_and_read(homer);
}
} // namespace test_readme_example
+4
View File
@@ -0,0 +1,4 @@
namespace test_readme_example{
void test();
}
+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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
+4
View File
@@ -0,0 +1,4 @@
namespace test_readme_example2 {
void test();
}
+49
View File
@@ -0,0 +1,49 @@
#include "test_ref.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<DecisionTree> lesser;
rfl::Ref<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
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>(DecisionTree{leaf1}),
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
}
} // namespace test_ref
+4
View File
@@ -0,0 +1,4 @@
namespace test_ref{
void test();
}
+70
View File
@@ -0,0 +1,70 @@
#include "test_save_load.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/toml.hpp>
#include <source_location>
#include <string>
#include <vector>
namespace test_save_load {
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, 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<Person> 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<Person>()};
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<Person>({bart, lisa, maggie})};
rfl::toml::save("homer.toml", homer1);
const auto homer2 = rfl::toml::load<Person>("homer.toml").value();
const auto string1 = rfl::toml::write(homer1);
const auto string2 = rfl::toml::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
+4
View File
@@ -0,0 +1,4 @@
namespace test_save_load {
void test();
}
+30
View File
@@ -0,0 +1,30 @@
#include "test_set.hpp"
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<std::set<std::string>> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto children = std::make_unique<std::set<std::string>>(
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
}
} // namespace test_set
+4
View File
@@ -0,0 +1,4 @@
namespace test_set{
void test();
}
+42
View File
@@ -0,0 +1,42 @@
#include "test_size.hpp"
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<std::vector<Person>,
rfl::Size<rfl::AnyOf<rfl::EqualTo<0>, 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<Person>({bart, lisa, maggie})};
write_and_read(homer);
}
} // namespace test_size
+4
View File
@@ -0,0 +1,4 @@
namespace test_size{
void test();
}
+24
View File
@@ -0,0 +1,24 @@
#include "test_string_map.hpp"
#include <iostream>
#include <map>
#include <memory>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include "write_and_read.hpp"
namespace test_string_map {
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
std::map<std::string, std::unique_ptr<std::string>> homer;
homer.insert(
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
homer.insert(
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
write_and_read(homer);
}
} // namespace test_string_map
+4
View File
@@ -0,0 +1,4 @@
namespace test_string_map{
void test();
}
+36
View File
@@ -0,0 +1,36 @@
#include "test_tagged_union.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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
+4
View File
@@ -0,0 +1,4 @@
namespace test_tagged_union{
void test();
}
+36
View File
@@ -0,0 +1,36 @@
#include "test_timestamp.hpp"
#include <ctime>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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
+4
View File
@@ -0,0 +1,4 @@
namespace test_timestamp{
void test();
}
+33
View File
@@ -0,0 +1,33 @@
#include "test_unique_ptr.hpp"
#include <iostream>
#include <memory>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<std::vector<Person>> children;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto children = std::make_unique<std::vector<Person>>();
children->emplace_back(Person{.first_name = "Bart"});
children->emplace_back(Person{.first_name = "Lisa"});
children->emplace_back(Person{.first_name = "Maggie"});
const auto homer =
Person{.first_name = "Homer", .children = std::move(children)};
write_and_read(homer);
}
} // namespace test_unique_ptr
+4
View File
@@ -0,0 +1,4 @@
namespace test_unique_ptr{
void test();
}
+48
View File
@@ -0,0 +1,48 @@
#include "test_unique_ptr2.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<DecisionTree> lesser;
std::unique_ptr<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto node = DecisionTree::Node{
.critical_value = 10.0,
.lesser = std::make_unique<DecisionTree>(DecisionTree{leaf1}),
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
}
} // namespace test_unique_ptr2
+4
View File
@@ -0,0 +1,4 @@
namespace test_unique_ptr2{
void test();
}
+36
View File
@@ -0,0 +1,36 @@
#include "test_variant.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#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<Circle, Rectangle, std::unique_ptr<Square>>;
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
+4
View File
@@ -0,0 +1,4 @@
namespace test_variant{
void test();
}
+26
View File
@@ -0,0 +1,26 @@
#include "test_wstring.hpp"
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <source_location>
#include <string>
#include <vector>
#include "write_and_read.hpp"
struct Test {
std::string theNormalString;
std::wstring theWiderString;
};
namespace test_wstring {
void test() {
std::cout << std::source_location::current().function_name() << std::endl;
const Test test = Test{.theNormalString = "The normal string",
.theWiderString = L"The wider string"};
write_and_read(test);
}
} // namespace test_wstring
+3
View File
@@ -0,0 +1,3 @@
namespace test_wstring {
void test();
}
+69
View File
@@ -0,0 +1,69 @@
#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"
#include "test_wstring.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();
test_wstring::test();
return 0;
}
+32
View File
@@ -0,0 +1,32 @@
#ifndef WRITE_AND_READ_
#define WRITE_AND_READ_
#include <iostream>
#include <rfl/toml.hpp>
#include <string>
template <class T>
void write_and_read(const T& _struct) {
const auto toml_string1 = rfl::toml::write(_struct);
const auto res = rfl::toml::read<T>(toml_string1);
if (!res) {
std::cout << "Test failed on read. Error: " << res.error().value().what()
<< std::endl
<< "Original string: " << toml_string1 << std::endl
<< std::endl;
return;
}
const auto toml_string2 = rfl::toml::write(res.value());
if (toml_string2 != toml_string1) {
std::cout << "Test failed on read. Expected:" << std::endl
<< toml_string1 << std::endl
<< "Got: " << std::endl
<< toml_string2 << std::endl
<< std::endl;
return;
}
// std::cout << toml_string1 << std::endl;
std::cout << "OK" << std::endl << std::endl;
}
#endif
+4
View File
@@ -19,6 +19,10 @@
"name": "tinycbor",
"version>=": "0.6.0"
},
{
"name": "tomlplusplus",
"version>=": "3.4.0"
},
{
"name": "yaml-cpp",
"version>=": "0.8.0#1"