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
58 changes: 58 additions & 0 deletions lager/extra/cereal/immer_table.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// 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>

namespace cereal {

template <typename Archive,
typename T,
typename KF,
typename H,
typename E,
typename MP,
std::uint32_t B>
void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, immer::table<T, KF, H, E, MP, B>& m)
{
size_type size;
ar(make_size_tag(size));

for (auto i = size_type{}; i < size; ++i) {
T x;
ar(x);
m = std::move(m).insert(std::move(x));
}
if (size != m.size())
throw std::runtime_error{"duplicate ids?"};
}

template <typename Archive,
typename T,
typename KF,
typename H,
typename E,
typename MP,
std::uint32_t B>
void CEREAL_SAVE_FUNCTION_NAME(Archive& ar,
const immer::table<T, KF, H, E, MP, B>& m)
{
ar(make_size_tag(static_cast<size_type>(m.size())));
for (auto&& v : m)
ar(v);
}

} // namespace cereal
44 changes: 44 additions & 0 deletions test/cereal/immer_table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// 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 <lager/extra/cereal/immer_table.hpp>

#include <catch2/catch.hpp>

struct entry_t
{
size_t id;
size_t value;

bool operator==(const entry_t& other) const
{
return id == other.id && value == other.value;
}

template <typename Archive>
void serialize(Archive& ar)
{
ar(CEREAL_NVP(id), CEREAL_NVP(value));
}
};

TEST_CASE("basic")
{
auto x = immer::table<entry_t>{{.id = 1, .value = 2},
{.id = 3, .value = 4},
{.id = 12, .value = 42},
{.id = 0, .value = 5}};
auto y = cerealize(x);
CHECK(x == y);
}