git repos / blockattack-game

blame: source/code/Libs/include/cereal/archives/binary.hpp

normal view · raw

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