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
7a956470 sago007 2016-02-14 17:09 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
7a956470 sago007 2016-02-14 17:09 62
      //! Writes size bytes of data to the output stream
7a956470 sago007 2016-02-14 17:09 63
      void saveBinary( const void * data, std::size_t size )
7a956470 sago007 2016-02-14 17:09 64
      {
7a956470 sago007 2016-02-14 17:09 65
        auto const writtenSize = static_cast<std::size_t>( itsStream.rdbuf()->sputn( reinterpret_cast<const char*>( data ), size ) );
7a956470 sago007 2016-02-14 17:09 66
7a956470 sago007 2016-02-14 17:09 67
        if(writtenSize != size)
7a956470 sago007 2016-02-14 17:09 68
          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 69
      }
7a956470 sago007 2016-02-14 17:09 70
7a956470 sago007 2016-02-14 17:09 71
    private:
7a956470 sago007 2016-02-14 17:09 72
      std::ostream & itsStream;
7a956470 sago007 2016-02-14 17:09 73
  };
7a956470 sago007 2016-02-14 17:09 74
7a956470 sago007 2016-02-14 17:09 75
  // ######################################################################
7a956470 sago007 2016-02-14 17:09 76
  //! An input archive designed to load data saved using BinaryOutputArchive
7a956470 sago007 2016-02-14 17:09 77
  /*  This archive does nothing to ensure that the endianness of the saved
7a956470 sago007 2016-02-14 17:09 78
      and loaded data is the same.  If you need to have portability over
7a956470 sago007 2016-02-14 17:09 79
      architectures with different endianness, use PortableBinaryOutputArchive.
7a956470 sago007 2016-02-14 17:09 80
7a956470 sago007 2016-02-14 17:09 81
      When using a binary archive and a file stream, you must use the
7a956470 sago007 2016-02-14 17:09 82
      std::ios::binary format flag to avoid having your data altered
7a956470 sago007 2016-02-14 17:09 83
      inadvertently.
7a956470 sago007 2016-02-14 17:09 84
7a956470 sago007 2016-02-14 17:09 85
      \ingroup Archives */
7a956470 sago007 2016-02-14 17:09 86
  class BinaryInputArchive : public InputArchive<BinaryInputArchive, AllowEmptyClassElision>
7a956470 sago007 2016-02-14 17:09 87
  {
7a956470 sago007 2016-02-14 17:09 88
    public:
7a956470 sago007 2016-02-14 17:09 89
      //! Construct, loading from the provided stream
7a956470 sago007 2016-02-14 17:09 90
      BinaryInputArchive(std::istream & stream) :
7a956470 sago007 2016-02-14 17:09 91
        InputArchive<BinaryInputArchive, AllowEmptyClassElision>(this),
7a956470 sago007 2016-02-14 17:09 92
        itsStream(stream)
7a956470 sago007 2016-02-14 17:09 93
    { }
7a956470 sago007 2016-02-14 17:09 94
7a956470 sago007 2016-02-14 17:09 95
      //! Reads size bytes of data from the input stream
7a956470 sago007 2016-02-14 17:09 96
      void loadBinary( void * const data, std::size_t size )
7a956470 sago007 2016-02-14 17:09 97
      {
7a956470 sago007 2016-02-14 17:09 98
        auto const readSize = static_cast<std::size_t>( itsStream.rdbuf()->sgetn( reinterpret_cast<char*>( data ), size ) );
7a956470 sago007 2016-02-14 17:09 99
7a956470 sago007 2016-02-14 17:09 100
        if(readSize != size)
7a956470 sago007 2016-02-14 17:09 101
          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 102
      }
7a956470 sago007 2016-02-14 17:09 103
7a956470 sago007 2016-02-14 17:09 104
    private:
7a956470 sago007 2016-02-14 17:09 105
      std::istream & itsStream;
7a956470 sago007 2016-02-14 17:09 106
  };
7a956470 sago007 2016-02-14 17:09 107
7a956470 sago007 2016-02-14 17:09 108
  // ######################################################################
7a956470 sago007 2016-02-14 17:09 109
  // Common BinaryArchive serialization functions
7a956470 sago007 2016-02-14 17:09 110
7a956470 sago007 2016-02-14 17:09 111
  //! Saving for POD types to binary
7a956470 sago007 2016-02-14 17:09 112
  template<class T> inline
7a956470 sago007 2016-02-14 17:09 113
  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
7a956470 sago007 2016-02-14 17:09 114
  CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, T const & t)
7a956470 sago007 2016-02-14 17:09 115
  {
7a956470 sago007 2016-02-14 17:09 116
    ar.saveBinary(std::addressof(t), sizeof(t));
7a956470 sago007 2016-02-14 17:09 117
  }
7a956470 sago007 2016-02-14 17:09 118
7a956470 sago007 2016-02-14 17:09 119
  //! Loading for POD types from binary
7a956470 sago007 2016-02-14 17:09 120
  template<class T> inline
7a956470 sago007 2016-02-14 17:09 121
  typename std::enable_if<std::is_arithmetic<T>::value, void>::type
7a956470 sago007 2016-02-14 17:09 122
  CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, T & t)
7a956470 sago007 2016-02-14 17:09 123
  {
7a956470 sago007 2016-02-14 17:09 124
    ar.loadBinary(std::addressof(t), sizeof(t));
7a956470 sago007 2016-02-14 17:09 125
  }
7a956470 sago007 2016-02-14 17:09 126
7a956470 sago007 2016-02-14 17:09 127
  //! Serializing NVP types to binary
7a956470 sago007 2016-02-14 17:09 128
  template <class Archive, class T> inline
7a956470 sago007 2016-02-14 17:09 129
  CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
7a956470 sago007 2016-02-14 17:09 130
  CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, NameValuePair<T> & t )
7a956470 sago007 2016-02-14 17:09 131
  {
7a956470 sago007 2016-02-14 17:09 132
    ar( t.value );
7a956470 sago007 2016-02-14 17:09 133
  }
7a956470 sago007 2016-02-14 17:09 134
7a956470 sago007 2016-02-14 17:09 135
  //! Serializing SizeTags to binary
7a956470 sago007 2016-02-14 17:09 136
  template <class Archive, class T> inline
7a956470 sago007 2016-02-14 17:09 137
  CEREAL_ARCHIVE_RESTRICT(BinaryInputArchive, BinaryOutputArchive)
7a956470 sago007 2016-02-14 17:09 138
  CEREAL_SERIALIZE_FUNCTION_NAME( Archive & ar, SizeTag<T> & t )
7a956470 sago007 2016-02-14 17:09 139
  {
7a956470 sago007 2016-02-14 17:09 140
    ar( t.size );
7a956470 sago007 2016-02-14 17:09 141
  }
7a956470 sago007 2016-02-14 17:09 142
7a956470 sago007 2016-02-14 17:09 143
  //! Saving binary data
7a956470 sago007 2016-02-14 17:09 144
  template <class T> inline
7a956470 sago007 2016-02-14 17:09 145
  void CEREAL_SAVE_FUNCTION_NAME(BinaryOutputArchive & ar, BinaryData<T> const & bd)
7a956470 sago007 2016-02-14 17:09 146
  {
7a956470 sago007 2016-02-14 17:09 147
    ar.saveBinary( bd.data, static_cast<std::size_t>( bd.size ) );
7a956470 sago007 2016-02-14 17:09 148
  }
7a956470 sago007 2016-02-14 17:09 149
7a956470 sago007 2016-02-14 17:09 150
  //! Loading binary data
7a956470 sago007 2016-02-14 17:09 151
  template <class T> inline
7a956470 sago007 2016-02-14 17:09 152
  void CEREAL_LOAD_FUNCTION_NAME(BinaryInputArchive & ar, BinaryData<T> & bd)
7a956470 sago007 2016-02-14 17:09 153
  {
7a956470 sago007 2016-02-14 17:09 154
    ar.loadBinary(bd.data, static_cast<std::size_t>(bd.size));
7a956470 sago007 2016-02-14 17:09 155
  }
7a956470 sago007 2016-02-14 17:09 156
} // namespace cereal
7a956470 sago007 2016-02-14 17:09 157
7a956470 sago007 2016-02-14 17:09 158
// register archives for polymorphic support
7a956470 sago007 2016-02-14 17:09 159
CEREAL_REGISTER_ARCHIVE(cereal::BinaryOutputArchive)
7a956470 sago007 2016-02-14 17:09 160
CEREAL_REGISTER_ARCHIVE(cereal::BinaryInputArchive)
7a956470 sago007 2016-02-14 17:09 161
7a956470 sago007 2016-02-14 17:09 162
// tie input and output archives together
7a956470 sago007 2016-02-14 17:09 163
CEREAL_SETUP_ARCHIVE_TRAITS(cereal::BinaryInputArchive, cereal::BinaryOutputArchive)
7a956470 sago007 2016-02-14 17:09 164
7a956470 sago007 2016-02-14 17:09 165
#endif // CEREAL_ARCHIVES_BINARY_HPP_
1970-01-01 00:00 166