mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 00:26:19 +08:00
Updated the flexbuffers tests
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_all_of() {
|
||||
std::cout << "test_all_of" << std::endl;
|
||||
namespace test_all_of {
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
@@ -23,3 +26,4 @@ void test_all_of() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_all_of
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_anonymous_fields() {
|
||||
std::cout << "test_anonymous_fields" << std::endl;
|
||||
namespace test_anonymous_fields {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
std::string last_name;
|
||||
rfl::Timestamp<"%Y-%m-%d"> birthday;
|
||||
Age age;
|
||||
rfl::Email email;
|
||||
std::vector<Person> children;
|
||||
};
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
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",
|
||||
@@ -49,3 +52,4 @@ void test_anonymous_fields() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_anonymous_fields
|
||||
|
||||
@@ -2,32 +2,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_box() {
|
||||
std::cout << "test_box" << std::endl;
|
||||
namespace test_box {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
|
||||
rfl::Field<"right", 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};
|
||||
@@ -41,3 +42,4 @@ void test_box() {
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_box
|
||||
|
||||
@@ -2,36 +2,39 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_custom_class1() {
|
||||
std::cout << "test_custom_class1" << std::endl;
|
||||
namespace test_custom_class1 {
|
||||
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
struct PersonImpl {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
|
||||
using ReflectionType = PersonImpl;
|
||||
using ReflectionType = PersonImpl;
|
||||
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
Person(const PersonImpl& _impl) : impl(_impl) {}
|
||||
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
Person(const std::string& _first_name)
|
||||
: impl(PersonImpl{.first_name = _first_name}) {}
|
||||
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
const ReflectionType& reflection() const { return impl; };
|
||||
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
private:
|
||||
PersonImpl impl;
|
||||
};
|
||||
|
||||
const auto bart = Person("Bart");
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
write_and_read(bart);
|
||||
const auto bart = Person("Bart");
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
} // namespace test_custom_class1
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc2 {
|
||||
namespace test_custom_class2 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name, const std::string& _last_name,
|
||||
@@ -40,23 +41,27 @@ struct PersonImpl {
|
||||
Person to_class() const { return Person(first_name(), last_name(), age()); }
|
||||
};
|
||||
|
||||
} // namespace tcc2
|
||||
} // namespace test_custom_class2
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc2::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc2::Person,
|
||||
tcc2::PersonImpl> {};
|
||||
struct Parser<ReaderType, WriterType, test_custom_class2::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class2::Person,
|
||||
test_custom_class2::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class2() {
|
||||
std::cout << "test_custom_class2" << std::endl;
|
||||
namespace test_custom_class2 {
|
||||
|
||||
const auto bart = tcc2::Person("Bart", "Simpson", 10);
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = test_custom_class2::Person("Bart", "Simpson", 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class2
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace tcc3 {
|
||||
namespace test_custom_class3 {
|
||||
|
||||
struct Person {
|
||||
Person(const std::string& _first_name,
|
||||
@@ -40,24 +41,28 @@ struct PersonImpl {
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace tcc3
|
||||
} // namespace test_custom_class3
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct Parser<ReaderType, WriterType, tcc3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, tcc3::Person,
|
||||
tcc3::PersonImpl> {};
|
||||
struct Parser<ReaderType, WriterType, test_custom_class3::Person>
|
||||
: public CustomParser<ReaderType, WriterType, test_custom_class3::Person,
|
||||
test_custom_class3::PersonImpl> {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
void test_custom_class3() {
|
||||
std::cout << "test_custom_class3" << std::endl;
|
||||
namespace test_custom_class3 {
|
||||
|
||||
const auto bart =
|
||||
tcc3::Person("Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = test_custom_class3::Person(
|
||||
"Bart", rfl::make_box<std::string>("Simpson"), 10);
|
||||
|
||||
write_and_read(bart);
|
||||
}
|
||||
|
||||
} // namespace test_custom_class3
|
||||
|
||||
@@ -1,36 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that the example in the README works as expected.
|
||||
// It would be embarrassing if it didn't...
|
||||
void test_custom_constructor() {
|
||||
std::cout << "test_custom_constructor" << std::endl;
|
||||
namespace test_custom_constructor {
|
||||
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_flexbuf(
|
||||
typename rfl::flexbuf::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::flexbuf::read<rfl::named_tuple_t<Person>>(_obj)
|
||||
.transform(from_nt)
|
||||
.transform(change_first_name);
|
||||
}
|
||||
struct Person {
|
||||
static rfl::Result<Person> from_flexbuf(
|
||||
typename rfl::flexbuf::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::flexbuf::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;
|
||||
};
|
||||
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 person1 = Person{
|
||||
.first_name = "Homer", .last_name = "Simpson", .birthday = "1987-04-19"};
|
||||
@@ -53,3 +54,4 @@ void test_custom_constructor() {
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace test_custom_constructor
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_deque() {
|
||||
std::cout << "test_deque" << std::endl;
|
||||
namespace test_deque {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
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"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
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"});
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_deque
|
||||
|
||||
@@ -2,33 +2,37 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_field_variant() {
|
||||
std::cout << "test_field_variant" << std::endl;
|
||||
namespace test_field_variant {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
using Shapes = rfl::Variant<rfl::Field<"circle", Circle>,
|
||||
rfl::Field<"rectangle", Rectangle>,
|
||||
rfl::Field<"square", rfl::Box<Square>>>;
|
||||
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
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_flatten_anonymous() {
|
||||
std::cout << "test_flatten_anonymous" << std::endl;
|
||||
namespace test_flatten_anonymous {
|
||||
|
||||
struct Person {
|
||||
std::string first_name;
|
||||
rfl::Box<std::string> last_name;
|
||||
int age;
|
||||
};
|
||||
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;
|
||||
};
|
||||
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",
|
||||
@@ -31,3 +34,4 @@ void test_flatten_anonymous() {
|
||||
|
||||
write_and_read(employee);
|
||||
}
|
||||
} // namespace test_flatten_anonymous
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_forward_list() {
|
||||
std::cout << "test_forward_list" << std::endl;
|
||||
namespace test_forward_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
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"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
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"});
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_forward_list
|
||||
|
||||
@@ -1,28 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_list() {
|
||||
std::cout << "test_list" << std::endl;
|
||||
namespace test_list {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::list<Person>>();
|
||||
children->emplace_back(Person{.first_name = "Bart"});
|
||||
children->emplace_back(Person{.first_name = "Lisa"});
|
||||
children->emplace_back(Person{.first_name = "Maggie"});
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_list
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_literal() {
|
||||
std::cout << "test_literal" << std::endl;
|
||||
namespace test_literal {
|
||||
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">;
|
||||
using LastName = rfl::Literal<"Simpson">;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", FirstName> first_name;
|
||||
rfl::Field<"lastName", LastName> last_name = rfl::default_value;
|
||||
rfl::Field<"children", std::vector<Person>> children = rfl::default_value;
|
||||
};
|
||||
|
||||
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
|
||||
|
||||
@@ -2,27 +2,31 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_map() {
|
||||
std::cout << "test_map" << std::endl;
|
||||
namespace test_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<int, Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_map
|
||||
|
||||
@@ -1,41 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_monster_example1() {
|
||||
std::cout << "test_monster_example1" << std::endl;
|
||||
namespace test_monster_example1 {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
struct Weapon {
|
||||
std::string name;
|
||||
short damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
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<std::uint8_t> inventory;
|
||||
Color color = Color::make<"Blue">();
|
||||
std::vector<Weapon> weapons;
|
||||
Equipment equipped;
|
||||
std::vector<Vec3> path;
|
||||
};
|
||||
struct Monster {
|
||||
Vec3 pos;
|
||||
short mana = 150;
|
||||
short hp = 100;
|
||||
std::string name;
|
||||
bool friendly = false;
|
||||
std::vector<std::uint8_t> 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};
|
||||
@@ -59,3 +62,4 @@ void test_monster_example1() {
|
||||
write_and_read(orc);
|
||||
}
|
||||
|
||||
} // namespace test_monster_example1
|
||||
|
||||
@@ -1,41 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_monster_example2() {
|
||||
std::cout << "test_monster_example2" << std::endl;
|
||||
namespace test_monster_example2 {
|
||||
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
using Color = rfl::Literal<"Red", "Green", "Blue">;
|
||||
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
struct Weapon {
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"damage", short> damage;
|
||||
};
|
||||
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
using Equipment = rfl::Variant<rfl::Field<"weapon", Weapon>>;
|
||||
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
struct Vec3 {
|
||||
rfl::Field<"x", float> x;
|
||||
rfl::Field<"y", float> y;
|
||||
rfl::Field<"z", float> z;
|
||||
};
|
||||
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", std::vector<Vec3>> path;
|
||||
};
|
||||
struct Monster {
|
||||
rfl::Field<"pos", Vec3> pos;
|
||||
rfl::Field<"mana", short> mana = 150;
|
||||
rfl::Field<"hp", short> hp = 100;
|
||||
rfl::Field<"name", std::string> name;
|
||||
rfl::Field<"friendly", bool> friendly = false;
|
||||
rfl::Field<"inventory", std::vector<std::uint8_t>> inventory;
|
||||
rfl::Field<"color", Color> color = Color::make<"Blue">();
|
||||
rfl::Field<"weapons", std::vector<Weapon>> weapons;
|
||||
rfl::Field<"equipped", Equipment> equipped;
|
||||
rfl::Field<"path", 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};
|
||||
@@ -59,4 +62,4 @@ void test_monster_example2() {
|
||||
|
||||
write_and_read(orc);
|
||||
}
|
||||
|
||||
} // namespace test_monster_example2
|
||||
|
||||
@@ -2,27 +2,31 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multimap() {
|
||||
std::cout << "test_multimap" << std::endl;
|
||||
namespace test_multimap {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multimap<int, Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_multimap
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multiset() {
|
||||
std::cout << "test_multiset" << std::endl;
|
||||
namespace test_multiset {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::multiset<std::string>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_multiset
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_optional_fields() {
|
||||
std::cout << "test_optional_fields" << std::endl;
|
||||
namespace test_optional_fields {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::optional<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::optional<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto bart = Person{.first_name = "Bart"};
|
||||
|
||||
@@ -28,3 +31,4 @@ void test_optional_fields() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_optional_fields
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_readme_example() {
|
||||
std::cout << "test_readme_example" << std::endl;
|
||||
namespace test_readme_example {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", 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",
|
||||
@@ -52,3 +55,4 @@ void test_readme_example() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_readme_example
|
||||
|
||||
@@ -2,32 +2,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_ref() {
|
||||
std::cout << "test_ref" << std::endl;
|
||||
namespace test_ref {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
|
||||
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
|
||||
rfl::Field<"right", 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};
|
||||
@@ -41,3 +42,4 @@ void test_ref() {
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_ref
|
||||
|
||||
@@ -2,25 +2,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_save_load() {
|
||||
std::cout << "test_save_load" << std::endl;
|
||||
namespace test_save_load {
|
||||
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
using Age = rfl::Validator<unsigned int,
|
||||
rfl::AllOf<rfl::Minimum<0>, rfl::Maximum<130>>>;
|
||||
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", std::vector<Person>> children;
|
||||
};
|
||||
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<"age", Age> age;
|
||||
rfl::Field<"email", rfl::Email> email;
|
||||
rfl::Field<"children", 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",
|
||||
@@ -66,3 +69,4 @@ void test_save_load() {
|
||||
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_save_load
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_set() {
|
||||
std::cout << "test_set" << std::endl;
|
||||
namespace test_set {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::set<std::string>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
write_and_read(homer);
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_set
|
||||
|
||||
@@ -3,18 +3,21 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_map() {
|
||||
std::cout << "test_string_map" << std::endl;
|
||||
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")));
|
||||
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);
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_string_map
|
||||
|
||||
@@ -2,24 +2,27 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_unordered_map() {
|
||||
std::cout << "test_string_unordered_map" << std::endl;
|
||||
namespace test_string_unordered_map {
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
|
||||
Map 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")));
|
||||
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
|
||||
Map 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")));
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_string_unordered_map
|
||||
|
||||
@@ -2,30 +2,29 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_tagged_union() {
|
||||
std::cout << "test_tagged_union" << std::endl;
|
||||
namespace test_tagged_union {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value;
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
double radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"shape", rfl::Literal<"Rectangle">> shape = rfl::default_value;
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
double height;
|
||||
double width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
double width;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>;
|
||||
|
||||
@@ -33,3 +32,4 @@ void test_tagged_union() {
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_tagged_union
|
||||
|
||||
@@ -2,20 +2,23 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unique_ptr() {
|
||||
std::cout << "test_unique_ptr" << std::endl;
|
||||
namespace test_unique_ptr {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::vector<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
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"});
|
||||
@@ -27,3 +30,4 @@ void test_unique_ptr() {
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
} // namespace test_unique_ptr
|
||||
|
||||
@@ -2,32 +2,33 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unique_ptr2() {
|
||||
std::cout << "test_unique_ptr2" << std::endl;
|
||||
namespace test_unique_ptr2 {
|
||||
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
|
||||
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
|
||||
};
|
||||
|
||||
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
|
||||
|
||||
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
|
||||
struct DecisionTree {
|
||||
struct Leaf {
|
||||
rfl::Field<"value", double> value;
|
||||
};
|
||||
|
||||
struct Node {
|
||||
rfl::Field<"criticalValue", double> critical_value;
|
||||
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
|
||||
rfl::Field<"right", 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};
|
||||
@@ -41,3 +42,4 @@ void test_unique_ptr2() {
|
||||
|
||||
write_and_read(tree);
|
||||
}
|
||||
} // namespace test_unique_ptr2
|
||||
|
||||
@@ -1,32 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_map() {
|
||||
std::cout << "test_unordered_map" << std::endl;
|
||||
namespace test_unordered_map {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_map<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_map
|
||||
|
||||
@@ -2,32 +2,35 @@
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multimap() {
|
||||
std::cout << "test_unordered_multimap" << std::endl;
|
||||
namespace test_unordered_multimap {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children",
|
||||
std::unique_ptr<std::unordered_multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_multimap<int, Person>>();
|
||||
children->insert(std::make_pair(1, Person{.first_name = "Bart"}));
|
||||
children->insert(std::make_pair(2, Person{.first_name = "Lisa"}));
|
||||
children->insert(std::make_pair(3, Person{.first_name = "Maggie"}));
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered maps are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_multimap
|
||||
|
||||
@@ -1,31 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multiset() {
|
||||
std::cout << "test_unordered_multiset" << std::endl;
|
||||
namespace test_unordered_multiset {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children",
|
||||
std::unique_ptr<std::unordered_multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_multiset<std::string>>(
|
||||
std::unordered_multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_multiset<std::string>>(
|
||||
std::unordered_multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
// Unordered multisets are unpredictable. We therefore only make sure that
|
||||
// this compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered multisets are unpredictable. We therefore only make sure that
|
||||
// this compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_multiset
|
||||
|
||||
@@ -1,30 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_set() {
|
||||
std::cout << "test_unordered_set" << std::endl;
|
||||
namespace test_unordered_set {
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_set<std::string>>(
|
||||
std::unordered_set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
auto children = std::make_unique<std::unordered_set<std::string>>(
|
||||
std::unordered_set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
// Unordered sets are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
// Unordered sets are unpredictable. We therefore only make sure that this
|
||||
// compiles.
|
||||
const auto json_string = rfl::flexbuf::write(homer);
|
||||
const auto homer2 = rfl::flexbuf::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
} // namespace test_unordered_set
|
||||
|
||||
@@ -2,27 +2,29 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
// Makes sure that we can set default values.
|
||||
void test_variant() {
|
||||
std::cout << "test_variant" << std::endl;
|
||||
namespace test_variant {
|
||||
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
struct Circle {
|
||||
rfl::Field<"radius", double> radius;
|
||||
};
|
||||
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Rectangle {
|
||||
rfl::Field<"height", double> height;
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
struct Square {
|
||||
rfl::Field<"width", double> width;
|
||||
};
|
||||
|
||||
void test() {
|
||||
std::cout << std::source_location::current().function_name() << std::endl;
|
||||
|
||||
using Shapes = std::variant<Circle, Rectangle, std::unique_ptr<Square>>;
|
||||
|
||||
@@ -30,3 +32,4 @@ void test_variant() {
|
||||
|
||||
write_and_read(r);
|
||||
}
|
||||
} // namespace test_variant
|
||||
|
||||
+33
-33
@@ -33,43 +33,43 @@
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
test_readme_example();
|
||||
test_monster_example1();
|
||||
test_monster_example2();
|
||||
test_anonymous_fields();
|
||||
test_flatten_anonymous();
|
||||
test_optional_fields();
|
||||
test_unique_ptr();
|
||||
test_unique_ptr2();
|
||||
test_literal();
|
||||
test_variant();
|
||||
test_field_variant();
|
||||
test_tagged_union();
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
test_custom_class3();
|
||||
test_box();
|
||||
test_ref();
|
||||
test_readme_example::test();
|
||||
test_monster_example1::test();
|
||||
test_monster_example2::test();
|
||||
test_anonymous_fields::test();
|
||||
test_flatten_anonymous::test();
|
||||
test_optional_fields::test();
|
||||
test_unique_ptr::test();
|
||||
test_unique_ptr2::test();
|
||||
test_literal::test();
|
||||
test_variant::test();
|
||||
test_field_variant::test();
|
||||
test_tagged_union::test();
|
||||
test_custom_class1::test();
|
||||
test_custom_class2::test();
|
||||
test_custom_class3::test();
|
||||
test_box::test();
|
||||
test_ref::test();
|
||||
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
test_list();
|
||||
test_map();
|
||||
test_multimap();
|
||||
test_multiset();
|
||||
test_set();
|
||||
test_string_map();
|
||||
test_string_unordered_map();
|
||||
test_unordered_map();
|
||||
test_unordered_multimap();
|
||||
test_unordered_multiset();
|
||||
test_unordered_set();
|
||||
test_deque::test();
|
||||
test_forward_list::test();
|
||||
test_list::test();
|
||||
test_map::test();
|
||||
test_multimap::test();
|
||||
test_multiset::test();
|
||||
test_set::test();
|
||||
test_string_map::test();
|
||||
test_string_unordered_map::test();
|
||||
test_unordered_map::test();
|
||||
test_unordered_multimap::test();
|
||||
test_unordered_multiset::test();
|
||||
test_unordered_set::test();
|
||||
|
||||
test_custom_constructor();
|
||||
test_custom_constructor::test();
|
||||
|
||||
test_all_of();
|
||||
test_all_of::test();
|
||||
|
||||
test_save_load();
|
||||
test_save_load::test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user