blob: bc11a1e70efd06f36a3ebc7279ccb48a06095de4 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkBuffer_DEFINED
18#define SkBuffer_DEFINED
19
20#include "SkScalar.h"
21
22/** \class SkRBuffer
23
24 Light weight class for reading data from a memory block.
25 The RBuffer is given the buffer to read from, with either a specified size
26 or no size (in which case no range checking is performed). It is iillegal
27 to attempt to read a value from an empty RBuffer (data == null).
28*/
29class SkRBuffer : SkNoncopyable {
30public:
31 SkRBuffer() : fData(0), fPos(0), fStop(0) {}
32 /** Initialize RBuffer with a data pointer, but no specified length.
33 This signals the RBuffer to not perform range checks during reading.
34 */
35 SkRBuffer(const void* data)
36 {
37 fData = (const char*)data;
38 fPos = (const char*)data;
39 fStop = 0; // no bounds checking
40 }
41 /** Initialize RBuffer with a data point and length.
42 */
43 SkRBuffer(const void* data, size_t size)
44 {
45 SkASSERT(data != 0 || size == 0);
46 fData = (const char*)data;
47 fPos = (const char*)data;
48 fStop = (const char*)data + size;
49 }
50
51 /** Return the number of bytes that have been read from the beginning
52 of the data pointer.
53 */
54 size_t pos() const { return fPos - fData; }
55 /** Return the total size of the data pointer. Only defined if the length was
56 specified in the constructor or in a call to reset().
57 */
58 size_t size() const { return fStop - fData; }
59 /** Return true if the buffer has read to the end of the data pointer.
60 Only defined if the length was specified in the constructor or in a call
61 to reset(). Always returns true if the length was not specified.
62 */
63 bool eof() const { return fPos >= fStop; }
64
65 /** Read the specified number of bytes from the data pointer. If buffer is not
66 null, copy those bytes into buffer.
67 */
68 void read(void* buffer, size_t size) { if (size) this->readNoSizeCheck(buffer, size); }
69 const void* skip(size_t size); // return start of skipped data
70 size_t skipToAlign4();
71
72 void* readPtr() { void* ptr; read(&ptr, sizeof(ptr)); return ptr; }
73 SkScalar readScalar() { SkScalar x; read(&x, 4); return x; }
74 uint32_t readU32() { uint32_t x; read(&x, 4); return x; }
75 int32_t readS32() { int32_t x; read(&x, 4); return x; }
76 uint16_t readU16() { uint16_t x; read(&x, 2); return x; }
77 int16_t readS16() { int16_t x; read(&x, 2); return x; }
78 uint8_t readU8() { uint8_t x; read(&x, 1); return x; }
79 bool readBool() { return this->readU8() != 0; }
80
81protected:
82 void readNoSizeCheck(void* buffer, size_t size);
83
84 const char* fData;
85 const char* fPos;
86 const char* fStop;
87};
88
89/** \class SkWBuffer
90
91 Light weight class for writing data to a memory block.
92 The WBuffer is given the buffer to write into, with either a specified size
93 or no size, in which case no range checking is performed. An empty WBuffer
94 is legal, in which case no data is ever written, but the relative pos()
95 is updated.
96*/
97class SkWBuffer : SkNoncopyable {
98public:
99 SkWBuffer() : fData(0), fPos(0), fStop(0) {}
100 SkWBuffer(void* data) { reset(data); }
101 SkWBuffer(void* data, size_t size) { reset(data, size); }
102
103 void reset(void* data)
104 {
105 fData = (char*)data;
106 fPos = (char*)data;
107 fStop = 0; // no bounds checking
108 }
109 void reset(void* data, size_t size)
110 {
111 SkASSERT(data != 0 || size == 0);
112 fData = (char*)data;
113 fPos = (char*)data;
114 fStop = (char*)data + size;
115 }
116
117 void* data() const { return fData; }
118 size_t pos() const { return fPos - fData; }
119 size_t size() const { return fStop - fData; }
120 bool eof() const { return fPos >= fStop; }
121 void* skip(size_t size); // return start of skipped data
122 void write(const void* buffer, size_t size) { if (size) this->writeNoSizeCheck(buffer, size); }
123 size_t padToAlign4();
124
125 void writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); }
126 void writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); }
127 void write32(int32_t x) { this->writeNoSizeCheck(&x, 4); }
128 void write16(int16_t x) { this->writeNoSizeCheck(&x, 2); }
129 void write8(int8_t x) { this->writeNoSizeCheck(&x, 1); }
130 void writeBool(bool x) { this->write8(x); }
131
132protected:
133 void writeNoSizeCheck(const void* buffer, size_t size);
134
135 char* fData;
136 char* fPos;
137 char* fStop;
138};
139
140#endif
141