Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / histogram / detail / variant_proxy.hpp
1 // Copyright 2019 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #ifndef BOOST_HISTOGRAM_DETAIL_VARIANT_PROXY_HPP
8 #define BOOST_HISTOGRAM_DETAIL_VARIANT_PROXY_HPP
9
10 #include <boost/core/nvp.hpp>
11 #include <boost/histogram/axis/traits.hpp> // variant_access
12 #include <boost/histogram/detail/static_if.hpp>
13 #include <boost/mp11/algorithm.hpp> // mp_with_index, mp_find, mp_at
14 #include <boost/mp11/list.hpp>      // mp_size
15 #include <boost/throw_exception.hpp>
16 #include <stdexcept>
17
18 namespace boost {
19 namespace histogram {
20 namespace detail {
21
22 // This is a workaround to remain backward compatible in the serialization format. The
23 // proxy uses only the public interface of axis::variant for serialization and works
24 // independently of the underlying variant implementation.
25 template <class Variant>
26 struct variant_proxy {
27   Variant& variant;
28
29   template <class Archive>
30   void serialize(Archive& ar, unsigned /* version */) {
31     detail::static_if_c<Archive::is_loading::value>(
32         [this](auto& ar) { // loading
33           int which = 0;
34           ar >> make_nvp("which", which);
35           constexpr unsigned N = mp11::mp_size<Variant>::value;
36           if (which < 0 || static_cast<unsigned>(which) >= N)
37             // throw if which >= N, can happen if type was removed from variant
38             BOOST_THROW_EXCEPTION(
39                 std::runtime_error("variant has fewer types than stored version"));
40           mp11::mp_with_index<N>(static_cast<unsigned>(which), [&ar, this](auto i) {
41             using T = mp11::mp_at_c<Variant, i>;
42             T value;
43             ar >> make_nvp("value", value);
44             this->variant = std::move(value);
45             T* new_address = variant_access::template get_if<T>(&this->variant);
46             ar.reset_object_address(new_address, &value);
47           });
48         },
49         [this](auto& ar) { // saving
50           visit(
51               [&ar](const auto& value) {
52                 using T = std::decay_t<decltype(value)>;
53                 const int which = static_cast<int>(mp11::mp_find<Variant, T>::value);
54                 ar << make_nvp("which", which);
55                 ar << make_nvp("value", value);
56               },
57               this->variant);
58         },
59         ar);
60   }
61 };
62
63 } // namespace detail
64 } // namespace histogram
65 } // namespace boost
66
67 #endif