binary.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*! \file binary.hpp
  2. \brief Binary input and output archives */
  3. /*
  4. Copyright (c) 2014, Randolph Voorhies, Shane Grant
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the copyright holder nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef CEREAL_ARCHIVES_BINARY_HPP_
  28. #define CEREAL_ARCHIVES_BINARY_HPP_
  29. #include "cereal/cereal.hpp"
  30. #include <sstream>
  31. namespace cereal
  32. {
  33. // ######################################################################
  34. //! An output archive designed to save data in a compact binary representation
  35. /*! This archive outputs data to a stream in an extremely compact binary
  36. representation with as little extra metadata as possible.
  37. This archive does nothing to ensure that the endianness of the saved
  38. and loaded data is the same. If you need to have portability over
  39. architectures with different endianness, use PortableBinaryOutputArchive.
  40. When using a binary archive and a file stream, you must use the
  41. std::ios::binary format flag to avoid having your data altered
  42. inadvertently.
  43. \ingroup Archives */
  44. class BinaryOutputArchive : public OutputArchive<BinaryOutputArchive, AllowEmptyClassElision>
  45. {
  46. public:
  47. //! Construct, outputting to the provided stream
  48. /*! @param stream The stream to output to. Can be a stringstream, a file stream, or
  49. even cout! */
  50. BinaryOutputArchive(std::ostream & stream) :
  51. OutputArchive<BinaryOutputArchive, AllowEmptyClassElision>(this),
  52. itsStream(stream)
  53. { }
  54. ~BinaryOutputArchive() CEREAL_NOEXCEPT = default;
  55. //! Writes size bytes of data to the output stream
  56. void saveBinary( const void * data, std::streamsize size )
  57. {
  58. auto const writtenSize = itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size );
  59. if(writtenSize != size)
  60. throw Exception("Failed to write " + std::to_string(size) + " bytes to output stream! Wrote " + std::to_string(writtenSize));
  61. }
  62. private:
  63. std::ostream & itsStream;
  64. };
  65. // ######################################################################
  66. //! An input archive designed to load data saved using BinaryOutputArchive
  67. /* This archive does nothing to ensure that the endianness of the saved
  68. and loaded data is the same. If you need to have portability over
  69. architectures with different endianness, use PortableBinaryOutputArchive.
  70. When using a binary archive and a file stream, you must use the
  71. std::ios::binary format flag to avoid having your data altered
  72. inadvertently.
  73. \ingroup Archives */
  74. class BinaryInputArchive : public InputArchive<BinaryInputArchive, AllowEmptyClassElision>
  75. {
  76. public:
  77. //! Construct, loading from the provided stream
  78. BinaryInputArchive(std::istream & stream) :
  79. InputArchive<BinaryInputArchive, AllowEmptyClassElision>(this),
  80. itsStream(stream)
  81. { }
  82. ~BinaryInputArchive() CEREAL_NOEXCEPT = default;
  83. //! Reads size bytes of data from the input stream
  84. void loadBinary( void * const data, std::streamsize size )
  85. {
  86. auto const readSize = itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size );
  87. if(readSize != size)
  88. throw Exception("Failed to read " + std::to_string(size) + " bytes from input stream! Read " + std::to_string(readSize));
  89. }
  90. private:
  91. std::istream & itsStream;
  92. };
  93. // ######################################################################
  94. // Common BinaryArchive serialization functions
  95. //! Saving for POD types to binary
  96. template<class T> inline
  97. typename std::enable_if<std::is_arithmetic<T>::value, void>::type
  98. CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, T const & t)
  99. {
  100. ar.saveBinary(std::addressof(t), sizeof(t));
  101. }
  102. //! Loading for POD types from binary
  103. template<class T> inline
  104. typename std::enable_if<std::is_arithmetic<T>::value, void>::type
  105. CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, T & t)
  106. {
  107. ar.loadBinary(std::addressof(t), sizeof(t));
  108. }
  109. //! Serializing NVP types to binary
  110. template <class Archive, class T> inline
  111. CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
  112. CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, NameValuePair<T> & t )
  113. {
  114. ar( t.value );
  115. }
  116. //! Serializing SizeTags to binary
  117. template <class Archive, class T> inline
  118. CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
  119. CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, SizeTag<T> & t )
  120. {
  121. ar( t.size );
  122. }
  123. //! Saving binary data
  124. template <class T> inline
  125. void CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, BinaryData<T> const & bd)
  126. {
  127. ar.saveBinary( bd.data, static_cast<std::streamsize>( bd.size ) );
  128. }
  129. //! Loading binary data
  130. template <class T> inline
  131. void CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, BinaryData<T> & bd)
  132. {
  133. ar.loadBinary(bd.data, static_cast<std::streamsize>( bd.size ) );
  134. }
  135. } // namespace cereal
  136. // register archives for polymorphic support
  137. CEREAL_REGISTER_ARCHIVE(cereal::BinaryOutputArchive)
  138. CEREAL_REGISTER_ARCHIVE(cereal::BinaryInputArchive)
  139. // tie input and output archives together
  140. CEREAL_SETUP_ARCHIVE_TRAITS(cereal::BinaryInputArchive, cereal::BinaryOutputArchive)
  141. #endif // CEREAL_ARCHIVES_BINARY_HPP_