29 #ifndef CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_ 30 #define CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_ 38 namespace portable_binary_detail
44 static std::int32_t test = 1;
45 return *
reinterpret_cast<std::int8_t*
>( &test ) == 1;
52 template <std::
size_t DataSize>
55 for( std::size_t i = 0, end = DataSize / 2; i < end; ++i )
56 std::swap( data[i], data[DataSize - i - 1] );
94 auto const writtenSize =
static_cast<std::size_t
>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size ) );
96 if(writtenSize != size)
97 throw Exception(
"Failed to write " + std::to_string(size) +
" bytes to output stream! Wrote " + std::to_string(writtenSize));
101 std::ostream & itsStream;
138 itsConvertEndianness( false )
140 bool streamLittleEndian;
141 this->operator()( streamLittleEndian );
149 template <std::
size_t DataSize>
153 auto const readSize =
static_cast<std::size_t
>( itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size ) );
156 throw Exception(
"Failed to read " + std::to_string(size) +
" bytes from input stream! Read " + std::to_string(readSize));
159 if( itsConvertEndianness )
161 std::uint8_t * ptr =
reinterpret_cast<std::uint8_t*
>( data );
162 for( std::size_t i = 0; i < size; i += DataSize )
163 portable_binary_detail::swap_bytes<DataSize>( ptr );
168 std::istream & itsStream;
169 bool itsConvertEndianness;
176 template<
class T>
inline 177 typename std::enable_if<std::is_arithmetic<T>::value,
void>::type
180 static_assert( !std::is_floating_point<T>::value ||
181 (std::is_floating_point<T>::value && std::numeric_limits<T>::is_iec559),
182 "Portable binary only supports IEEE 754 standardized floating point" );
187 template<
class T>
inline 188 typename std::enable_if<std::is_arithmetic<T>::value,
void>::type
191 static_assert( !std::is_floating_point<T>::value ||
192 (std::is_floating_point<T>::value && std::numeric_limits<T>::is_iec559),
193 "Portable binary only supports IEEE 754 standardized floating point" );
194 ar.template loadBinary<sizeof(T)>(std::addressof(t),
sizeof(t));
198 template <
class Archive,
class T>
inline 206 template <
class Archive,
class T>
inline 214 template <
class T>
inline 217 typedef typename std::remove_pointer<T>::type TT;
218 static_assert( !std::is_floating_point<TT>::value ||
219 (std::is_floating_point<TT>::value && std::numeric_limits<TT>::is_iec559),
220 "Portable binary only supports IEEE 754 standardized floating point" );
226 template <
class T>
inline 229 typedef typename std::remove_pointer<T>::type TT;
230 static_assert( !std::is_floating_point<TT>::value ||
231 (std::is_floating_point<TT>::value && std::numeric_limits<TT>::is_iec559),
232 "Portable binary only supports IEEE 754 standardized floating point" );
234 ar.template loadBinary<sizeof(TT)>( bd.
data,
static_cast<std::size_t
>( bd.
size ) );
245 #endif // CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_ #define CEREAL_SETUP_ARCHIVE_TRAITS(InputArchive, OutputArchive)
Sets up traits that relate an input archive to an output archive.
Definition: traits.hpp:169
PT data
pointer to beginning of data
Definition: helpers.hpp:217
A wrapper around size metadata.
Definition: helpers.hpp:250
#define CEREAL_SERIALIZE_FUNCTION_NAME
The serialization/deserialization function name to search for.
Definition: macros.hpp:51
void saveBinary(const void *data, std::size_t size)
Writes size bytes of data to the output stream.
Definition: portable_binary.hpp:92
bool is_little_endian()
Returns true if the current machine is little endian.
Definition: portable_binary.hpp:42
Definition: access.hpp:39
#define CEREAL_REGISTER_ARCHIVE(Archive)
Registers a specific Archive type with cereal.
Definition: cereal.hpp:141
#define CEREAL_ARCHIVE_RESTRICT(INTYPE, OUTTYPE)
A macro to use to restrict which types of archives your function will work for.
Definition: traits.hpp:1260
Main cereal functionality.
For holding name value pairs.
Definition: helpers.hpp:135
#define CEREAL_LOAD_FUNCTION_NAME
The deserialization (load) function name to search for.
Definition: macros.hpp:58
void swap_bytes(std::uint8_t *data)
Swaps the order of bytes for some chunk of memory.
Definition: portable_binary.hpp:53
A wrapper around data that can be serialized in a binary fashion.
Definition: helpers.hpp:207
The base output archive class.
Definition: cereal.hpp:234
#define CEREAL_SAVE_FUNCTION_NAME
The serialization (save) function name to search for.
Definition: macros.hpp:65
uint64_t size
size in bytes
Definition: helpers.hpp:218
PortableBinaryOutputArchive(std::ostream &stream)
Construct, outputting to the provided stream.
Definition: portable_binary.hpp:84
An exception class thrown when things go wrong at runtime.
Definition: helpers.hpp:48
An output archive designed to save data in a compact binary representation portable over different ar...
Definition: portable_binary.hpp:78