123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #ifndef CEREAL_ARCHIVES_BINARY_HPP_
- #define CEREAL_ARCHIVES_BINARY_HPP_
- #include "cereal/cereal.hpp"
- #include <sstream>
- namespace cereal
- {
-
-
-
- class BinaryOutputArchive : public OutputArchive<BinaryOutputArchive, AllowEmptyClassElision>
- {
- public:
-
-
- BinaryOutputArchive(std::ostream & stream) :
- OutputArchive<BinaryOutputArchive, AllowEmptyClassElision>(this),
- itsStream(stream)
- { }
- ~BinaryOutputArchive() CEREAL_NOEXCEPT = default;
-
- void saveBinary( const void * data, std::streamsize size )
- {
- auto const writtenSize = itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size );
- if(writtenSize != size)
- throw Exception("Failed to write " + std::to_string(size) + " bytes to output stream! Wrote " + std::to_string(writtenSize));
- }
- private:
- std::ostream & itsStream;
- };
-
-
-
- class BinaryInputArchive : public InputArchive<BinaryInputArchive, AllowEmptyClassElision>
- {
- public:
-
- BinaryInputArchive(std::istream & stream) :
- InputArchive<BinaryInputArchive, AllowEmptyClassElision>(this),
- itsStream(stream)
- { }
- ~BinaryInputArchive() CEREAL_NOEXCEPT = default;
-
- void loadBinary( void * const data, std::streamsize size )
- {
- auto const readSize = itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size );
- if(readSize != size)
- throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
- }
- private:
- std::istream & itsStream;
- };
-
-
-
- template<class T> inline
- typename std::enable_if<std::is_arithmetic<T>::value, void>::type
- CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, T const & t)
- {
- ar.saveBinary(std::addressof(t), sizeof(t));
- }
-
- template<class T> inline
- typename std::enable_if<std::is_arithmetic<T>::value, void>::type
- CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, T & t)
- {
- ar.loadBinary(std::addressof(t), sizeof(t));
- }
-
- template <class Archive, class T> inline
- CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
- CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, NameValuePair<T> & t )
- {
- ar( t.value );
- }
-
- template <class Archive, class T> inline
- CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
- CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, SizeTag<T> & t )
- {
- ar( t.size );
- }
-
- template <class T> inline
- void CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, BinaryData<T> const & bd)
- {
- ar.saveBinary( bd.data, static_cast<std::streamsize>( bd.size ) );
- }
-
- template <class T> inline
- void CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, BinaryData<T> & bd)
- {
- ar.loadBinary(bd.data, static_cast<std::streamsize>( bd.size ) );
- }
- }
- CEREAL_REGISTER_ARCHIVE(cereal::BinaryOutputArchive)
- CEREAL_REGISTER_ARCHIVE(cereal::BinaryInputArchive)
- CEREAL_SETUP_ARCHIVE_TRAITS(cereal::BinaryInputArchive, cereal::BinaryOutputArchive)
- #endif
|