Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkBuffer.h
Go to the documentation of this file.
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#ifndef SkBuffer_DEFINED
9#define SkBuffer_DEFINED
10
13#include "src/base/SkSafeMath.h"
14
15#include <cstddef>
16#include <cstdint>
17
18typedef float SkScalar;
19
20/** \class SkRBuffer
21
22 Light weight class for reading data from a memory block.
23 The RBuffer is given the buffer to read from, with either a specified size
24 or no size (in which case no range checking is performed). It is iillegal
25 to attempt to read a value from an empty RBuffer (data == null).
26*/
28public:
29 SkRBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
30
31 /** Initialize RBuffer with a data point and length.
32 */
33 SkRBuffer(const void* data, size_t size) {
34 SkASSERT(data != nullptr || size == 0);
35 fData = (const char*)data;
36 fPos = (const char*)data;
37 fStop = (const char*)data + size;
38 }
39
40 /** Return the number of bytes that have been read from the beginning
41 of the data pointer.
42 */
43 size_t pos() const { return fPos - fData; }
44 /** Return the total size of the data pointer. Only defined if the length was
45 specified in the constructor or in a call to reset().
46 */
47 size_t size() const { return fStop - fData; }
48 /** Return true if the buffer has read to the end of the data pointer.
49 Only defined if the length was specified in the constructor or in a call
50 to reset(). Always returns true if the length was not specified.
51 */
52 bool eof() const { return fPos >= fStop; }
53
54 size_t available() const { return fStop - fPos; }
55
56 bool isValid() const { return fValid; }
57
58 /** Read the specified number of bytes from the data pointer. If buffer is not
59 null, copy those bytes into buffer.
60 */
61 bool read(void* buffer, size_t size);
62 bool skipToAlign4();
63
64 bool readU8(uint8_t* x) { return this->read(x, 1); }
65 bool readS32(int32_t* x) { return this->read(x, 4); }
66 bool readU32(uint32_t* x) { return this->read(x, 4); }
67
68 // returns nullptr on failure
69 const void* skip(size_t bytes);
70 template <typename T> const T* skipCount(size_t count) {
71 return static_cast<const T*>(this->skip(SkSafeMath::Mul(count, sizeof(T))));
72 }
73
74private:
75 const char* fData;
76 const char* fPos;
77 const char* fStop;
78 bool fValid = true;
79};
80
81/** \class SkWBuffer
82
83 Light weight class for writing data to a memory block.
84 The WBuffer is given the buffer to write into, with either a specified size
85 or no size, in which case no range checking is performed. An empty WBuffer
86 is legal, in which case no data is ever written, but the relative pos()
87 is updated.
88*/
90public:
91 SkWBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
92 SkWBuffer(void* data) { reset(data); }
93 SkWBuffer(void* data, size_t size) { reset(data, size); }
94
95 void reset(void* data) {
96 fData = (char*)data;
97 fPos = (char*)data;
98 fStop = nullptr; // no bounds checking
99 }
100
101 void reset(void* data, size_t size) {
102 SkASSERT(data != nullptr || size == 0);
103 fData = (char*)data;
104 fPos = (char*)data;
105 fStop = (char*)data + size;
106 }
107
108 size_t pos() const { return fPos - fData; }
109 void* skip(size_t size); // return start of skipped data
110
111 void write(const void* buffer, size_t size) {
112 if (size) {
113 this->writeNoSizeCheck(buffer, size);
114 }
115 }
116
117 size_t padToAlign4();
118
119 void writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); }
120 void writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); }
121 void write32(int32_t x) { this->writeNoSizeCheck(&x, 4); }
122 void write16(int16_t x) { this->writeNoSizeCheck(&x, 2); }
123 void write8(int8_t x) { this->writeNoSizeCheck(&x, 1); }
124 void writeBool(bool x) { this->write8(x); }
125
126private:
127 void writeNoSizeCheck(const void* buffer, size_t size);
128
129 char* fData;
130 char* fPos;
131 char* fStop;
132};
133
134#endif
m reset()
int count
#define SkASSERT(cond)
Definition SkAssert.h:116
float SkScalar
Definition SkBuffer.h:18
bool eof() const
Definition SkBuffer.h:52
bool read(void *buffer, size_t size)
Definition SkBuffer.cpp:27
bool isValid() const
Definition SkBuffer.h:56
size_t pos() const
Definition SkBuffer.h:43
const T * skipCount(size_t count)
Definition SkBuffer.h:70
size_t available() const
Definition SkBuffer.h:54
SkRBuffer()
Definition SkBuffer.h:29
bool readS32(int32_t *x)
Definition SkBuffer.h:65
size_t size() const
Definition SkBuffer.h:47
SkRBuffer(const void *data, size_t size)
Definition SkBuffer.h:33
const void * skip(size_t bytes)
Definition SkBuffer.cpp:17
bool skipToAlign4()
Definition SkBuffer.cpp:35
bool readU32(uint32_t *x)
Definition SkBuffer.h:66
bool readU8(uint8_t *x)
Definition SkBuffer.h:64
static size_t Mul(size_t x, size_t y)
SkWBuffer(void *data, size_t size)
Definition SkBuffer.h:93
void write16(int16_t x)
Definition SkBuffer.h:122
void * skip(size_t size)
Definition SkBuffer.cpp:49
size_t padToAlign4()
Definition SkBuffer.cpp:63
void write32(int32_t x)
Definition SkBuffer.h:121
void writePtr(const void *x)
Definition SkBuffer.h:119
void writeBool(bool x)
Definition SkBuffer.h:124
SkWBuffer()
Definition SkBuffer.h:91
size_t pos() const
Definition SkBuffer.h:108
void reset(void *data, size_t size)
Definition SkBuffer.h:101
SkWBuffer(void *data)
Definition SkBuffer.h:92
void write8(int8_t x)
Definition SkBuffer.h:123
void writeScalar(SkScalar x)
Definition SkBuffer.h:120
void reset(void *data)
Definition SkBuffer.h:95
void write(const void *buffer, size_t size)
Definition SkBuffer.h:111
static const uint8_t buffer[]
double x
#define T