Added macro guard for restricting serialization type

This commit is contained in:
Shane Grant 2013-06-14 15:01:46 -07:00
parent e66e6d38df
commit 618b9c5815
4 changed files with 28 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#ifndef CEREAL_BINARY_ARCHIVE_TUPLE_HPP_
#define CEREAL_BINARY_ARCHIVE_TUPLE_HPP_
#include <cereal/details/traits.hpp>
#include <cereal/binary_archive/binary_archive.hpp>
#include <tuple>
@ -24,7 +25,8 @@ namespace cereal
//! Serializing for std::tuple to binary
template <class Archive, class ... Types> inline
void serialize( Archive & ar, std::tuple<Types...> & tuple )
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
serialize( Archive & ar, std::tuple<Types...> & tuple )
{
tuple_size::serialize<std::tuple_size<std::tuple<Types...>>>( ar, tuple );
}

View File

@ -8,7 +8,8 @@ namespace cereal
{
//! Serializing for std::pair to binary
template <class Archive, class T1, class T2> inline
void serialize( Archive & ar, std::pair<T1, T2> & pair )
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
serialize( Archive & ar, std::pair<T1, T2> & pair )
{
ar & pair.first;
ar & pair.second;

View File

@ -105,6 +105,28 @@ namespace cereal
{
return rank == 0 ? 1 : std::extent<T>::value * sizeofArray<typename std::remove_extent<T>::type>( rank - 1 );
}
// ######################################################################
//! A macro to use to restrict which types of archives your serialize function will work for.
/*! This requires you to have a template class parameter named Archive and replaces the void return
type for your serialize function.
INTYPE refers to the input archive type you wish to restrict on.
OUTTYPE refers to the output archive type you wish to restrict on.
For example, if we want to limit a serialize to only work with binary serialization:
@code{.cpp}
template <class Archive>
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
serialize( Archive & ar, MyCoolType & m )
{
ar & m;
}
@endcode
*/
#define CEREAL_ARCHIVE_RESTRICT_SERIALIZE(INTYPE, OUTTYPE) \
typename std::enable_if<std::is_same<Archive, INTYPE>::value || std::is_same<Archive, OUTTYPE>::value, void>::type
}
}

View File

@ -2,6 +2,7 @@
#include <cereal/cereal.hpp>
#include <cereal/binary_archive/binary_archive.hpp>
#include <cereal/binary_archive/string.hpp>
#include <cereal/binary_archive/utility.hpp>
#include <cereal/json_archive/json_archive.hpp>
#include <cereal/binary_archive/memory.hpp>