Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / BinaryStream.h
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 "common/angleutils.h"
13 #include "common/mathutil.h"
14
15 #include <cstddef>
16 #include <string>
17 #include <vector>
18 #include <stdint.h>
19
20 namespace gl
21 {
22
23 class BinaryInputStream
24 {
25   public:
26     BinaryInputStream(const void *data, size_t length)
27     {
28         mError = false;
29         mOffset = 0;
30         mData = static_cast<const uint8_t*>(data);
31         mLength = length;
32     }
33
34     // readInt will generate an error for bool types
35     template <class IntT>
36     IntT readInt()
37     {
38         int value;
39         read(&value);
40         return static_cast<IntT>(value);
41     }
42
43     template <class IntT>
44     void readInt(IntT *outValue)
45     {
46         *outValue = readInt<IntT>();
47     }
48
49     bool readBool()
50     {
51         int value;
52         read(&value);
53         return (value > 0);
54     }
55
56     void readBool(bool *outValue)
57     {
58         *outValue = readBool();
59     }
60
61     void readBytes(unsigned char outArray[], size_t count)
62     {
63         read<unsigned char>(outArray, count);
64     }
65
66     std::string readString()
67     {
68         std::string outString;
69         readString(&outString);
70         return outString;
71     }
72
73     void readString(std::string *v)
74     {
75         size_t length;
76         readInt(&length);
77
78         if (mError)
79         {
80             return;
81         }
82
83         if (mOffset + length > mLength)
84         {
85             mError = true;
86             return;
87         }
88
89         v->assign(reinterpret_cast<const char *>(mData) + mOffset, length);
90         mOffset += length;
91     }
92
93     void skip(size_t length)
94     {
95         if (mOffset + length > mLength)
96         {
97             mError = true;
98             return;
99         }
100
101         mOffset += length;
102     }
103
104     size_t offset() const
105     {
106         return mOffset;
107     }
108
109     bool error() const
110     {
111         return mError;
112     }
113
114     bool endOfStream() const
115     {
116         return mOffset == mLength;
117     }
118
119     const uint8_t *data()
120     {
121         return mData;
122     }
123
124   private:
125     DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
126     bool mError;
127     size_t mOffset;
128     const uint8_t *mData;
129     size_t mLength;
130
131     template <typename T>
132     void read(T *v, size_t num)
133     {
134         META_ASSERT(std::is_fundamental<T>::value);
135
136         size_t length = num * sizeof(T);
137
138         if (mOffset + length > mLength)
139         {
140             mError = true;
141             return;
142         }
143
144         memcpy(v, mData + mOffset, length);
145         mOffset += length;
146     }
147
148     template <typename T>
149     void read(T *v)
150     {
151         read(v, 1);
152     }
153
154 };
155
156 class BinaryOutputStream
157 {
158   public:
159     BinaryOutputStream()
160     {
161     }
162
163     // writeInt also handles bool types
164     template <class IntT>
165     void writeInt(IntT param)
166     {
167         ASSERT(rx::IsIntegerCastSafe<int>(param));
168         int intValue = static_cast<int>(param);
169         write(&intValue, 1);
170     }
171
172     void writeString(const std::string &v)
173     {
174         writeInt(v.length());
175         write(v.c_str(), v.length());
176     }
177
178     void writeBytes(const unsigned char *bytes, size_t count)
179     {
180         write(bytes, count);
181     }
182
183     size_t length() const
184     {
185         return mData.size();
186     }
187
188     const void* data() const
189     {
190         return mData.size() ? &mData[0] : NULL;
191     }
192
193   private:
194     DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
195     std::vector<char> mData;
196
197     template <typename T>
198     void write(const T *v, size_t num)
199     {
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));
203     }
204
205 };
206 }
207
208 #endif  // LIBGLESV2_BINARYSTREAM_H_