Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed support for boost variant. #768

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 64 additions & 10 deletions include/cereal/types/boost_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
\ingroup OtherTypes */
/*
Copyright (c) 2014, Randolph Voorhies, Shane Grant
Copyright (c) 2022, Nokia
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -38,8 +39,11 @@
#endif

#include "cereal/cereal.hpp"
#include <boost/variant/variant_fwd.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/size_t.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/variant_fwd.hpp>

namespace cereal
{
Expand Down Expand Up @@ -130,6 +134,62 @@ namespace cereal
load_variant_impl( ar, variant, typename std::is_default_constructible<T>::type() );
}
};

//! @internal
template<size_t ... indices>
struct sequence
{
};

//! @internal
template<typename S1, std::size_t x>
struct push_back;

//! @internal
template<std::size_t ... indices, std::size_t x>
struct push_back<sequence<indices...>, x>
{
using type = sequence<indices..., x>;
};

//! @internal
template<std::size_t N>
struct make_sequence;

//! @internal
template<>
struct make_sequence<0>
{
using type = sequence<>;
};

//! @internal
template<std::size_t N>
struct make_sequence
{
using type = typename push_back<typename make_sequence<N - 1>::type, N - 1>::type;
};

//! @internal
template <class Archive, typename ... VariantTypes, size_t ... indices> inline
void load_variant_with_sequence( Archive & ar, boost::variant<VariantTypes...> & variant, sequence<indices...>)
{
int32_t which;
ar( CEREAL_NVP_("which", which) );

using TypesSequence = typename boost::variant<VariantTypes...>::types;
using LoadFuncType = void(*)(Archive &, boost::variant<VariantTypes...> &);
CEREAL_CONSTEXPR_LAMBDA LoadFuncType loadFuncArray[] = {
&boost_variant_detail::load_variant_wrapper<typename boost::mpl::at<
TypesSequence,
boost::mpl::size_t<indices>>::type>::load_variant...};

if(which >= int32_t(sizeof(loadFuncArray)/sizeof(loadFuncArray[0])))
throw Exception("Invalid 'which' selector when deserializing boost::variant");

loadFuncArray[which](ar, variant);
}

} // namespace boost_variant_detail

//! Saving for boost::variant
Expand All @@ -146,16 +206,10 @@ namespace cereal
template <class Archive, typename ... VariantTypes> inline
void CEREAL_LOAD_FUNCTION_NAME( Archive & ar, boost::variant<VariantTypes...> & variant )
{
int32_t which;
ar( CEREAL_NVP_("which", which) );

using LoadFuncType = void(*)(Archive &, boost::variant<VariantTypes...> &);
CEREAL_CONSTEXPR_LAMBDA LoadFuncType loadFuncArray[] = {&boost_variant_detail::load_variant_wrapper<VariantTypes>::load_variant...};

if(which >= int32_t(sizeof(loadFuncArray)/sizeof(loadFuncArray[0])))
throw Exception("Invalid 'which' selector when deserializing boost::variant");
using TypesSequence = typename boost::variant<VariantTypes...>::types;
using IndexSequence = typename boost_variant_detail::make_sequence<boost::mpl::size<TypesSequence>::value>::type;

loadFuncArray[which](ar, variant);
load_variant_with_sequence(ar, variant, IndexSequence());
}
} // namespace cereal

Expand Down
20 changes: 16 additions & 4 deletions unittests/boost/boost_variant.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright (c) 2015, Kyle Fleming
Copyright (c) 2022, Nokia
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -29,6 +30,7 @@

#include "../common.hpp"
#include <cereal/types/boost_variant.hpp>
#include <boost/mpl/vector/vector30.hpp>
#include <boost/variant.hpp>

struct NonDefaultConstructible
Expand Down Expand Up @@ -58,14 +60,12 @@ template <> struct LoadAndConstruct<NonDefaultConstructible>
};
} // end namespace cereal

template <class IArchive, class OArchive> inline
void test_boost_variant()
template <class IArchive, class OArchive, typename VariantType> inline
void test_boost_variant_case()
{
std::random_device rd;
std::mt19937 gen(rd());

using VariantType = boost::variant<int, double, std::string, NonDefaultConstructible>;

VariantType o_bv1 = random_value<int>(gen);
VariantType o_bv2 = random_value<double>(gen);
VariantType o_bv3 = random_basic_string<char>(gen);
Expand Down Expand Up @@ -102,4 +102,16 @@ void test_boost_variant()
CHECK_EQ( boost::get<NonDefaultConstructible>(i_bv4).index, boost::get<NonDefaultConstructible>(o_bv4).index );
}

template <class IArchive, class OArchive> inline
void test_boost_variant()
{
using VariantType = boost::variant<int, double, std::string, NonDefaultConstructible>;

using TypeList = boost::mpl::vector4<int, double, std::string, NonDefaultConstructible>;
using OversizedVariantType = boost::make_variant_over<TypeList>::type;

test_boost_variant_case<IArchive, OArchive, VariantType>();
test_boost_variant_case<IArchive, OArchive, OversizedVariantType>();
}

#endif // CEREAL_TEST_BOOST_VARIANT_H_