Home | History | Annotate | Download | only in libGLESv2
      1 //
      2 // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // BinaryStream.h: Provides binary serialization of simple types.
      8 
      9 #ifndef LIBGLESV2_BINARYSTREAM_H_
     10 #define LIBGLESV2_BINARYSTREAM_H_
     11 
     12 #include "common/angleutils.h"
     13 #include "common/mathutil.h"
     14 
     15 namespace gl
     16 {
     17 
     18 class BinaryInputStream
     19 {
     20   public:
     21     BinaryInputStream(const void *data, size_t length)
     22     {
     23         mError = false;
     24         mOffset = 0;
     25         mData = static_cast<const char*>(data);
     26         mLength = length;
     27     }
     28 
     29     // readInt will generate an error for bool types
     30     template <class IntT>
     31     IntT readInt()
     32     {
     33         int value;
     34         read(&value);
     35         return static_cast<IntT>(value);
     36     }
     37 
     38     template <class IntT>
     39     void readInt(IntT *outValue)
     40     {
     41         *outValue = readInt<IntT>();
     42     }
     43 
     44     bool readBool()
     45     {
     46         int value;
     47         read(&value);
     48         return (value > 0);
     49     }
     50 
     51     void readBool(bool *outValue)
     52     {
     53         *outValue = readBool();
     54     }
     55 
     56     void readBytes(unsigned char outArray[], size_t count)
     57     {
     58         read<unsigned char>(outArray, count);
     59     }
     60 
     61     std::string readString()
     62     {
     63         std::string outString;
     64         readString(&outString);
     65         return outString;
     66     }
     67 
     68     void readString(std::string *v)
     69     {
     70         size_t length;
     71         readInt(&length);
     72 
     73         if (mError)
     74         {
     75             return;
     76         }
     77 
     78         if (mOffset + length > mLength)
     79         {
     80             mError = true;
     81             return;
     82         }
     83 
     84         v->assign(mData + mOffset, length);
     85         mOffset += length;
     86     }
     87 
     88     void skip(size_t length)
     89     {
     90         if (mOffset + length > mLength)
     91         {
     92             mError = true;
     93             return;
     94         }
     95 
     96         mOffset += length;
     97     }
     98 
     99     size_t offset() const
    100     {
    101         return mOffset;
    102     }
    103 
    104     bool error() const
    105     {
    106         return mError;
    107     }
    108 
    109     bool endOfStream() const
    110     {
    111         return mOffset == mLength;
    112     }
    113 
    114   private:
    115     DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
    116     bool mError;
    117     size_t mOffset;
    118     const char *mData;
    119     size_t mLength;
    120 
    121     template <typename T>
    122     void read(T *v, size_t num)
    123     {
    124         META_ASSERT(std::is_fundamental<T>::value);
    125 
    126         size_t length = num * sizeof(T);
    127 
    128         if (mOffset + length > mLength)
    129         {
    130             mError = true;
    131             return;
    132         }
    133 
    134         memcpy(v, mData + mOffset, length);
    135         mOffset += length;
    136     }
    137 
    138     template <typename T>
    139     void read(T *v)
    140     {
    141         read(v, 1);
    142     }
    143 
    144 };
    145 
    146 class BinaryOutputStream
    147 {
    148   public:
    149     BinaryOutputStream()
    150     {
    151     }
    152 
    153     // writeInt also handles bool types
    154     template <class IntT>
    155     void writeInt(IntT param)
    156     {
    157         ASSERT(rx::IsIntegerCastSafe<int>(param));
    158         int intValue = static_cast<int>(param);
    159         write(&intValue, 1);
    160     }
    161 
    162     void writeString(const std::string &v)
    163     {
    164         writeInt(v.length());
    165         write(v.c_str(), v.length());
    166     }
    167 
    168     void writeBytes(unsigned char *bytes, size_t count)
    169     {
    170         write(bytes, count);
    171     }
    172 
    173     size_t length() const
    174     {
    175         return mData.size();
    176     }
    177 
    178     const void* data() const
    179     {
    180         return mData.size() ? &mData[0] : NULL;
    181     }
    182 
    183   private:
    184     DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
    185     std::vector<char> mData;
    186 
    187     template <typename T>
    188     void write(const T *v, size_t num)
    189     {
    190         META_ASSERT(std::is_fundamental<T>::value);
    191         const char *asBytes = reinterpret_cast<const char*>(v);
    192         mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
    193     }
    194 
    195 };
    196 }
    197 
    198 #endif  // LIBGLESV2_BINARYSTREAM_H_
    199