mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-05 18:26:18 +08:00
Added support for various containers
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
#include "rfl/parsing/is_forward_list.hpp"
|
||||
#include "rfl/parsing/is_map_like.hpp"
|
||||
#include "rfl/parsing/is_required.hpp"
|
||||
#include "rfl/parsing/is_set_like.hpp"
|
||||
#include "rfl/to_named_tuple.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -967,14 +968,16 @@ struct VectorParser {
|
||||
vec.emplace_front(get_elem(*it));
|
||||
}
|
||||
} else if constexpr (is_map_like<VecType>()) {
|
||||
for (auto it = input_vars.begin(); it != input_vars.end();
|
||||
++it) {
|
||||
vec.insert(get_pair(*it));
|
||||
for (auto& v : input_vars) {
|
||||
vec.insert(get_pair(v));
|
||||
}
|
||||
} else if constexpr (is_set_like<VecType>()) {
|
||||
for (auto& v : input_vars) {
|
||||
vec.insert(get_elem(v));
|
||||
}
|
||||
} else {
|
||||
for (auto it = input_vars.begin(); it != input_vars.end();
|
||||
++it) {
|
||||
vec.emplace_back(get_elem(*it));
|
||||
for (auto& v : input_vars) {
|
||||
vec.emplace_back(get_elem(v));
|
||||
}
|
||||
}
|
||||
return vec;
|
||||
@@ -1020,6 +1023,14 @@ template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::map<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType, std::map<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::multimap<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType, std::multimap<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::multiset<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType, std::multiset<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::set<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::set<T>> {};
|
||||
@@ -1028,6 +1039,16 @@ template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_map<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType, std::unordered_map<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_multiset<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType,
|
||||
std::unordered_multiset<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class K, class V>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_multimap<K, V>>
|
||||
: public VectorParser<ReaderType, WriterType,
|
||||
std::unordered_multimap<K, V>> {};
|
||||
|
||||
template <class ReaderType, class WriterType, class T>
|
||||
struct Parser<ReaderType, WriterType, std::unordered_set<T>>
|
||||
: public VectorParser<ReaderType, WriterType, std::unordered_set<T>> {};
|
||||
|
||||
@@ -17,9 +17,15 @@ class is_map_like : public std::false_type {};
|
||||
template <class K, class V>
|
||||
class is_map_like<std::map<K, V>> : public std::true_type {};
|
||||
|
||||
template <class K, class V>
|
||||
class is_map_like<std::multimap<K, V>> : public std::true_type {};
|
||||
|
||||
template <class K, class V>
|
||||
class is_map_like<std::unordered_map<K, V>> : public std::true_type {};
|
||||
|
||||
template <class K, class V>
|
||||
class is_map_like<std::unordered_multimap<K, V>> : public std::true_type {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef RFL_PARSING_IS_SET_LIKE_HPP_
|
||||
#define RFL_PARSING_IS_SET_LIKE_HPP_
|
||||
|
||||
#include <set>
|
||||
#include <type_traits>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T>
|
||||
class is_set_like;
|
||||
|
||||
template <class T>
|
||||
class is_set_like : public std::false_type {};
|
||||
|
||||
template <class T>
|
||||
class is_set_like<std::set<T>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_set_like<std::unordered_set<T>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_set_like<std::multiset<T>> : public std::true_type {};
|
||||
|
||||
template <class T>
|
||||
class is_set_like<std::unordered_multiset<T>> : public std::true_type {};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multimap() {
|
||||
std::cout << "test_multimap" << std::endl;
|
||||
|
||||
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"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multiset() {
|
||||
std::cout << "test_multiset" << std::endl;
|
||||
|
||||
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"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_set() {
|
||||
std::cout << "test_set" << std::endl;
|
||||
|
||||
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"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(
|
||||
homer,
|
||||
R"({"firstName":"Homer","lastName":"Simpson","children":["Bart","Lisa","Maggie"]})");
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multimap() {
|
||||
std::cout << "test_unordered_multimap" << std::endl;
|
||||
|
||||
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"}));
|
||||
|
||||
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::json::write(homer);
|
||||
const auto homer2 = rfl::json::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multiset() {
|
||||
std::cout << "test_unordered_multiset" << std::endl;
|
||||
|
||||
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"}));
|
||||
|
||||
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::json::write(homer);
|
||||
const auto homer2 = rfl::json::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_set() {
|
||||
std::cout << "test_unordered_set" << std::endl;
|
||||
|
||||
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"}));
|
||||
|
||||
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::json::write(homer);
|
||||
const auto homer2 = rfl::json::read<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -14,17 +14,23 @@
|
||||
#include "test_list.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_map.hpp"
|
||||
#include "test_multimap.hpp"
|
||||
#include "test_multiset.hpp"
|
||||
#include "test_optional_fields.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_ref.hpp"
|
||||
#include "test_replace.hpp"
|
||||
#include "test_replace_flatten.hpp"
|
||||
#include "test_replace_with_other_struct.hpp"
|
||||
#include "test_set.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_timestamp.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
#include "test_unordered_map.hpp"
|
||||
#include "test_unordered_multimap.hpp"
|
||||
#include "test_unordered_multiset.hpp"
|
||||
#include "test_unordered_set.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
@@ -47,6 +53,12 @@ int main() {
|
||||
test_list();
|
||||
test_map();
|
||||
test_unordered_map();
|
||||
test_set();
|
||||
test_unordered_set();
|
||||
test_multimap();
|
||||
test_unordered_multimap();
|
||||
test_multiset();
|
||||
test_unordered_multiset();
|
||||
|
||||
test_custom_class1();
|
||||
test_custom_class2();
|
||||
|
||||
Reference in New Issue
Block a user