TDME2  1.9.200
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 <string>
8 #include <cstring>
9 #include <vector>
10 
11 using std::memcpy;
12 using std::vector;
13 
14 using tdme::math::Math;
15 
16 /**
17  * Base class of buffers
18  * @author Andreas Drewke
19  */
21 {
22  friend class ByteBuffer;
23  friend class FloatBuffer;
24  friend class IntBuffer;
25  friend class ShortBuffer;
26 
27 protected:
28  bool ownsBuffer;
29  mutable int64_t position { 0 };
30  vector<uint8_t>* buffer { nullptr };
31 
32 public:
33  /**
34  * Public constructor
35  * @param capacity capacity
36  */
37  inline Buffer() {
38  this->ownsBuffer = false;
39  this->position = 0;
40  this->buffer = nullptr;
41  }
42 
43  /**
44  * Public constructor
45  * @param capacity capacity
46  */
47  inline Buffer(int64_t capacity) {
48  this->ownsBuffer = true;
49  this->position = 0;
50  this->buffer = new vector<uint8_t>(capacity);
51  }
52 
53  /**
54  * Public constructor
55  * @param data data
56  */
57  inline Buffer(const vector<uint8_t>& data) {
58  this->ownsBuffer = true;
59  this->position = 0;
60  this->buffer = new vector<uint8_t>(0);
61  *this->buffer = data;
62  }
63 
64  /**
65  * Destructor
66  */
67  inline virtual ~Buffer() {
68  if (ownsBuffer == true && buffer != nullptr) delete buffer;
69  }
70 
71  /**
72  * Clear
73  */
74  inline Buffer* clear() {
75  position = 0;
76  return this;
77  }
78 
79  /**
80  * @returns capacity
81  */
82  inline virtual int64_t getCapacity() const {
83  return buffer->size();
84  }
85 
86  /**
87  * @returns position
88  */
89  inline virtual int64_t getPosition() const {
90  return position;
91  }
92 
93  /**
94  * Set position
95  * @param position position
96  * @returns pointer to this buffer
97  */
98  inline virtual Buffer* setPosition(int64_t position) {
99  this->position = position;
100  return this;
101  }
102 
103  /**
104  * @returns value at given position
105  * @param position position
106  */
107  inline uint8_t get(int64_t position) const {
108  return (*buffer)[position];
109  }
110 
111  /**
112  * Put value into buffer
113  * @param value value
114  */
115  inline Buffer* put(uint8_t value) {
116  (*buffer)[position++] = value;
117  return this;
118  }
119 
120  /**
121  * Put data into buffer
122  * @param data pointer to data
123  * @param size to put
124  * @returns pointer to this buffer
125  */
126  inline Buffer* put(const uint8_t* data, int64_t size) {
127  auto sizeUsed = Math::min(size, buffer->size() - position);
128  memcpy(&(*buffer)[position], data, sizeUsed);
129  position+= sizeUsed;
130  return this;
131  }
132 
133  /**
134  * @returns const pointer to underlying data vector
135  */
136  inline const vector<uint8_t>* getBufferVector() const {
137  return buffer;
138  }
139 
140  /**
141  * @returns pointer to underlying data vector
142  */
143  inline vector<uint8_t>* getBufferVector() {
144  return buffer;
145  }
146 
147  /**
148  * @returns const pointer to underlying data
149  */
150  inline const uint8_t* getBuffer() const {
151  return buffer->data();
152  }
153 
154  /**
155  * @returns pointer to underlying data
156  */
157  inline uint8_t* getBuffer() {
158  return buffer->data();
159  }
160 
161 };
Standard math functions.
Definition: Math.h:19
Base class of buffers.
Definition: Buffer.h:21
uint8_t get(int64_t position) const
Definition: Buffer.h:107
uint8_t * getBuffer()
Definition: Buffer.h:157
const vector< uint8_t > * getBufferVector() const
Definition: Buffer.h:136
Buffer * clear()
Clear.
Definition: Buffer.h:74
Buffer()
Public constructor.
Definition: Buffer.h:37
Buffer * put(const uint8_t *data, int64_t size)
Put data into buffer.
Definition: Buffer.h:126
vector< uint8_t > * getBufferVector()
Definition: Buffer.h:143
Buffer(int64_t capacity)
Public constructor.
Definition: Buffer.h:47
virtual Buffer * setPosition(int64_t position)
Set position.
Definition: Buffer.h:98
vector< uint8_t > * buffer
Definition: Buffer.h:30
const uint8_t * getBuffer() const
Definition: Buffer.h:150
Buffer * put(uint8_t value)
Put value into buffer.
Definition: Buffer.h:115
virtual int64_t getCapacity() const
Definition: Buffer.h:82
virtual int64_t getPosition() const
Definition: Buffer.h:89
virtual ~Buffer()
Destructor.
Definition: Buffer.h:67
Buffer(const vector< uint8_t > &data)
Public constructor.
Definition: Buffer.h:57
Byte buffer class.
Definition: ByteBuffer.h:27
Float buffer class.
Definition: FloatBuffer.h:18
Integer buffer class.
Definition: IntBuffer.h:14
Short buffer class.
Definition: ShortBuffer.h:14