-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from bluescarni/pr/new_anomalies
Several misc fixes/cleanups
- Loading branch information
Showing
25 changed files
with
868 additions
and
791 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
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
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
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
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
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
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
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,72 @@ | ||
// Copyright 2020, 2021, 2022, 2023 Francesco Biscani ([email protected]), Dario Izzo ([email protected]) | ||
// | ||
// This file is part of the heyoka library. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla | ||
// Public License v. 2.0. If a copy of the MPL was not distributed | ||
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef HEYOKA_DETAIL_OPTIONAL_S11N_HPP | ||
#define HEYOKA_DETAIL_OPTIONAL_S11N_HPP | ||
|
||
#if !defined(HEYOKA_BUILD_LIBRARY) | ||
|
||
#error This header can be included only when building heyoka. | ||
|
||
#endif | ||
|
||
#include <optional> | ||
|
||
#include <heyoka/s11n.hpp> | ||
|
||
namespace boost::serialization | ||
{ | ||
|
||
// NOTE: inspired by the boost::optional implementation. | ||
template <typename Archive, typename T> | ||
void save(Archive &ar, const std::optional<T> &opt, unsigned) | ||
{ | ||
// First check if opt contains something. | ||
const auto flag = static_cast<bool>(opt); | ||
ar << flag; | ||
|
||
if (flag) { | ||
// Save the contained value. | ||
ar << *opt; | ||
} | ||
} | ||
|
||
template <typename Archive, typename T> | ||
void load(Archive &ar, std::optional<T> &opt, unsigned) | ||
{ | ||
// Recover the flag. | ||
bool flag{}; | ||
ar >> flag; | ||
|
||
if (!flag) { | ||
// No value in the archive, | ||
// reset opt and return. | ||
opt.reset(); | ||
|
||
return; | ||
} | ||
|
||
if (!opt) { | ||
// opt is currently empty, reset it | ||
// to the def-cted value. | ||
opt = T{}; | ||
} | ||
|
||
// Deserialise the value. | ||
ar >> *opt; | ||
} | ||
|
||
template <typename Archive, typename T> | ||
void serialize(Archive &ar, std::optional<T> &opt, unsigned v) | ||
{ | ||
split_free(ar, opt, v); | ||
} | ||
|
||
} // namespace boost::serialization | ||
|
||
#endif |
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
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,109 @@ | ||
// Copyright 2020, 2021, 2022, 2023 Francesco Biscani ([email protected]), Dario Izzo ([email protected]) | ||
// | ||
// This file is part of the heyoka library. | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla | ||
// Public License v. 2.0. If a copy of the MPL was not distributed | ||
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef HEYOKA_DETAIL_VARIANT_S11N_HPP | ||
#define HEYOKA_DETAIL_VARIANT_S11N_HPP | ||
|
||
#if !defined(HEYOKA_BUILD_LIBRARY) | ||
|
||
#error This header can be included only when building heyoka. | ||
|
||
#endif | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <stdexcept> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <variant> | ||
|
||
#include <heyoka/config.hpp> | ||
#include <heyoka/s11n.hpp> | ||
|
||
HEYOKA_BEGIN_NAMESPACE | ||
|
||
namespace detail | ||
{ | ||
|
||
// Implementation detail for loading a std::variant. | ||
template <typename Archive, typename... Args, std::size_t... Is> | ||
void s11n_variant_load_impl(Archive &ar, std::variant<Args...> &var, std::size_t idx, std::index_sequence<Is...>) | ||
{ | ||
auto loader = [&ar, &var, idx](auto val) { | ||
constexpr auto N = decltype(val)::value; | ||
|
||
if (N == idx) { | ||
// NOTE: deserialise into a temporary, then move | ||
// it into the variant. | ||
std::variant_alternative_t<N, std::variant<Args...>> x; | ||
ar >> x; | ||
var = std::move(x); | ||
|
||
// Inform the archive of the new address | ||
// of the object we just deserialised. | ||
ar.reset_object_address(&std::get<N>(var), &x); | ||
|
||
return true; | ||
} else { | ||
assert(N < idx); | ||
|
||
return false; | ||
} | ||
}; | ||
|
||
// LCOV_EXCL_START | ||
[[maybe_unused]] auto ret = (loader(std::integral_constant<std::size_t, Is>{}) || ...); | ||
|
||
assert(ret); | ||
assert(var.index() == idx); | ||
// LCOV_EXCL_STOP | ||
} | ||
|
||
} // namespace detail | ||
|
||
HEYOKA_END_NAMESPACE | ||
|
||
namespace boost::serialization | ||
{ | ||
|
||
// NOTE: inspired by the boost::variant implementation. | ||
template <typename Archive, typename... Args> | ||
void save(Archive &ar, const std::variant<Args...> &var, unsigned) | ||
{ | ||
const auto idx = var.index(); | ||
|
||
ar << idx; | ||
|
||
std::visit([&ar](const auto &x) { ar << x; }, var); | ||
} | ||
|
||
template <typename Archive, typename... Args> | ||
void load(Archive &ar, std::variant<Args...> &var, unsigned) | ||
{ | ||
// Recover the variant index. | ||
std::size_t idx{}; | ||
ar >> idx; | ||
|
||
// LCOV_EXCL_START | ||
if (idx >= sizeof...(Args)) { | ||
throw std::invalid_argument("Invalid index loaded during the deserialisation of a variant"); | ||
} | ||
// LCOV_EXCL_STOP | ||
|
||
heyoka::detail::s11n_variant_load_impl(ar, var, idx, std::make_index_sequence<sizeof...(Args)>{}); | ||
} | ||
|
||
template <typename Archive, typename... Args> | ||
void serialize(Archive &ar, std::variant<Args...> &var, unsigned v) | ||
{ | ||
split_free(ar, var, v); | ||
} | ||
|
||
} // namespace boost::serialization | ||
|
||
#endif |
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
Oops, something went wrong.