Files
cereal/binary_archive/bitset.hpp
Shane Grant e2d07a1670 Added unit tests for complex and bitset
Made all template functions for the various types inline
2013-06-16 11:31:59 -07:00

90 lines
1.8 KiB
C++

#ifndef CEREAL_BINARY_ARCHIVE_BITSET_HPP_
#define CEREAL_BINARY_ARCHIVE_BITSET_HPP_
#include <cereal/binary_archive/binary_archive.hpp>
#include <bitset>
namespace cereal
{
namespace bitset_detail
{
enum class type : uint8_t
{
ulong,
ullong,
string
};
template <class Archive> inline
CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive)
serialize( Archive & ar, type & t )
{
ar & reinterpret_cast<uint8_t &>( t );
}
}
//! Serializing (save) for std::bitset to binary
template <size_t N> inline
void save( BinaryOutputArchive & ar, std::bitset<N> const & bits )
{
try
{
auto const b = bits.to_ulong();
ar & bitset_detail::type::ulong;
ar & b;
}
catch( std::overflow_error const & e )
{
try
{
auto const b = bits.to_ullong();
ar & bitset_detail::type::ullong;
ar & b;
}
catch( std::overflow_error const & e )
{
ar & bitset_detail::type::string;
ar & bits.to_string();
}
}
}
//! Serializing (load) for std::bitset to binary
template <size_t N> inline
void load( BinaryInputArchive & ar, std::bitset<N> & bits )
{
bitset_detail::type t;
ar & t;
switch( t )
{
case bitset_detail::type::ulong:
{
unsigned long b;
ar & b;
bits = std::bitset<N>( b );
break;
}
case bitset_detail::type::ullong:
{
unsigned long long b;
ar & b;
bits = std::bitset<N>( b );
break;
}
case bitset_detail::type::string:
{
std::string b;
ar & b;
bits = std::bitset<N>( b );
break;
}
default:
throw 1;
// TODO: emit some kind of error
}
}
} // namespace cereal
#endif // CEREAL_BINARY_ARCHIVE_BITSET_HPP_