"Fossies" - the Fresh Open Source Software Archive

Member "stella-6.0.2/src/emucore/Serializer.hxx" (11 Oct 2019, 6682 Bytes) of package /linux/privat/stella-6.0.2-src.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Serializer.hxx" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 6.0.1_vs_6.0.2.

    1 //============================================================================
    2 //
    3 //   SSSS    tt          lll  lll
    4 //  SS  SS   tt           ll   ll
    5 //  SS     tttttt  eeee   ll   ll   aaaa
    6 //   SSSS    tt   ee  ee  ll   ll      aa
    7 //      SS   tt   eeeeee  ll   ll   aaaaa  --  "An Atari 2600 VCS Emulator"
    8 //  SS  SS   tt   ee      ll   ll  aa  aa
    9 //   SSSS     ttt  eeeee llll llll  aaaaa
   10 //
   11 // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
   12 // and the Stella Team
   13 //
   14 // See the file "License.txt" for information on usage and redistribution of
   15 // this file, and for a DISCLAIMER OF ALL WARRANTIES.
   16 //============================================================================
   17 
   18 #ifndef SERIALIZER_HXX
   19 #define SERIALIZER_HXX
   20 
   21 #include "bspf.hxx"
   22 
   23 /**
   24   This class implements a Serializer device, whereby data is serialized and
   25   read from/written to a binary stream in a system-independent way.  The
   26   stream can be either an actual file, or an in-memory structure.
   27 
   28   Bytes are written as characters, shorts as 2 characters (16-bits),
   29   integers as 4 characters (32-bits), long integers as 8 bytes (64-bits),
   30   strings are written as characters prepended by the length of the string,
   31   boolean values are written using a special character pattern.
   32 
   33   @author  Stephen Anthony
   34 */
   35 class Serializer
   36 {
   37   public:
   38     /**
   39       Creates a new Serializer device for streaming binary data.
   40 
   41       If a filename is provided, the stream will be to the given
   42       filename.  Otherwise, the stream will be in memory.
   43 
   44       If a file is opened readonly, we can never write to it.
   45 
   46       The valid() method must immediately be called to verify the stream
   47       was correctly initialized.
   48     */
   49     Serializer(const string& filename, bool readonly = false);
   50     Serializer();
   51 
   52   public:
   53     /**
   54       Answers whether the serializer is currently initialized for reading
   55       and writing.
   56     */
   57     explicit operator bool() const { return myStream != nullptr; }
   58 
   59     /**
   60       Resets the read/write location to the beginning of the stream.
   61     */
   62     void rewind();
   63 
   64     /**
   65       Reads a byte value (unsigned 8-bit) from the current input stream.
   66 
   67       @result The byte value which has been read from the stream.
   68     */
   69     uInt8 getByte() const;
   70 
   71     /**
   72       Reads a byte array (unsigned 8-bit) from the current input stream.
   73 
   74       @param array  The location to store the bytes read
   75       @param size   The size of the array (number of bytes to read)
   76     */
   77     void getByteArray(uInt8* array, uInt32 size) const;
   78 
   79     /**
   80       Reads a short value (unsigned 16-bit) from the current input stream.
   81 
   82       @result The short value which has been read from the stream.
   83     */
   84     uInt16 getShort() const;
   85 
   86     /**
   87       Reads a short array (unsigned 16-bit) from the current input stream.
   88 
   89       @param array  The location to store the shorts read
   90       @param size   The size of the array (number of shorts to read)
   91     */
   92     void getShortArray(uInt16* array, uInt32 size) const;
   93 
   94     /**
   95       Reads an int value (unsigned 32-bit) from the current input stream.
   96 
   97       @result The int value which has been read from the stream.
   98     */
   99     uInt32 getInt() const;
  100 
  101     /**
  102       Reads an integer array (unsigned 32-bit) from the current input stream.
  103 
  104       @param array  The location to store the integers read
  105       @param size   The size of the array (number of integers to read)
  106     */
  107     void getIntArray(uInt32* array, uInt32 size) const;
  108 
  109     /**
  110       Reads a long int value (unsigned 64-bit) from the current input stream.
  111 
  112       @result The long int value which has been read from the stream.
  113     */
  114     uInt64 getLong() const;
  115 
  116     /**
  117       Reads a double value (signed 64-bit) from the current input stream.
  118 
  119       @result The double value which has been read from the stream.
  120     */
  121     double getDouble() const;
  122 
  123     /**
  124       Reads a string from the current input stream.
  125 
  126       @result The string which has been read from the stream.
  127     */
  128     string getString() const;
  129 
  130     /**
  131       Reads a boolean value from the current input stream.
  132 
  133       @result The boolean value which has been read from the stream.
  134     */
  135     bool getBool() const;
  136 
  137     /**
  138       Writes an byte value (unsigned 8-bit) to the current output stream.
  139 
  140       @param value The byte value to write to the output stream.
  141     */
  142     void putByte(uInt8 value);
  143 
  144     /**
  145       Writes a byte array (unsigned 8-bit) to the current output stream.
  146 
  147       @param array  The bytes to write
  148       @param size   The size of the array (number of bytes to write)
  149     */
  150     void putByteArray(const uInt8* array, uInt32 size);
  151 
  152     /**
  153       Writes a short value (unsigned 16-bit) to the current output stream.
  154 
  155       @param value The short value to write to the output stream.
  156     */
  157     void putShort(uInt16 value);
  158 
  159     /**
  160       Writes a short array (unsigned 16-bit) to the current output stream.
  161 
  162       @param array  The short to write
  163       @param size   The size of the array (number of shorts to write)
  164     */
  165     void putShortArray(const uInt16* array, uInt32 size);
  166 
  167     /**
  168       Writes an int value (unsigned 32-bit) to the current output stream.
  169 
  170       @param value The int value to write to the output stream.
  171     */
  172     void putInt(uInt32 value);
  173 
  174     /**
  175       Writes an integer array (unsigned 32-bit) to the current output stream.
  176 
  177       @param array  The integers to write
  178       @param size   The size of the array (number of integers to write)
  179     */
  180     void putIntArray(const uInt32* array, uInt32 size);
  181 
  182     /**
  183       Writes a long int value (unsigned 64-bit) to the current output stream.
  184 
  185       @param value The long int value to write to the output stream.
  186     */
  187     void putLong(uInt64 value);
  188 
  189     /**
  190       Writes a double value (signed 64-bit) to the current output stream.
  191 
  192       @param value The double value to write to the output stream.
  193     */
  194     void putDouble(double value);
  195 
  196     /**
  197       Writes a string to the current output stream.
  198 
  199       @param str The string to write to the output stream.
  200     */
  201     void putString(const string& str);
  202 
  203     /**
  204       Writes a boolean value to the current output stream.
  205 
  206       @param b The boolean value to write to the output stream.
  207     */
  208     void putBool(bool b);
  209 
  210   private:
  211     // The stream to send the serialized data to.
  212     unique_ptr<iostream> myStream;
  213 
  214     enum {
  215       TruePattern  = 0xfe,
  216       FalsePattern = 0x01
  217     };
  218 
  219   private:
  220     // Following constructors and assignment operators not supported
  221     Serializer(const Serializer&) = delete;
  222     Serializer(Serializer&&) = delete;
  223     Serializer& operator=(const Serializer&) = delete;
  224     Serializer& operator=(Serializer&&) = delete;
  225 };
  226 
  227 #endif