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 cereal 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 RANDOLPH VOORHIES OR SHANE GRANT 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::size_t DataSize> inline
void saveBinary( const void * data, std::size_t size )
{
std::size_t writtenSize = 0;
if( itsConvertEndianness )
{
for( std::size_t i = 0; i < size; i += DataSize )
for( std::size_t j = 0; j < DataSize; ++j )
writtenSize += static_cast<std::size_t>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ) + DataSize - j - 1 + i, 1 ) );
}
else
writtenSize = static_cast<std::size_t>( 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::size_t DataSize> inline
void loadBinary( void * const data, std::size_t size )
{
// load data
auto const readSize = static_cast<std::size_t>( 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::size_t 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::size_t>( 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::size_t>( 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_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::BinaryData::data
PT data
pointer to beginning of data
Definition: helpers.hpp:217
cereal::SizeTag
A wrapper around size metadata.
Definition: helpers.hpp:271
cereal::PortableBinaryInputArchive::Options
A class containing various advanced options for the PortableBinaryInput archive.
Definition: portable_binary.hpp:184
cereal::PortableBinaryOutputArchive::Options
A class containing various advanced options for the PortableBinaryOutput archive. ...
Definition: portable_binary.hpp:82
cereal::PortableBinaryInputArchive::Options::LittleEndian
static Options LittleEndian()
Load into little endian.
Definition: portable_binary.hpp:195
cereal::InputArchive
The base input archive class.
Definition: cereal.hpp:585
cereal::PortableBinaryOutputArchive::Options::LittleEndian
static Options LittleEndian()
Save as little endian.
Definition: portable_binary.hpp:93
CEREAL_SERIALIZE_FUNCTION_NAME
#define CEREAL_SERIALIZE_FUNCTION_NAME
The serialization/deserialization function name to search for.
Definition: macros.hpp:64
cereal::PortableBinaryInputArchive::Options::Default
static Options Default()
Default options, preserve system endianness.
Definition: portable_binary.hpp:192
cereal::PortableBinaryOutputArchive::PortableBinaryOutputArchive
PortableBinaryOutputArchive(std::ostream &stream, Options const &options=Options::Default())
Construct, outputting to the provided stream.
Definition: portable_binary.hpp:120
cereal
Definition: access.hpp:40
CEREAL_REGISTER_ARCHIVE
#define CEREAL_REGISTER_ARCHIVE(Archive)
Registers a specific Archive type with cereal.
Definition: cereal.hpp:141
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:1293
cereal.hpp
Main cereal functionality.
cereal::NameValuePair
For holding name value pairs.
Definition: helpers.hpp:135
CEREAL_LOAD_FUNCTION_NAME
#define CEREAL_LOAD_FUNCTION_NAME
The deserialization (load) function name to search for.
Definition: macros.hpp:71
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::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::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::PortableBinaryInputArchive::Options::BigEndian
static Options BigEndian()
Load into big endian.
Definition: portable_binary.hpp:198
CEREAL_NOEXCEPT
#define CEREAL_NOEXCEPT
Defines the CEREAL_NOEXCEPT macro to use instead of noexcept.
Definition: macros.hpp:116
cereal::BinaryData
A wrapper around data that can be serialized in a binary fashion.
Definition: helpers.hpp:207
cereal::PortableBinaryInputArchive::Options::Options
Options(Endianness inputEndian=getEndianness())
Specify specific options for the PortableBinaryInputArchive.
Definition: portable_binary.hpp:202
cereal::OutputArchive
The base output archive class.
Definition: cereal.hpp:234
cereal::PortableBinaryOutputArchive::saveBinary
void saveBinary(const void *data, std::size_t size)
Writes size bytes of data to the output stream.
Definition: portable_binary.hpp:132
cereal::PortableBinaryInputArchive
An input archive designed to load data saved using PortableBinaryOutputArchive.
Definition: portable_binary.hpp:180
CEREAL_SAVE_FUNCTION_NAME
#define CEREAL_SAVE_FUNCTION_NAME
The serialization (save) function name to search for.
Definition: macros.hpp:78
cereal::PortableBinaryOutputArchive::Options::Default
static Options Default()
Default options, preserve system endianness.
Definition: portable_binary.hpp:90
cereal::BinaryData::size
uint64_t size
size in bytes
Definition: helpers.hpp:218
cereal::PortableBinaryOutputArchive::Options::BigEndian
static Options BigEndian()
Save as big endian.
Definition: portable_binary.hpp:96
cereal::PortableBinaryInputArchive::loadBinary
void loadBinary(void *const data, std::size_t 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::Exception
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:48
cereal::PortableBinaryInputArchive::PortableBinaryInputArchive
PortableBinaryInputArchive(std::istream &stream, Options const &options=Options::Default())
Construct, loading from the provided stream.
Definition: portable_binary.hpp:222
cereal::PortableBinaryOutputArchive
An output archive designed to save data in a compact binary representation portable over different ar...
Definition: portable_binary.hpp:78