#ifndef CEREAL_BINARY_ARCHIVE_BITSET_HPP_ #define CEREAL_BINARY_ARCHIVE_BITSET_HPP_ #include #include namespace cereal { namespace bitset_detail { enum class type : uint8_t { ulong, ullong, string }; template inline CEREAL_ARCHIVE_RESTRICT_SERIALIZE(BinaryInputArchive, BinaryOutputArchive) serialize( Archive & ar, type & t ) { ar & reinterpret_cast( t ); } } //! Serializing (save) for std::bitset to binary template inline void save( BinaryOutputArchive & ar, std::bitset 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 inline void load( BinaryInputArchive & ar, std::bitset & bits ) { bitset_detail::type t; ar & t; switch( t ) { case bitset_detail::type::ulong: { unsigned long b; ar & b; bits = std::bitset( b ); break; } case bitset_detail::type::ullong: { unsigned long long b; ar & b; bits = std::bitset( b ); break; } case bitset_detail::type::string: { std::string b; ar & b; bits = std::bitset( b ); break; } default: throw 1; // TODO: emit some kind of error } } } // namespace cereal #endif // CEREAL_BINARY_ARCHIVE_BITSET_HPP_