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 
     14 namespace gl
     15 {
     16 
     17 class BinaryInputStream
     18 {
     19   public:
     20     BinaryInputStream(const void *data, size_t length)
     21     {
     22         mError = false;
     23         mOffset = 0;
     24         mData = static_cast<const char*>(data);
     25         mLength = length;
     26     }
     27 
     28     template <typename T>
     29     void read(T *v, size_t num)
     30     {
     31         union
     32         {
     33             T dummy;  // Compilation error for non-trivial types
     34         } dummy;
     35         (void) dummy;
     36 
     37         if (mError)
     38         {
     39             return;
     40         }
     41 
     42         size_t length = num * sizeof(T);
     43 
     44         if (mOffset + length > mLength)
     45         {
     46             mError = true;
     47             return;
     48         }
     49 
     50         memcpy(v, mData + mOffset, length);
     51         mOffset += length;
     52     }
     53 
     54     template <typename T>
     55     void read(T * v)
     56     {
     57         read(v, 1);
     58     }
     59 
     60     void read(std::string *v)
     61     {
     62         size_t length;
     63         read(&length);
     64 
     65         if (mError)
     66         {
     67             return;
     68         }
     69 
     70         if (mOffset + length > mLength)
     71         {
     72             mError = true;
     73             return;
     74         }
     75 
     76         v->assign(mData + mOffset, length);
     77         mOffset += length;
     78     }
     79 
     80     void skip(size_t length)
     81     {
     82         if (mOffset + length > mLength)
     83         {
     84             mError = true;
     85             return;
     86         }
     87 
     88         mOffset += length;
     89     }
     90 
     91     size_t offset() const
     92     {
     93         return mOffset;
     94     }
     95 
     96     bool error() const
     97     {
     98         return mError;
     99     }
    100 
    101     bool endOfStream() const
    102     {
    103         return mOffset == mLength;
    104     }
    105 
    106   private:
    107     DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
    108     bool mError;
    109     size_t mOffset;
    110     const char *mData;
    111     size_t mLength;
    112 };
    113 
    114 class BinaryOutputStream
    115 {
    116   public:
    117     BinaryOutputStream()
    118     {
    119     }
    120 
    121     template <typename T>
    122     void write(const T *v, size_t num)
    123     {
    124         union
    125         {
    126             T dummy;  // Compilation error for non-trivial types
    127         } dummy;
    128         (void) dummy;
    129 
    130         const char *asBytes = reinterpret_cast<const char*>(v);
    131         mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
    132     }
    133 
    134     template <typename T>
    135     void write(const T &v)
    136     {
    137         write(&v, 1);
    138     }
    139 
    140     void write(const std::string &v)
    141     {
    142         size_t length = v.length();
    143         write(length);
    144 
    145         write(v.c_str(), length);
    146     }
    147 
    148     size_t length() const
    149     {
    150         return mData.size();
    151     }
    152 
    153     const void* data() const
    154     {
    155         return mData.size() ? &mData[0] : NULL;
    156     }
    157 
    158   private:
    159     DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
    160     std::vector<char> mData;
    161 };
    162 }
    163 
    164 #endif  // LIBGLESV2_BINARYSTREAM_H_
    165