TDME2 1.9.121
Buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <tdme/tdme.h>
4#include <tdme/math/Math.h>
6
7#include <cstring>
8#include <vector>
9
10using std::memcpy;
11using std::vector;
12
14
15/**
16 * Base class of buffers
17 * @author Andreas Drewke
18 */
20{
21private:
23 int32_t position { 0 };
24 vector<uint8_t>* buffer { nullptr };
25
26public:
27 /**
28 * Public constructor
29 * @param capacity capacity
30 */
31 inline Buffer(int32_t capacity) {
32 this->createdBuffer = true;
33 this->position = 0;
34 this->buffer = new vector<uint8_t>(capacity);
35 }
36
37 /**
38 * Public constructor
39 * @param buffer buffer
40 */
41 inline Buffer(Buffer* buffer) {
42 this->createdBuffer = false;
43 this->position = 0;
44 this->buffer = buffer == nullptr?nullptr:buffer->buffer;
45 }
46
47 /**
48 * Public constructor
49 * @param data data
50 */
51 inline Buffer(vector<uint8_t>* data) {
52 this->createdBuffer = false;
53 this->position = 0;
54 this->buffer = data;
55 }
56
57 /**
58 * Destructor
59 */
60 inline virtual ~Buffer() {
61 if (createdBuffer == true) {
62 delete this->buffer;
63 }
64 }
65
66 /**
67 * Clear
68 */
69 inline Buffer* clear() {
70 position = 0;
71 return this;
72 }
73
74 /**
75 * @returns capacity
76 */
77 inline virtual int32_t getCapacity() {
78 return buffer->size();
79 }
80
81 /**
82 * @returns position
83 */
84 inline virtual int32_t getPosition() {
85 return position;
86 }
87
88 /**
89 * Set position
90 * @param position position
91 * @returns pointer to this buffer
92 */
93 inline virtual Buffer* setPosition(int32_t position) {
94 this->position = position;
95 return this;
96 }
97
98 /**
99 * @returns value at given position
100 * @param position position
101 */
102 inline uint8_t get(int32_t position) {
103 return (*buffer)[position];
104 }
105
106 /**
107 * Put value into buffer
108 * @param value value
109 */
110 inline Buffer* put(uint8_t value) {
111 (*buffer)[position++] = value;
112 return this;
113 }
114
115 /**
116 * Put data into buffer
117 * @param data pointer to data
118 * @param size to put
119 * @returns pointer to this buffer
120 */
121 inline Buffer* put(const uint8_t* data, int32_t size) {
122 auto sizeUsed = Math::min(size, buffer->size() - position);
123 memcpy(&(*buffer)[position], data, sizeUsed);
124 position+= sizeUsed;
125 return this;
126 }
127
128 /**
129 * @returns pointer to underlying data
130 */
131 inline uint8_t* getBuffer() {
132 return buffer->data();
133 }
134
135};
Standard math functions.
Definition: Math.h:21
Base class of buffers.
Definition: Buffer.h:20
Buffer * put(uint8_t value)
Put value into buffer.
Definition: Buffer.h:110
uint8_t * getBuffer()
Definition: Buffer.h:131
uint8_t get(int32_t position)
Definition: Buffer.h:102
virtual int32_t getPosition()
Definition: Buffer.h:84
Buffer(Buffer *buffer)
Public constructor.
Definition: Buffer.h:41
vector< uint8_t > * buffer
Definition: Buffer.h:24
virtual int32_t getCapacity()
Definition: Buffer.h:77
Buffer * put(const uint8_t *data, int32_t size)
Put data into buffer.
Definition: Buffer.h:121
Buffer * clear()
Clear.
Definition: Buffer.h:69
Buffer(vector< uint8_t > *data)
Public constructor.
Definition: Buffer.h:51
Buffer(int32_t capacity)
Public constructor.
Definition: Buffer.h:31
virtual Buffer * setPosition(int32_t position)
Set position.
Definition: Buffer.h:93
virtual ~Buffer()
Destructor.
Definition: Buffer.h:60