Line data Source code
1 : /*
2 : * Copyright 2006 The Android Open Source Project
3 : *
4 : * Use of this source code is governed by a BSD-style license that can be
5 : * found in the LICENSE file.
6 : */
7 :
8 :
9 : #ifndef SkBuffer_DEFINED
10 : #define SkBuffer_DEFINED
11 :
12 : #include "SkScalar.h"
13 : #include "SkTypes.h"
14 :
15 : /** \class SkRBuffer
16 :
17 : Light weight class for reading data from a memory block.
18 : The RBuffer is given the buffer to read from, with either a specified size
19 : or no size (in which case no range checking is performed). It is iillegal
20 : to attempt to read a value from an empty RBuffer (data == null).
21 : */
22 : class SkRBuffer : SkNoncopyable {
23 : public:
24 : SkRBuffer() : fData(0), fPos(0), fStop(0) {}
25 :
26 : /** Initialize RBuffer with a data point and length.
27 : */
28 0 : SkRBuffer(const void* data, size_t size) {
29 0 : SkASSERT(data != 0 || size == 0);
30 0 : fData = (const char*)data;
31 0 : fPos = (const char*)data;
32 0 : fStop = (const char*)data + size;
33 0 : }
34 :
35 : /** Return the number of bytes that have been read from the beginning
36 : of the data pointer.
37 : */
38 0 : size_t pos() const { return fPos - fData; }
39 : /** Return the total size of the data pointer. Only defined if the length was
40 : specified in the constructor or in a call to reset().
41 : */
42 : size_t size() const { return fStop - fData; }
43 : /** Return true if the buffer has read to the end of the data pointer.
44 : Only defined if the length was specified in the constructor or in a call
45 : to reset(). Always returns true if the length was not specified.
46 : */
47 : bool eof() const { return fPos >= fStop; }
48 :
49 0 : size_t available() const { return fStop - fPos; }
50 :
51 0 : bool isValid() const { return fValid; }
52 :
53 : /** Read the specified number of bytes from the data pointer. If buffer is not
54 : null, copy those bytes into buffer.
55 : */
56 : bool read(void* buffer, size_t size);
57 : bool skipToAlign4();
58 :
59 : bool readU8(uint8_t* x) { return this->read(x, 1); }
60 0 : bool readS32(int32_t* x) { return this->read(x, 4); }
61 0 : bool readU32(uint32_t* x) { return this->read(x, 4); }
62 :
63 : private:
64 : const char* fData;
65 : const char* fPos;
66 : const char* fStop;
67 : bool fValid = true;
68 : };
69 :
70 : /** \class SkWBuffer
71 :
72 : Light weight class for writing data to a memory block.
73 : The WBuffer is given the buffer to write into, with either a specified size
74 : or no size, in which case no range checking is performed. An empty WBuffer
75 : is legal, in which case no data is ever written, but the relative pos()
76 : is updated.
77 : */
78 : class SkWBuffer : SkNoncopyable {
79 : public:
80 : SkWBuffer() : fData(0), fPos(0), fStop(0) {}
81 0 : SkWBuffer(void* data) { reset(data); }
82 : SkWBuffer(void* data, size_t size) { reset(data, size); }
83 :
84 0 : void reset(void* data) {
85 0 : fData = (char*)data;
86 0 : fPos = (char*)data;
87 0 : fStop = 0; // no bounds checking
88 0 : }
89 :
90 : void reset(void* data, size_t size) {
91 : SkASSERT(data != 0 || size == 0);
92 : fData = (char*)data;
93 : fPos = (char*)data;
94 : fStop = (char*)data + size;
95 : }
96 :
97 0 : size_t pos() const { return fPos - fData; }
98 : void* skip(size_t size); // return start of skipped data
99 :
100 0 : void write(const void* buffer, size_t size) {
101 0 : if (size) {
102 0 : this->writeNoSizeCheck(buffer, size);
103 : }
104 0 : }
105 :
106 : size_t padToAlign4();
107 :
108 : void writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); }
109 : void writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); }
110 0 : void write32(int32_t x) { this->writeNoSizeCheck(&x, 4); }
111 : void write16(int16_t x) { this->writeNoSizeCheck(&x, 2); }
112 : void write8(int8_t x) { this->writeNoSizeCheck(&x, 1); }
113 : void writeBool(bool x) { this->write8(x); }
114 :
115 : private:
116 : void writeNoSizeCheck(const void* buffer, size_t size);
117 :
118 : char* fData;
119 : char* fPos;
120 : char* fStop;
121 : };
122 :
123 : #endif
|