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