portable_binary.hpp
/*
Copyright (c) 2014, Randolph Voorhies, Shane Grant
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_
#define CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_
#include "cereal/cereal.hpp"
#include <sstream>
#include <limits>
namespace cereal
{
namespace portable_binary_detail
{
inline std::uint8_t is_little_endian()
{
static std::int32_t test = 1;
return *reinterpret_cast<std::int8_t*>( &test ) == 1;
}
template <std::size_t DataSize>
inline void swap_bytes( std::uint8_t * data )
{
for( std::size_t i = 0, end = DataSize / 2; i < end; ++i )
std::swap( data[i], data[DataSize - i - 1] );
}
} // end namespace portable_binary_detail
// ######################################################################
class PortableBinaryOutputArchive : public OutputArchive<PortableBinaryOutputArchive, AllowEmptyClassElision>
{
public:
class Options
{
public:
enum class Endianness : std::uint8_t
{ big, little };
static Options Default(){ return Options(); }
static Options LittleEndian(){ return Options( Endianness::little ); }
static Options BigEndian(){ return Options( Endianness::big ); }
explicit Options( Endianness outputEndian = getEndianness() ) :
itsOutputEndianness( outputEndian ) { }
private:
inline static Endianness getEndianness()
{ return portable_binary_detail::is_little_endian() ? Endianness::little : Endianness::big; }
inline std::uint8_t is_little_endian() const
{ return itsOutputEndianness == Endianness::little; }
friend class PortableBinaryOutputArchive;
Endianness itsOutputEndianness;
};
PortableBinaryOutputArchive(std::ostream & stream, Options const & options = Options::Default()) :
OutputArchive<PortableBinaryOutputArchive, AllowEmptyClassElision>(this),
itsStream(stream),
itsConvertEndianness( portable_binary_detail::is_little_endian() ^ options.is_little_endian() )
{
this->operator()( options.is_little_endian() );
}
~PortableBinaryOutputArchive() CEREAL_NOEXCEPT = default;
template <std::streamsize DataSize> inline
void saveBinary( const void * data, std::streamsize size )
{
std::streamsize writtenSize = 0;
if( itsConvertEndianness )
{
for( std::streamsize i = 0; i < size; i += DataSize )
for( std::streamsize j = 0; j < DataSize; ++j )
writtenSize += itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ) + DataSize - j - 1 + i, 1 );
}
else
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;
const uint8_t itsConvertEndianness;
};
// ######################################################################
class PortableBinaryInputArchive : public InputArchive<PortableBinaryInputArchive, AllowEmptyClassElision>
{
public:
class Options
{
public:
enum class Endianness : std::uint8_t
{ big, little };
static Options Default(){ return Options(); }
static Options LittleEndian(){ return Options( Endianness::little ); }
static Options BigEndian(){ return Options( Endianness::big ); }
explicit Options( Endianness inputEndian = getEndianness() ) :
itsInputEndianness( inputEndian ) { }
private:
inline static Endianness getEndianness()
{ return portable_binary_detail::is_little_endian() ? Endianness::little : Endianness::big; }
inline std::uint8_t is_little_endian() const
{ return itsInputEndianness == Endianness::little; }
friend class PortableBinaryInputArchive;
Endianness itsInputEndianness;
};
PortableBinaryInputArchive(std::istream & stream, Options const & options = Options::Default()) :
InputArchive<PortableBinaryInputArchive, AllowEmptyClassElision>(this),
itsStream(stream),
itsConvertEndianness( false )
{
uint8_t streamLittleEndian;
this->operator()( streamLittleEndian );
itsConvertEndianness = options.is_little_endian() ^ streamLittleEndian;
}
~PortableBinaryInputArchive() CEREAL_NOEXCEPT = default;
template <std::streamsize DataSize> inline
void loadBinary( void * const data, std::streamsize size )
{
// load data
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));
// flip bits if needed
if( itsConvertEndianness )
{
std::uint8_t * ptr = reinterpret_cast<std::uint8_t*>( data );
for( std::streamsize i = 0; i < size; i += DataSize )
portable_binary_detail::swap_bytes<DataSize>( ptr + i );
}
}
private:
std::istream & itsStream;
uint8_t itsConvertEndianness;
};
// ######################################################################
// Common BinaryArchive serialization functions
template<class T> inline
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
CEREAL_SAVE_FUNCTION_NAME(PortableBinaryOutputArchive & ar, T const & t)
{
static_assert( !std::is_floating_point<T>::value ||
(std::is_floating_point<T>::value && std::numeric_limits<T>::is_iec559),
"Portable binary only supports IEEE 754 standardized floating point" );
ar.template saveBinary<sizeof(T)>(std::addressof(t), sizeof(t));
}
template<class T> inline
typename std::enable_if<std::is_arithmetic<T>::value, void>::type
CEREAL_LOAD_FUNCTION_NAME(PortableBinaryInputArchive & ar, T & t)
{
static_assert( !std::is_floating_point<T>::value ||
(std::is_floating_point<T>::value && std::numeric_limits<T>::is_iec559),
"Portable binary only supports IEEE 754 standardized floating point" );
ar.template loadBinary<sizeof(T)>(std::addressof(t), sizeof(t));
}
template <class Archive, class T> inline
CEREAL_ARCHIVE_RESTRICT(PortableBinaryInputArchive, PortableBinaryOutputArchive)
CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, NameValuePair<T> & t )
{
ar( t.value );
}
template <class Archive, class T> inline
CEREAL_ARCHIVE_RESTRICT(PortableBinaryInputArchive, PortableBinaryOutputArchive)
CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, SizeTag<T> & t )
{
ar( t.size );
}
template <class T> inline
void CEREAL_SAVE_FUNCTION_NAME(PortableBinaryOutputArchive & ar, BinaryData<T> const & bd)
{
typedef typename std::remove_pointer<T>::type TT;
static_assert( !std::is_floating_point<TT>::value ||
(std::is_floating_point<TT>::value && std::numeric_limits<TT>::is_iec559),
"Portable binary only supports IEEE 754 standardized floating point" );
ar.template saveBinary<sizeof(TT)>( bd.data, static_cast<std::streamsize>( bd.size ) );
}
template <class T> inline
void CEREAL_LOAD_FUNCTION_NAME(PortableBinaryInputArchive & ar, BinaryData<T> & bd)
{
typedef typename std::remove_pointer<T>::type TT;
static_assert( !std::is_floating_point<TT>::value ||
(std::is_floating_point<TT>::value && std::numeric_limits<TT>::is_iec559),
"Portable binary only supports IEEE 754 standardized floating point" );
ar.template loadBinary<sizeof(TT)>( bd.data, static_cast<std::streamsize>( bd.size ) );
}
} // namespace cereal
// register archives for polymorphic support
CEREAL_REGISTER_ARCHIVE(cereal::PortableBinaryOutputArchive)
CEREAL_REGISTER_ARCHIVE(cereal::PortableBinaryInputArchive)
// tie input and output archives together
CEREAL_SETUP_ARCHIVE_TRAITS(cereal::PortableBinaryInputArchive, cereal::PortableBinaryOutputArchive)
#endif // CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_
cereal::InputArchive
The base input archive class.
Definition: cereal.hpp:710
cereal::PortableBinaryOutputArchive::Options::Default
static Options Default()
Default options, preserve system endianness.
Definition: portable_binary.hpp:90
cereal::PortableBinaryInputArchive::loadBinary
void loadBinary(void *const data, std::streamsize size)
Reads size bytes of data from the input stream.
Definition: portable_binary.hpp:239
cereal::PortableBinaryInputArchive::Options::Endianness
Endianness
Represents desired endianness.
Definition: portable_binary.hpp:188
cereal::PortableBinaryOutputArchive::PortableBinaryOutputArchive
PortableBinaryOutputArchive(std::ostream &stream, Options const &options=Options::Default())
Construct, outputting to the provided stream.
Definition: portable_binary.hpp:120
cereal::PortableBinaryInputArchive::PortableBinaryInputArchive
PortableBinaryInputArchive(std::istream &stream, Options const &options=Options::Default())
Construct, loading from the provided stream.
Definition: portable_binary.hpp:222
cereal::PortableBinaryInputArchive::Options::Default
static Options Default()
Default options, preserve system endianness.
Definition: portable_binary.hpp:192
cereal::InputArchive< PortableBinaryInputArchive, AllowEmptyClassElision >::operator()
PortableBinaryInputArchive & operator()(Types &&... args)
Serializes all passed in data.
Definition: cereal.hpp:728
CEREAL_ARCHIVE_RESTRICT
#define CEREAL_ARCHIVE_RESTRICT(INTYPE, OUTTYPE)
A macro to use to restrict which types of archives your function will work for.
Definition: traits.hpp:1315
cereal::PortableBinaryInputArchive
An input archive designed to load data saved using PortableBinaryOutputArchive.
Definition: portable_binary.hpp:180
CEREAL_SERIALIZE_FUNCTION_NAME
#define CEREAL_SERIALIZE_FUNCTION_NAME
The serialization/deserialization function name to search for.
Definition: macros.hpp:78
CEREAL_LOAD_FUNCTION_NAME
#define CEREAL_LOAD_FUNCTION_NAME
The deserialization (load) function name to search for.
Definition: macros.hpp:85
cereal::PortableBinaryInputArchive::Options::Options
Options(Endianness inputEndian=getEndianness())
Specify specific options for the PortableBinaryInputArchive.
Definition: portable_binary.hpp:202
CEREAL_REGISTER_ARCHIVE
#define CEREAL_REGISTER_ARCHIVE(Archive)
Registers a specific Archive type with cereal.
Definition: cereal.hpp:195
cereal::PortableBinaryOutputArchive::Options::LittleEndian
static Options LittleEndian()
Save as little endian.
Definition: portable_binary.hpp:93
cereal::OutputArchive
The base output archive class.
Definition: cereal.hpp:318
cereal::PortableBinaryInputArchive::Options::BigEndian
static Options BigEndian()
Load into big endian.
Definition: portable_binary.hpp:198
cereal::PortableBinaryOutputArchive::saveBinary
void saveBinary(const void *data, std::streamsize size)
Writes size bytes of data to the output stream.
Definition: portable_binary.hpp:132
CEREAL_SAVE_FUNCTION_NAME
#define CEREAL_SAVE_FUNCTION_NAME
The serialization (save) function name to search for.
Definition: macros.hpp:92
CEREAL_SETUP_ARCHIVE_TRAITS
#define CEREAL_SETUP_ARCHIVE_TRAITS(InputArchive, OutputArchive)
Sets up traits that relate an input archive to an output archive.
Definition: traits.hpp:169
CEREAL_NOEXCEPT
#define CEREAL_NOEXCEPT
Defines the CEREAL_NOEXCEPT macro to use instead of noexcept.
Definition: macros.hpp:130
cereal::PortableBinaryOutputArchive::Options::BigEndian
static Options BigEndian()
Save as big endian.
Definition: portable_binary.hpp:96
cereal::PortableBinaryInputArchive::Options
A class containing various advanced options for the PortableBinaryInput archive.
Definition: portable_binary.hpp:184
cereal::portable_binary_detail::is_little_endian
std::uint8_t is_little_endian()
Returns true if the current machine is little endian.
Definition: portable_binary.hpp:42
cereal::portable_binary_detail::swap_bytes
void swap_bytes(std::uint8_t *data)
Swaps the order of bytes for some chunk of memory.
Definition: portable_binary.hpp:53
cereal.hpp
Main cereal functionality.
cereal::Exception
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:48
cereal::PortableBinaryOutputArchive::Options::Options
Options(Endianness outputEndian=getEndianness())
Specify specific options for the PortableBinaryOutputArchive.
Definition: portable_binary.hpp:100
cereal::PortableBinaryOutputArchive::Options::Endianness
Endianness
Represents desired endianness.
Definition: portable_binary.hpp:86
cereal::PortableBinaryOutputArchive
An output archive designed to save data in a compact binary representation portable over different ar...
Definition: portable_binary.hpp:78
cereal::OutputArchive< PortableBinaryOutputArchive, AllowEmptyClassElision >::operator()
PortableBinaryOutputArchive & operator()(Types &&... args)
Serializes all passed in data.
Definition: cereal.hpp:331
cereal::PortableBinaryInputArchive::Options::LittleEndian
static Options LittleEndian()
Load into little endian.
Definition: portable_binary.hpp:195
cereal::PortableBinaryOutputArchive::Options
A class containing various advanced options for the PortableBinaryOutput archive.
Definition: portable_binary.hpp:82