variant_proxy.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef BOOST_HISTOGRAM_DETAIL_VARIANT_PROXY_HPP
  7. #define BOOST_HISTOGRAM_DETAIL_VARIANT_PROXY_HPP
  8. #include <boost/core/nvp.hpp>
  9. #include <boost/histogram/axis/traits.hpp> // variant_access
  10. #include <boost/histogram/detail/static_if.hpp>
  11. #include <boost/mp11/algorithm.hpp> // mp_with_index, mp_find, mp_at
  12. #include <boost/mp11/list.hpp> // mp_size
  13. #include <boost/throw_exception.hpp>
  14. #include <stdexcept>
  15. namespace boost {
  16. namespace histogram {
  17. namespace detail {
  18. // This is a workaround to remain backward compatible in the serialization format. The
  19. // proxy uses only the public interface of axis::variant for serialization and works
  20. // independently of the underlying variant implementation.
  21. template <class Variant>
  22. struct variant_proxy {
  23. Variant& variant;
  24. template <class Archive>
  25. void serialize(Archive& ar, unsigned /* version */) {
  26. detail::static_if_c<Archive::is_loading::value>(
  27. [this](auto& ar) { // loading
  28. int which = 0;
  29. ar >> make_nvp("which", which);
  30. constexpr unsigned N = mp11::mp_size<Variant>::value;
  31. if (which < 0 || static_cast<unsigned>(which) >= N)
  32. // throw if which >= N, can happen if type was removed from variant
  33. BOOST_THROW_EXCEPTION(
  34. std::runtime_error("variant has fewer types than stored version"));
  35. mp11::mp_with_index<N>(static_cast<unsigned>(which), [&ar, this](auto i) {
  36. using T = mp11::mp_at_c<Variant, i>;
  37. T value;
  38. ar >> make_nvp("value", value);
  39. this->variant = std::move(value);
  40. T* new_address = variant_access::template get_if<T>(&this->variant);
  41. ar.reset_object_address(new_address, &value);
  42. });
  43. },
  44. [this](auto& ar) { // saving
  45. visit(
  46. [&ar](const auto& value) {
  47. using T = std::decay_t<decltype(value)>;
  48. const int which = static_cast<int>(mp11::mp_find<Variant, T>::value);
  49. ar << make_nvp("which", which);
  50. ar << make_nvp("value", value);
  51. },
  52. this->variant);
  53. },
  54. ar);
  55. }
  56. };
  57. } // namespace detail
  58. } // namespace histogram
  59. } // namespace boost
  60. #endif