-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add immer table cerealize as vector style
- Loading branch information
1 parent
f8a95de
commit 3203b6d
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// lager - library for functional interactive c++ programs | ||
// Copyright (C) 2017 Juan Pedro Bolivar Puente | ||
// | ||
// This file is part of lager. | ||
// | ||
// lager is free software: you can redistribute it and/or modify | ||
// it under the terms of the MIT License, as detailed in the LICENSE | ||
// file located at the root of this source code distribution, | ||
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE> | ||
// | ||
|
||
#pragma once | ||
|
||
#include <lager/config.hpp> | ||
|
||
#include <cereal/cereal.hpp> | ||
|
||
#include <immer/table.hpp> | ||
#include <immer/table_transient.hpp> | ||
#include <type_traits> | ||
|
||
namespace cereal { | ||
// This code has mostly been adapted from <cereal/types/vector.hpp> | ||
// We don't deal for now with data that could be potentially serialized | ||
// directly in binary format. | ||
|
||
template <typename Archive, | ||
typename T, | ||
typename K, | ||
typename H, | ||
typename E, | ||
typename MP, | ||
std::uint32_t B> | ||
void CEREAL_SAVE_FUNCTION_NAME(Archive& ar, | ||
const immer::table<T, K, H, E, MP, B>& table) | ||
{ | ||
ar(make_size_tag(static_cast<size_type>(table.size()))); | ||
for (auto&& v : table) | ||
ar(v); | ||
} | ||
|
||
template <typename Archive, | ||
typename T, | ||
typename K, | ||
typename H, | ||
typename E, | ||
typename MP, | ||
std::uint32_t B> | ||
void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, | ||
immer::table<T, K, H, E, MP, B>& table) | ||
{ | ||
size_type size{}; | ||
ar(make_size_tag(size)); | ||
|
||
if (!size) | ||
return; | ||
|
||
auto t = immer::table<T, K, H, E, MP, B>{}.transient(); | ||
|
||
for (auto i = size_type{}; i < size; ++i) { | ||
T x; | ||
ar(x); | ||
t.insert(std::move(x)); | ||
} | ||
table = std::move(t).persistent(); | ||
|
||
assert(size == table.size()); | ||
} | ||
|
||
} // namespace cereal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// lager - library for functional interactive c++ programs | ||
// Copyright (C) 2017 Juan Pedro Bolivar Puente | ||
// | ||
// This file is part of lager. | ||
// | ||
// lager is free software: you can redistribute it and/or modify | ||
// it under the terms of the MIT License, as detailed in the LICENSE | ||
// file located at the root of this source code distribution, | ||
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE> | ||
// | ||
|
||
#include "cerealize.hpp" | ||
#include <catch2/catch.hpp> | ||
#include <lager/extra/cereal/immer_table.hpp> | ||
|
||
struct entry_t | ||
{ | ||
size_t id; | ||
size_t value; | ||
}; | ||
|
||
TEST_CASE("basic") | ||
{ | ||
auto x = immer::table<entry_t>{{.id = 1, .value = 2}, // | ||
{.id = 3, .value = 4}}; | ||
auto y = cerealize(x); | ||
CHECK(x == y); | ||
} |