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.
7 // BinaryStream.h: Provides binary serialization of simple types.
9 #ifndef LIBGLESV2_BINARYSTREAM_H_
10 #define LIBGLESV2_BINARYSTREAM_H_
12 #include "common/angleutils.h"
13 #include "common/mathutil.h"
23 class BinaryInputStream
26 BinaryInputStream(const void *data, size_t length)
30 mData = static_cast<const uint8_t*>(data);
34 // readInt will generate an error for bool types
40 return static_cast<IntT>(value);
44 void readInt(IntT *outValue)
46 *outValue = readInt<IntT>();
56 void readBool(bool *outValue)
58 *outValue = readBool();
61 void readBytes(unsigned char outArray[], size_t count)
63 read<unsigned char>(outArray, count);
66 std::string readString()
68 std::string outString;
69 readString(&outString);
73 void readString(std::string *v)
83 if (mOffset + length > mLength)
89 v->assign(reinterpret_cast<const char *>(mData) + mOffset, length);
93 void skip(size_t length)
95 if (mOffset + length > mLength)
104 size_t offset() const
114 bool endOfStream() const
116 return mOffset == mLength;
119 const uint8_t *data()
125 DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
128 const uint8_t *mData;
131 template <typename T>
132 void read(T *v, size_t num)
134 META_ASSERT(std::is_fundamental<T>::value);
136 size_t length = num * sizeof(T);
138 if (mOffset + length > mLength)
144 memcpy(v, mData + mOffset, length);
148 template <typename T>
156 class BinaryOutputStream
163 // writeInt also handles bool types
164 template <class IntT>
165 void writeInt(IntT param)
167 ASSERT(rx::IsIntegerCastSafe<int>(param));
168 int intValue = static_cast<int>(param);
172 void writeString(const std::string &v)
174 writeInt(v.length());
175 write(v.c_str(), v.length());
178 void writeBytes(const unsigned char *bytes, size_t count)
183 size_t length() const
188 const void* data() const
190 return mData.size() ? &mData[0] : NULL;
194 DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
195 std::vector<char> mData;
197 template <typename T>
198 void write(const T *v, size_t num)
200 META_ASSERT(std::is_fundamental<T>::value);
201 const char *asBytes = reinterpret_cast<const char*>(v);
202 mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
208 #endif // LIBGLESV2_BINARYSTREAM_H_