variant.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*! \file variant.hpp
  2. \brief Support for std::variant
  3. \ingroup STLSupport */
  4. /*
  5. Copyright (c) 2014, 2017, Randolph Voorhies, Shane Grant, Juan Pedro
  6. Bolivar Puente. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. * Neither the name of the copyright holder nor the
  15. names of its contributors may be used to endorse or promote products
  16. derived from this software without specific prior written permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  21. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef CEREAL_TYPES_STD_VARIANT_HPP_
  29. #define CEREAL_TYPES_STD_VARIANT_HPP_
  30. #include "cereal/cereal.hpp"
  31. #include <variant>
  32. #include <cstdint>
  33. namespace cereal
  34. {
  35. namespace variant_detail
  36. {
  37. //! @internal
  38. template <class Archive>
  39. struct variant_save_visitor
  40. {
  41. variant_save_visitor(Archive & ar_) : ar(ar_) {}
  42. template<class T>
  43. void operator()(T const & value) const
  44. {
  45. ar( CEREAL_NVP_("data", value) );
  46. }
  47. Archive & ar;
  48. };
  49. //! @internal
  50. template<int N, class Variant, class Archive>
  51. typename std::enable_if<N == std::variant_size_v<Variant>, void>::type
  52. load_variant(Archive & /*ar*/, int /*target*/, Variant & /*variant*/)
  53. {
  54. throw ::cereal::Exception("Error traversing variant during load");
  55. }
  56. //! @internal
  57. template<int N, class Variant, class Archive>
  58. typename std::enable_if<N < std::variant_size_v<Variant>, void>::type
  59. load_variant(Archive & ar, int target, Variant & variant)
  60. {
  61. if(N == target)
  62. {
  63. variant.template emplace<N>();
  64. ar( CEREAL_NVP_("data", std::get<N>(variant)) );
  65. }
  66. else
  67. load_variant<N+1>(ar, target, variant);
  68. }
  69. } // namespace variant_detail
  70. //! Saving for std::variant
  71. template <class Archive, typename VariantType1, typename... VariantTypes> inline
  72. void CEREAL_SAVE_FUNCTION_NAME( Archive & ar, std::variant<VariantType1, VariantTypes...> const & variant )
  73. {
  74. std::int32_t index = static_cast<std::int32_t>(variant.index());
  75. ar( CEREAL_NVP_("index", index) );
  76. variant_detail::variant_save_visitor<Archive> visitor(ar);
  77. std::visit(visitor, variant);
  78. }
  79. //! Loading for std::variant
  80. template <class Archive, typename... VariantTypes> inline
  81. void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, std::variant<VariantTypes...> & variant )
  82. {
  83. using variant_t = typename std::variant<VariantTypes...>;
  84. std::int32_t index;
  85. ar( CEREAL_NVP_("index", index) );
  86. if(index >= static_cast<std::int32_t>(std::variant_size_v<variant_t>))
  87. throw Exception("Invalid 'index' selector when deserializing std::variant");
  88. variant_detail::load_variant<0>(ar, index, variant);
  89. }
  90. //! Serializing a std::monostate
  91. template <class Archive>
  92. void CEREAL_SERIALIZE_FUNCTION_NAME( Archive &, std::monostate const & ) {}
  93. } // namespace cereal
  94. #endif // CEREAL_TYPES_STD_VARIANT_HPP_