ttg 1.0.0
Template Task Graph (TTG): flowgraph-based programming model for high-performance distributed-memory algorithms
Loading...
Searching...
No Matches
variant.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// Created by Eduard Valeyev on 6/22/21.
4//
5
6#ifndef TTG_SERIALIZATION_STD_VARIANT_H
7#define TTG_SERIALIZATION_STD_VARIANT_H
8
10
11#ifdef TTG_SERIALIZATION_SUPPORTS_MADNESS
12// MADNESS does not supports std::variant serialization
13#endif
14
15#ifdef TTG_SERIALIZATION_SUPPORTS_BOOST
16
17namespace boost {
18 namespace serialization {
19
20 namespace detail {
21
22 template <typename Archive, typename... Ts, std::size_t I0, std::size_t... Is>
23 Archive& variant_load_impl(Archive& ar, std::variant<Ts...>& v, std::size_t which,
24 std::index_sequence<I0, Is...>) {
25 constexpr bool writing = ttg::detail::is_output_archive_v<Archive>;
26 static_assert(!writing);
27 if (which == I0) {
28 using type = std::variant_alternative_t<I0, std::variant<Ts...>>;
29 if (!std::is_same_v<type, std::monostate>) {
30 type value;
31 ar& value;
32 v.template emplace<I0>(std::move(value));
33 }
34 } else {
35 if constexpr (sizeof...(Is) == 0)
36 throw std::logic_error(
37 "boost::serialization::detail::variant_load_impl(ar,v,idx,idxs): idx is not present in idxs");
38 else
39 return variant_load_impl(ar, v, which, std::index_sequence<Is...>{});
40 }
41 return ar;
42 }
43
44 } // namespace detail
45
46 template <typename Archive, typename... Ts>
47 Archive& serialize(Archive& ar, std::variant<Ts...>& t, const unsigned int version) {
48 constexpr bool writing = ttg::detail::is_output_archive_v<Archive>;
49 const auto index = t.index();
50 ar& index;
51 // to write visit the current alternative
52 if constexpr (writing) {
53 std::visit(
54 [&ar](const auto& v) {
55 if constexpr (!std::is_same_v<std::decay_t<decltype(v)>, std::monostate>) ar& v;
56 },
57 t);
58 } else // reading by recursive traversal until found index
59 detail::variant_load_impl(ar, t, index, std::make_index_sequence<sizeof...(Ts)>{});
60 return ar;
61 }
62
63 } // namespace serialization
64} // namespace boost
65
66namespace ttg::detail {
67 template <typename Archive, typename... Ts>
68 inline static constexpr bool is_stlcontainer_boost_serializable_v<Archive, std::variant<Ts...>> =
69 (is_boost_serializable_v<Archive, Ts> && ...);
70 template <typename Archive, typename... Ts>
71 inline static constexpr bool is_stlcontainer_boost_serializable_v<Archive, const std::variant<Ts...>> =
72 (is_boost_serializable_v<Archive, const Ts> && ...);
73} // namespace ttg::detail
74
75#endif // TTG_SERIALIZATION_SUPPORTS_BOOST
76
77#endif // TTG_SERIALIZATION_STD_TUPLE_H