diff --git a/lager/extra/cereal/immer_table.hpp b/lager/extra/cereal/immer_table.hpp new file mode 100644 index 00000000..4161487e --- /dev/null +++ b/lager/extra/cereal/immer_table.hpp @@ -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: +// + +#pragma once + +#include + +#include + +#include +#include +#include + +namespace cereal { +// This code has mostly been adapted from +// We don't deal for now with data that could be potentially serialized +// directly in binary format. + +template +void CEREAL_SAVE_FUNCTION_NAME(Archive& ar, + const immer::table& table) +{ + ar(make_size_tag(static_cast(table.size()))); + for (auto&& v : table) + ar(v); +} + +template +void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, + immer::table& table) +{ + size_type size{}; + ar(make_size_tag(size)); + + if (!size) + return; + + auto t = immer::table{}.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 diff --git a/test/cereal/immer_table.cpp b/test/cereal/immer_table.cpp new file mode 100644 index 00000000..bf76aa5d --- /dev/null +++ b/test/cereal/immer_table.cpp @@ -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: +// + +#include "cerealize.hpp" +#include +#include + +struct entry_t +{ + size_t id; + size_t value; +}; + +TEST_CASE("basic") +{ + auto x = immer::table{{.id = 1, .value = 2}, // + {.id = 3, .value = 4}}; + auto y = cerealize(x); + CHECK(x == y); +}