Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add immer table cereal #201

Closed
wants to merge 3 commits into from
Closed
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
71 changes: 71 additions & 0 deletions lager/extra/cereal/immer_table.hpp
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
4 changes: 2 additions & 2 deletions nix/deps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ rec {
immer = stdenv.mkDerivation rec {
name = "immer-${version}";
version = "git-${commit}";
commit = "a1271fa712342f5c6dfad876820da17e10c28214";
commit = "57a8c5ede6f36a8ef9c6e3c82ac6c81f1e5273fd";
src = fetchFromGitHub {
owner = "arximboldi";
repo = "immer";
rev = commit;
sha256 = "1bqkinkbp1b1aprg7ydfrbfs7gi779nypwvh9fj129frq1c2rxw5";
sha256 = "D6QiTeOoBUJScQQl/e1jq4n+ORTzYnKg7LKTP6xacM4";
};
dontUseCmakeBuildDir = true;
nativeBuildInputs = [ cmake ];
Expand Down
29 changes: 29 additions & 0 deletions test/cereal/immer_table.cpp
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);
}
Loading