Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions include/rfl/parsing/is_set_like.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ 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 Compare, class Allocator>
class is_set_like<std::set<T, Compare, Allocator>> : public std::true_type {};

template <class T>
class is_set_like<std::unordered_set<T>> : public std::true_type {};
template <class T, class Compare, class Allocator>
class is_set_like<std::multiset<T, Compare, Allocator>>
: public std::true_type {};

template <class T>
class is_set_like<std::multiset<T>> : public std::true_type {};
template <class T, class Hash, class KeyEqual, class Allocator>
class is_set_like<std::unordered_set<T, Hash, KeyEqual, Allocator>>
: public std::true_type {};

template <class T>
class is_set_like<std::unordered_multiset<T>> : public std::true_type {};
template <class T, class Hash, class KeyEqual, class Allocator>
class is_set_like<std::unordered_multiset<T, Hash, KeyEqual, Allocator>>
: public std::true_type {};

template <class T>
constexpr bool is_set_like_v =
Expand Down
32 changes: 32 additions & 0 deletions tests/json/test_multiset_with_custom_comparator_and_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <set>
#include <string>

#include "write_and_read.hpp"

namespace test_multiset_with_custom_comparator_and_allocator {

struct CustomLess {
bool operator()(const int& a, const int& b) const { return a < b; }
};

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::multiset<int, CustomLess, std::allocator<int>>> ages;
};

TEST(json, test_multiset_with_custom_comparator_and_allocator) {
auto ages =
std::make_unique<std::multiset<int, CustomLess, std::allocator<int>>>(
std::multiset<int, CustomLess, std::allocator<int>>({20, 30, 30, 40}));

const auto homer =
Person{.first_name = "Homer", .ages = std::move(ages)};

write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","ages":[20,30,30,40]})");
}
} // namespace test_multiset_with_custom_comparator_and_allocator
31 changes: 31 additions & 0 deletions tests/json/test_set_with_custom_comparator_and_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <set>
#include <string>

#include "write_and_read.hpp"

namespace test_set_with_custom_comparator_and_allocator {

struct CustomLess {
bool operator()(const int& a, const int& b) const { return a < b; }
};

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::set<int, CustomLess, std::allocator<int>>> ages;
};

TEST(json, test_set_with_custom_comparator_and_allocator) {
auto ages = std::make_unique<std::set<int, CustomLess, std::allocator<int>>>(
std::set<int, CustomLess, std::allocator<int>>({20, 30, 40}));

const auto homer =
Person{.first_name = "Homer", .ages = std::move(ages)};

write_and_read(
homer,
R"({"firstName":"Homer","lastName":"Simpson","ages":[20,30,40]})");
}
} // namespace test_set_with_custom_comparator_and_allocator
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <unordered_set>

#include "write_and_read.hpp"

namespace test_unordered_multiset_with_custom_hash_and_allocator {

template <typename T>
struct DummyHash {
std::size_t operator()(const T& v) const { return std::hash<T>()(v); }
};

template <typename T>
struct DummyKeyEqual {
bool operator()(const T& a, const T& b) const { return a == b; }
};

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<
std::unordered_multiset<int, DummyHash<int>, DummyKeyEqual<int>,
std::allocator<int>>>
ages;
};

TEST(json, test_unordered_multiset_with_custom_hash_and_allocator) {
auto ages = std::make_unique<
std::unordered_multiset<int, DummyHash<int>, DummyKeyEqual<int>,
std::allocator<int>>>(
std::unordered_multiset<int, DummyHash<int>, DummyKeyEqual<int>,
std::allocator<int>>({20, 30, 30, 40}));

const auto homer =
Person{.first_name = "Homer", .ages = std::move(ages)};

// 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);
EXPECT_TRUE(homer2 && true);
}
} // namespace test_unordered_multiset_with_custom_hash_and_allocator
45 changes: 45 additions & 0 deletions tests/json/test_unordered_set_with_custom_hash_and_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <unordered_set>

#include "write_and_read.hpp"

namespace test_unordered_set_with_custom_hash_and_allocator {

template <typename T>
struct DummyHash {
std::size_t operator()(const T& v) const { return std::hash<T>()(v); }
};

template <typename T>
struct DummyKeyEqual {
bool operator()(const T& a, const T& b) const { return a == b; }
};

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<
std::unordered_set<int, DummyHash<int>, DummyKeyEqual<int>,
std::allocator<int>>>
ages;
};

TEST(json, test_unordered_set_with_custom_hash_and_allocator) {
auto ages = std::make_unique<
std::unordered_set<int, DummyHash<int>, DummyKeyEqual<int>,
std::allocator<int>>>(
std::unordered_set<int, DummyHash<int>, DummyKeyEqual<int>,
std::allocator<int>>({20, 30, 40}));

const auto homer =
Person{.first_name = "Homer", .ages = std::move(ages)};

// 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);
EXPECT_TRUE(homer2 && true);
}
} // namespace test_unordered_set_with_custom_hash_and_allocator
Loading