cereal
A C++11 library for serialization
portable_binary.hpp
1 
3 /*
4  Copyright (c) 2014, Randolph Voorhies, Shane Grant
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions are met:
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of cereal nor the
15  names of its contributors may be used to endorse or promote products
16  derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
22  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 #ifndef CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_
30 #define CEREAL_ARCHIVES_PORTABLE_BINARY_HPP_
31 
32 #include <cereal/cereal.hpp>
33 #include <sstream>
34 #include <limits>
35 
36 namespace cereal
37 {
38  namespace portable_binary_detail
39  {
41 
42  inline bool is_little_endian()
43  {
44  static std::int32_t test = 1;
45  return *reinterpret_cast<std::int8_t*>( &test ) == 1;
46  }
47 
49 
52  template <std::size_t DataSize>
53  inline void swap_bytes( std::uint8_t * data )
54  {
55  for( std::size_t i = 0, end = DataSize / 2; i < end; ++i )
56  std::swap( data[i], data[DataSize - i - 1] );
57  }
58  } // end namespace portable_binary_detail
59 
60  // ######################################################################
62 
78  class PortableBinaryOutputArchive : public OutputArchive<PortableBinaryOutputArchive, AllowEmptyClassElision>
79  {
80  public:
82 
84  PortableBinaryOutputArchive(std::ostream & stream) :
85  OutputArchive<PortableBinaryOutputArchive, AllowEmptyClassElision>(this),
86  itsStream(stream)
87  {
88  this->operator()( portable_binary_detail::is_little_endian() );
89  }
90 
92  void saveBinary( const void * data, std::size_t size )
93  {
94  auto const writtenSize = static_cast<std::size_t>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size ) );
95 
96  if(writtenSize != size)
97  throw Exception("Failed to write " + std::to_string(size) + " bytes to output stream! Wrote " + std::to_string(writtenSize));
98  }
99 
100  private:
101  std::ostream & itsStream;
102  };
103 
104  // ######################################################################
106 
130  class PortableBinaryInputArchive : public InputArchive<PortableBinaryInputArchive, AllowEmptyClassElision>
131  {
132  public:
134 
135  PortableBinaryInputArchive(std::istream & stream) :
136  InputArchive<PortableBinaryInputArchive, AllowEmptyClassElision>(this),
137  itsStream(stream),
138  itsConvertEndianness( false )
139  {
140  bool streamLittleEndian;
141  this->operator()( streamLittleEndian );
142  itsConvertEndianness = portable_binary_detail::is_little_endian() ^ streamLittleEndian;
143  }
144 
146 
149  template <std::size_t DataSize>
150  void loadBinary( void * const data, std::size_t size )
151  {
152  // load data
153  auto const readSize = static_cast<std::size_t>( itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size ) );
154 
155  if(readSize != size)
156  throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
157 
158  // flip bits if needed
159  if( itsConvertEndianness )
160  {
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 );
164  }
165  }
166 
167  private:
168  std::istream & itsStream;
169  bool itsConvertEndianness;
170  };
171 
172  // ######################################################################
173  // Common BinaryArchive serialization functions
174 
176  template<class T> inline
177  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
179  {
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" );
183  ar.saveBinary(std::addressof(t), sizeof(t));
184  }
185 
187  template<class T> inline
188  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
190  {
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));
195  }
196 
198  template <class Archive, class T> inline
201  {
202  ar( t.value );
203  }
204 
206  template <class Archive, class T> inline
208  CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, SizeTag<T> & t )
209  {
210  ar( t.size );
211  }
212 
214  template <class T> inline
216  {
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" );
221 
222  ar.saveBinary( bd.data, static_cast<std::size_t>( bd.size ) );
223  }
224 
226  template <class T> inline
228  {
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" );
233 
234  ar.template loadBinary<sizeof(TT)>( bd.data, static_cast<std::size_t>( bd.size ) );
235  }
236 } // namespace cereal
237 
238 // register archives for polymorphic support
241 
242 // tie input and output archives together
244 
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
The base input archive class.
Definition: cereal.hpp:584
PortableBinaryInputArchive(std::istream &stream)
Construct, loading from the provided stream.
Definition: portable_binary.hpp:135
#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
An input archive designed to load data saved using PortableBinaryOutputArchive.
Definition: portable_binary.hpp:130
#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
void loadBinary(void *const data, std::size_t size)
Reads size bytes of data from the input stream.
Definition: portable_binary.hpp:150
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