michael@0: // michael@0: // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // BinaryStream.h: Provides binary serialization of simple types. michael@0: michael@0: #ifndef LIBGLESV2_BINARYSTREAM_H_ michael@0: #define LIBGLESV2_BINARYSTREAM_H_ michael@0: michael@0: #include "common/angleutils.h" michael@0: michael@0: namespace gl michael@0: { michael@0: michael@0: class BinaryInputStream michael@0: { michael@0: public: michael@0: BinaryInputStream(const void *data, size_t length) michael@0: { michael@0: mError = false; michael@0: mOffset = 0; michael@0: mData = static_cast(data); michael@0: mLength = length; michael@0: } michael@0: michael@0: template michael@0: void read(T *v, size_t num) michael@0: { michael@0: union michael@0: { michael@0: T dummy; // Compilation error for non-trivial types michael@0: } dummy; michael@0: (void) dummy; michael@0: michael@0: if (mError) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: size_t length = num * sizeof(T); michael@0: michael@0: if (mOffset + length > mLength) michael@0: { michael@0: mError = true; michael@0: return; michael@0: } michael@0: michael@0: memcpy(v, mData + mOffset, length); michael@0: mOffset += length; michael@0: } michael@0: michael@0: template michael@0: void read(T * v) michael@0: { michael@0: read(v, 1); michael@0: } michael@0: michael@0: void read(std::string *v) michael@0: { michael@0: size_t length; michael@0: read(&length); michael@0: michael@0: if (mError) michael@0: { michael@0: return; michael@0: } michael@0: michael@0: if (mOffset + length > mLength) michael@0: { michael@0: mError = true; michael@0: return; michael@0: } michael@0: michael@0: v->assign(mData + mOffset, length); michael@0: mOffset += length; michael@0: } michael@0: michael@0: void skip(size_t length) michael@0: { michael@0: if (mOffset + length > mLength) michael@0: { michael@0: mError = true; michael@0: return; michael@0: } michael@0: michael@0: mOffset += length; michael@0: } michael@0: michael@0: size_t offset() const michael@0: { michael@0: return mOffset; michael@0: } michael@0: michael@0: bool error() const michael@0: { michael@0: return mError; michael@0: } michael@0: michael@0: bool endOfStream() const michael@0: { michael@0: return mOffset == mLength; michael@0: } michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(BinaryInputStream); michael@0: bool mError; michael@0: size_t mOffset; michael@0: const char *mData; michael@0: size_t mLength; michael@0: }; michael@0: michael@0: class BinaryOutputStream michael@0: { michael@0: public: michael@0: BinaryOutputStream() michael@0: { michael@0: } michael@0: michael@0: template michael@0: void write(const T *v, size_t num) michael@0: { michael@0: union michael@0: { michael@0: T dummy; // Compilation error for non-trivial types michael@0: } dummy; michael@0: (void) dummy; michael@0: michael@0: const char *asBytes = reinterpret_cast(v); michael@0: mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T)); michael@0: } michael@0: michael@0: template michael@0: void write(const T &v) michael@0: { michael@0: write(&v, 1); michael@0: } michael@0: michael@0: void write(const std::string &v) michael@0: { michael@0: size_t length = v.length(); michael@0: write(length); michael@0: michael@0: write(v.c_str(), length); michael@0: } michael@0: michael@0: size_t length() const michael@0: { michael@0: return mData.size(); michael@0: } michael@0: michael@0: const void* data() const michael@0: { michael@0: return mData.size() ? &mData[0] : NULL; michael@0: } michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream); michael@0: std::vector mData; michael@0: }; michael@0: } michael@0: michael@0: #endif // LIBGLESV2_BINARYSTREAM_H_