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
33 changes: 17 additions & 16 deletions include/rfl/parsing/Parser_map_like.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,30 @@ namespace rfl {
namespace parsing {

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::map<std::string, T>>
requires AreReaderAndWriter<R, W, std::map<std::string, T>>
struct Parser<R, W, std::map<std::string, T>, ProcessorsType>
: public MapParser<R, W, std::map<std::string, T>, ProcessorsType> {
};
: public MapParser<R, W, std::map<std::string, T>, ProcessorsType> {};

template <class R, class W, class T, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_map<std::string, T, Hash, KeyEqual, Allocator>>
struct Parser<R, W,
std::unordered_map<std::string, T, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public MapParser<
R, W, std::unordered_map<std::string, T, Hash, KeyEqual, Allocator>,
ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_map<std::string, T>>
struct Parser<R, W, std::unordered_map<std::string, T>, ProcessorsType>
: public MapParser<R, W, std::unordered_map<std::string, T>,
ProcessorsType> {
};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, Object<T>>
requires AreReaderAndWriter<R, W, Object<T>>
struct Parser<R, W, Object<T>, ProcessorsType>
: public MapParser<R, W, Object<T>, ProcessorsType> {
};
: public MapParser<R, W, Object<T>, ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, ExtraFields<T>>
requires AreReaderAndWriter<R, W, ExtraFields<T>>
struct Parser<R, W, ExtraFields<T>, ProcessorsType>
: public MapParser<R, W, ExtraFields<T>, ProcessorsType> {
};
: public MapParser<R, W, ExtraFields<T>, ProcessorsType> {};

} // namespace parsing
} // namespace rfl
Expand Down
97 changes: 52 additions & 45 deletions include/rfl/parsing/Parser_vector_like.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,76 +22,83 @@ namespace rfl {
namespace parsing {

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::deque<T>>
requires AreReaderAndWriter<R, W, std::deque<T>>
struct Parser<R, W, std::deque<T>, ProcessorsType>
: public VectorParser<R, W, std::deque<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::deque<T>, ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::forward_list<T>>
requires AreReaderAndWriter<R, W, std::forward_list<T>>
struct Parser<R, W, std::forward_list<T>, ProcessorsType>
: public VectorParser<R, W, std::forward_list<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::forward_list<T>, ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::list<T>>
requires AreReaderAndWriter<R, W, std::list<T>>
struct Parser<R, W, std::list<T>, ProcessorsType>
: public VectorParser<R, W, std::list<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::list<T>, ProcessorsType> {};

template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::map<K, V>>
requires AreReaderAndWriter<R, W, std::map<K, V>>
struct Parser<R, W, std::map<K, V>, ProcessorsType>
: public VectorParser<R, W, std::map<K, V>, ProcessorsType> {
};
: public VectorParser<R, W, std::map<K, V>, ProcessorsType> {};

template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::multimap<K, V>>
requires AreReaderAndWriter<R, W, std::multimap<K, V>>
struct Parser<R, W, std::multimap<K, V>, ProcessorsType>
: public VectorParser<R, W, std::multimap<K, V>, ProcessorsType> {
};
: public VectorParser<R, W, std::multimap<K, V>, ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::multiset<T>>
requires AreReaderAndWriter<R, W, std::multiset<T>>
struct Parser<R, W, std::multiset<T>, ProcessorsType>
: public VectorParser<R, W, std::multiset<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::multiset<T>, ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::set<T>>
requires AreReaderAndWriter<R, W, std::set<T>>
struct Parser<R, W, std::set<T>, ProcessorsType>
: public VectorParser<R, W, std::set<T>, ProcessorsType> {
};

template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_map<K, V>>
struct Parser<R, W, std::unordered_map<K, V>, ProcessorsType>
: public VectorParser<R, W, std::unordered_map<K, V>, ProcessorsType> {
};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_multiset<T>>
struct Parser<R, W, std::unordered_multiset<T>, ProcessorsType>
: public VectorParser<R, W, std::unordered_multiset<T>, ProcessorsType> {
};

template <class R, class W, class K, class V, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_multimap<K, V>>
struct Parser<R, W, std::unordered_multimap<K, V>, ProcessorsType>
: public VectorParser<R, W, std::set<T>, ProcessorsType> {};

template <class R, class W, class K, class V, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_map<K, V, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_map<K, V, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W,
std::unordered_map<K, V, Hash, KeyEqual, Allocator>,
ProcessorsType> {};

template <class R, class W, class T, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_multiset<T, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_multiset<T, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W,
std::unordered_multiset<T, Hash, KeyEqual, Allocator>,
ProcessorsType> {};

template <class R, class W, class K, class V, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<
R, W, std::unordered_multimap<K, V, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_multimap<K, V, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W, std::unordered_multimap<K, V>, ProcessorsType> {
};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::unordered_set<T>>
struct Parser<R, W, std::unordered_set<T>, ProcessorsType>
: public VectorParser<R, W, std::unordered_set<T>, ProcessorsType> {
};
template <class R, class W, class T, class Hash, class KeyEqual,
class Allocator, class ProcessorsType>
requires AreReaderAndWriter<R, W,
std::unordered_set<T, Hash, KeyEqual, Allocator>>
struct Parser<R, W, std::unordered_set<T, Hash, KeyEqual, Allocator>,
ProcessorsType>
: public VectorParser<R, W,
std::unordered_set<T, Hash, KeyEqual, Allocator>,
ProcessorsType> {};

template <class R, class W, class T, class ProcessorsType>
requires AreReaderAndWriter<R, W, std::vector<T>>
requires AreReaderAndWriter<R, W, std::vector<T>>
struct Parser<R, W, std::vector<T>, ProcessorsType>
: public VectorParser<R, W, std::vector<T>, ProcessorsType> {
};
: public VectorParser<R, W, std::vector<T>, ProcessorsType> {};

} // namespace parsing
} // namespace rfl
Expand Down
10 changes: 6 additions & 4 deletions include/rfl/parsing/is_map_like.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ 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 Hash, class KeyEqual, class Allocator>
class is_map_like<std::unordered_map<K, V, Hash, KeyEqual, Allocator>>
: public std::true_type {};

template <class K, class V>
class is_map_like<std::unordered_multimap<K, V>> : public std::true_type {};
template <class K, class V, class Hash, class KeyEqual, class Allocator>
class is_map_like<std::unordered_multimap<K, V, Hash, KeyEqual, Allocator>>
: public std::true_type {};

template <class T>
constexpr bool is_map_like_v =
Expand Down
5 changes: 3 additions & 2 deletions include/rfl/parsing/is_map_like_not_multimap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class is_map_like_not_multimap : public std::false_type {};
template <class K, class V>
class is_map_like_not_multimap<std::map<K, V>> : public std::true_type {};

template <class K, class V>
class is_map_like_not_multimap<std::unordered_map<K, V>>
template <class K, class V, class Hash, class KeyEqual, class Allocator>
class is_map_like_not_multimap<
std::unordered_map<K, V, Hash, KeyEqual, Allocator>>
: public std::true_type {};

} // namespace parsing
Expand Down
39 changes: 39 additions & 0 deletions tests/json/test_unordered_map_with_custom_hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <unordered_map>

#include "write_and_read.hpp"

namespace test_unordered_map_with_custom_hash {

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

struct Person {
rfl::Rename<"firstName", std::string> first_name;
rfl::Rename<"lastName", std::string> last_name = "Simpson";
std::unique_ptr<std::unordered_map<int, Person, DummyHash<int>>> children;
};

TEST(json, test_unordered_map_with_custom_hash) {
auto children =
std::make_unique<std::unordered_map<int, Person, DummyHash<int>>>();
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);

EXPECT_TRUE(true);
}
} // namespace test_unordered_map_with_custom_hash
Loading