SW Task Event Loop Framework v1.0.0
High-performance C++ asynchronous event loop framework with timer management and promise-based programming
Loading...
Searching...
No Matches
Buffer.cpp
Go to the documentation of this file.
1
9 #include "Buffer.h"
10 #include <cstring>
11 #include <iostream>
12 #include <iomanip>
13
14 namespace swt {
21 Buffer::Buffer() : mClear(false) {
22 // Empty buffer initialization
23 }
24
32 Buffer::Buffer(const Buffer& other) : mClear(false) {
33 setTo(other);
34 }
35
43 clear();
44 }
45
55 if (this != &other) {
56 setTo(other);
57 }
58 return *this;
59 }
60
61 // ========== SIZE AND INITIALIZATION METHODS ==========
62
73 void Buffer::setSize(int32_t len) {
74 if (len < 0) len = 0; // Safety check
75 mBuf.assign(len, 0); // Resize and fill with zeros
76 }
77
78 // ========== DATA COPYING METHODS ==========
79
87 void Buffer::setTo(const Buffer& buffer) {
88 mBuf = buffer.mBuf;
89 }
90
101 void Buffer::setTo(uint8_t* buf, int32_t len) {
102 if (buf && len > 0) {
103 mBuf.assign(buf, buf + len);
104 } else {
105 mBuf.clear(); // Clear buffer if invalid input
106 }
107 }
108
117 void Buffer::setTo(char* buf, int32_t len) {
118 setTo(reinterpret_cast<uint8_t*>(buf), len);
119 }
120
131 void Buffer::append(uint8_t* buf, int32_t len) {
132 if (buf && len > 0) {
133 mBuf.insert(mBuf.end(), buf, buf + len);
134 }
135 }
136
137 // ========== DATA ACCESS METHODS ==========
138
149 uint8_t* Buffer::data() {
150 return mBuf.empty() ? nullptr : mBuf.data();
151 }
152
160 uint32_t Buffer::size() const {
161 return static_cast<uint32_t>(mBuf.size());
162 }
163
172 return mBuf.empty();
173 }
174
175 // ========== BUFFER MANAGEMENT METHODS ==========
176
186 mBuf.clear();
187 mClear = true;
188 }
189
190 // ========== DEBUG AND UTILITY METHODS ==========
191
200 if (!mBuf.empty()) {
201 dump(mBuf.data(), static_cast<int32_t>(mBuf.size()));
202 } else {
203 std::cout << "Buffer dump: (empty buffer)" << std::endl;
204 }
205 }
206
218 void Buffer::dump(uint8_t* s, int32_t len) {
219 if (!s || len <= 0) {
220 std::cout << "Buffer dump: (null or empty data)" << std::endl;
221 return;
222 }
223
224 std::cout << "Buffer dump (" << len << " bytes): ";
225
226 // Save current format flags
227 auto flags = std::cout.flags();
228
229 // Set hex format with uppercase and proper width
230 std::cout << std::hex << std::uppercase << std::setfill('0');
231
232 for (int32_t i = 0; i < len; ++i) {
233 std::cout << std::setw(2) << static_cast<unsigned int>(s[i]);
234 if (i < len - 1) std::cout << " "; // Space between bytes
235 }
236
237 // Restore original format flags
238 std::cout.flags(flags);
239 std::cout << std::endl;
240 }
241
242 // ========== INTERNAL METHODS ==========
243
254 void Buffer::_assign(uint32_t size) {
255 mBuf.resize(size);
256 }
257
266 void Buffer::_release() {
267 clear();
268 }
269
270} // namespace swt
Buffer class for dynamic byte storage and manipulation.
Dynamic byte buffer for data storage and manipulation.
Definition Buffer.h:58
uint8_t * data()
Get pointer to buffer data.
Definition Buffer.cpp:149
Buffer()
Default constructor - creates empty buffer.
Definition Buffer.cpp:21
uint32_t size() const
Get current buffer size.
Definition Buffer.cpp:160
void append(uint8_t *buf, int32_t len)
Append data to existing buffer contents.
Definition Buffer.cpp:131
void setTo(const Buffer &buffer)
Copy data from another buffer.
Definition Buffer.cpp:87
bool empty()
Check if buffer contains no data.
Definition Buffer.cpp:171
~Buffer()
Destructor - cleans up buffer resources.
Definition Buffer.cpp:42
void setSize(int32_t len)
Set buffer size and initialize with zeros.
Definition Buffer.cpp:73
void dump()
Dump current buffer contents to stdout.
Definition Buffer.cpp:199
Buffer & operator=(const Buffer &other)
Assignment operator - assigns data from another buffer.
Definition Buffer.cpp:54
void clear()
Clear all buffer contents and mark as cleared.
Definition Buffer.cpp:185
Software Timer namespace containing all timer-related classes.
Definition Awaitable.h:21