Add support for std::pair in SLK

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1717
This commit is contained in:
Matej Ferencevic 2018-10-31 14:41:45 +01:00
parent 878b5c9f2b
commit 0493470f98
2 changed files with 30 additions and 1 deletions

View File

@ -12,6 +12,7 @@
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "communication/rpc/streams.hpp"
@ -262,6 +263,21 @@ inline void Load(std::experimental::optional<T> *obj, Reader *reader) {
}
}
template <typename A, typename B>
inline void Save(const std::pair<A, B> &obj, Builder *builder) {
Save(obj.first, builder);
Save(obj.second, builder);
}
template <typename A, typename B>
inline void Load(std::pair<A, B> *obj, Reader *reader) {
A first;
B second;
Load(&first, reader);
Load(&second, reader);
*obj = std::pair<A, B>(std::move(first), std::move(second));
}
// Implementation of three argument serialization for complex types.
template <typename T>

View File

@ -227,6 +227,18 @@ TEST(CustomSerialization, OptionalStringFull) {
ASSERT_EQ(*original, *decoded);
}
TEST(CustomSerialization, Pair) {
std::pair<std::string, int> original{"nandare!", 5};
slk::Builder builder;
slk::Save(original, &builder);
ASSERT_EQ(builder.size(),
sizeof(uint64_t) + original.first.size() + sizeof(int));
std::pair<std::string, int> decoded;
slk::Reader reader(builder.data(), builder.size());
slk::Load(&decoded, &reader);
ASSERT_EQ(original, decoded);
}
TEST(CustomSerialization, SharedPtrEmpty) {
std::shared_ptr<std::string> original;
std::vector<std::string *> saved;
@ -434,7 +446,8 @@ TEST(CustomSerialization, VectorSharedPtr) {
}
TEST(CustomSerialization, OptionalSharedPtr) {
std::experimental::optional<std::shared_ptr<std::string>> original = std::make_shared<std::string>("nandare!");
std::experimental::optional<std::shared_ptr<std::string>> original =
std::make_shared<std::string>("nandare!");
std::vector<std::string *> saved;
slk::Builder builder;