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"
18 class BinaryInputStream
21 BinaryInputStream(const void *data, size_t length)
25 mData = static_cast<const char*>(data);
29 // readInt will generate an error for bool types
35 return static_cast<IntT>(value);
39 void readInt(IntT *outValue)
41 *outValue = readInt<IntT>();
51 void readBool(bool *outValue)
53 *outValue = readBool();
56 void readBytes(unsigned char outArray[], size_t count)
58 read<unsigned char>(outArray, count);
61 std::string readString()
63 std::string outString;
64 readString(&outString);
68 void readString(std::string *v)
78 if (mOffset + length > mLength)
84 v->assign(mData + mOffset, length);
88 void skip(size_t length)
90 if (mOffset + length > mLength)
109 bool endOfStream() const
111 return mOffset == mLength;
115 DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
121 template <typename T>
122 void read(T *v, size_t num)
124 META_ASSERT(std::is_fundamental<T>::value);
126 size_t length = num * sizeof(T);
128 if (mOffset + length > mLength)
134 memcpy(v, mData + mOffset, length);
138 template <typename T>
146 class BinaryOutputStream
153 // writeInt also handles bool types
154 template <class IntT>
155 void writeInt(IntT param)
157 ASSERT(rx::IsIntegerCastSafe<int>(param));
158 int intValue = static_cast<int>(param);
162 void writeString(const std::string &v)
164 writeInt(v.length());
165 write(v.c_str(), v.length());
168 void writeBytes(const unsigned char *bytes, size_t count)
173 size_t length() const
178 const void* data() const
180 return mData.size() ? &mData[0] : NULL;
184 DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
185 std::vector<char> mData;
187 template <typename T>
188 void write(const T *v, size_t num)
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));
198 #endif // LIBGLESV2_BINARYSTREAM_H_