1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/libGLESv2/BinaryStream.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,164 @@ 1.4 +// 1.5 +// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. 1.6 +// Use of this source code is governed by a BSD-style license that can be 1.7 +// found in the LICENSE file. 1.8 +// 1.9 + 1.10 +// BinaryStream.h: Provides binary serialization of simple types. 1.11 + 1.12 +#ifndef LIBGLESV2_BINARYSTREAM_H_ 1.13 +#define LIBGLESV2_BINARYSTREAM_H_ 1.14 + 1.15 +#include "common/angleutils.h" 1.16 + 1.17 +namespace gl 1.18 +{ 1.19 + 1.20 +class BinaryInputStream 1.21 +{ 1.22 + public: 1.23 + BinaryInputStream(const void *data, size_t length) 1.24 + { 1.25 + mError = false; 1.26 + mOffset = 0; 1.27 + mData = static_cast<const char*>(data); 1.28 + mLength = length; 1.29 + } 1.30 + 1.31 + template <typename T> 1.32 + void read(T *v, size_t num) 1.33 + { 1.34 + union 1.35 + { 1.36 + T dummy; // Compilation error for non-trivial types 1.37 + } dummy; 1.38 + (void) dummy; 1.39 + 1.40 + if (mError) 1.41 + { 1.42 + return; 1.43 + } 1.44 + 1.45 + size_t length = num * sizeof(T); 1.46 + 1.47 + if (mOffset + length > mLength) 1.48 + { 1.49 + mError = true; 1.50 + return; 1.51 + } 1.52 + 1.53 + memcpy(v, mData + mOffset, length); 1.54 + mOffset += length; 1.55 + } 1.56 + 1.57 + template <typename T> 1.58 + void read(T * v) 1.59 + { 1.60 + read(v, 1); 1.61 + } 1.62 + 1.63 + void read(std::string *v) 1.64 + { 1.65 + size_t length; 1.66 + read(&length); 1.67 + 1.68 + if (mError) 1.69 + { 1.70 + return; 1.71 + } 1.72 + 1.73 + if (mOffset + length > mLength) 1.74 + { 1.75 + mError = true; 1.76 + return; 1.77 + } 1.78 + 1.79 + v->assign(mData + mOffset, length); 1.80 + mOffset += length; 1.81 + } 1.82 + 1.83 + void skip(size_t length) 1.84 + { 1.85 + if (mOffset + length > mLength) 1.86 + { 1.87 + mError = true; 1.88 + return; 1.89 + } 1.90 + 1.91 + mOffset += length; 1.92 + } 1.93 + 1.94 + size_t offset() const 1.95 + { 1.96 + return mOffset; 1.97 + } 1.98 + 1.99 + bool error() const 1.100 + { 1.101 + return mError; 1.102 + } 1.103 + 1.104 + bool endOfStream() const 1.105 + { 1.106 + return mOffset == mLength; 1.107 + } 1.108 + 1.109 + private: 1.110 + DISALLOW_COPY_AND_ASSIGN(BinaryInputStream); 1.111 + bool mError; 1.112 + size_t mOffset; 1.113 + const char *mData; 1.114 + size_t mLength; 1.115 +}; 1.116 + 1.117 +class BinaryOutputStream 1.118 +{ 1.119 + public: 1.120 + BinaryOutputStream() 1.121 + { 1.122 + } 1.123 + 1.124 + template <typename T> 1.125 + void write(const T *v, size_t num) 1.126 + { 1.127 + union 1.128 + { 1.129 + T dummy; // Compilation error for non-trivial types 1.130 + } dummy; 1.131 + (void) dummy; 1.132 + 1.133 + const char *asBytes = reinterpret_cast<const char*>(v); 1.134 + mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T)); 1.135 + } 1.136 + 1.137 + template <typename T> 1.138 + void write(const T &v) 1.139 + { 1.140 + write(&v, 1); 1.141 + } 1.142 + 1.143 + void write(const std::string &v) 1.144 + { 1.145 + size_t length = v.length(); 1.146 + write(length); 1.147 + 1.148 + write(v.c_str(), length); 1.149 + } 1.150 + 1.151 + size_t length() const 1.152 + { 1.153 + return mData.size(); 1.154 + } 1.155 + 1.156 + const void* data() const 1.157 + { 1.158 + return mData.size() ? &mData[0] : NULL; 1.159 + } 1.160 + 1.161 + private: 1.162 + DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream); 1.163 + std::vector<char> mData; 1.164 +}; 1.165 +} 1.166 + 1.167 +#endif // LIBGLESV2_BINARYSTREAM_H_