Skip to content

Commit

Permalink
add immer table cerealize as vector style
Browse files Browse the repository at this point in the history
  • Loading branch information
asa committed Mar 9, 2024
1 parent 53dfad0 commit de68a13
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
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
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);
}

0 comments on commit de68a13

Please sign in to comment.