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
swt::Buffer Class Reference

Dynamic byte buffer for data storage and manipulation. More...

#include <Buffer.h>

Collaboration diagram for swt::Buffer:
Collaboration graph

Public Member Functions

 Buffer ()
 Default constructor - creates empty buffer.
 
 Buffer (const Buffer &other)
 Copy constructor - creates buffer from another buffer.
 
 ~Buffer ()
 Destructor - cleans up buffer resources.
 
Bufferoperator= (const Buffer &other)
 Assignment operator - assigns data from another buffer.
 
void setSize (int32_t len)
 Set buffer size and initialize with zeros.
 
void setTo (const Buffer &buffer)
 Copy data from another buffer.
 
void setTo (uint8_t *buf, int32_t len)
 Copy data from byte array.
 
void setTo (char *buf, int32_t len)
 Copy data from char array.
 
void append (uint8_t *buf, int32_t len)
 Append data to existing buffer contents.
 
uint8_t * data ()
 Get pointer to buffer data.
 
uint32_t size () const
 Get current buffer size.
 
bool empty ()
 Check if buffer contains no data.
 
void clear ()
 Clear all buffer contents and mark as cleared.
 
void dump ()
 Dump current buffer contents to stdout.
 

Static Public Member Functions

static void dump (uint8_t *s, int32_t len)
 Static method to dump byte array contents to stdout.
 

Detailed Description

Dynamic byte buffer for data storage and manipulation.

Provides a dynamic byte array with various utility methods for data manipulation, copying, and debugging output. Built on top of std::vector<uint8_t> for efficient memory management.

Key features:

  • Dynamic sizing: Automatic memory management with resize operations
  • Data copying: Multiple methods for copying from various sources
  • Append operations: Efficient data concatenation
  • Debug support: Hex dump utilities for debugging
  • Safe access: Null pointer protection and bounds checking
  • STL integration: Compatible with standard library algorithms
// Create and initialize buffer
Buffer buffer;
buffer.setSize(1024); // 1KB buffer initialized with zeros
// Copy data from array
uint8_t data[] = {0x01, 0x02, 0x03, 0x04};
buffer.setTo(data, sizeof(data));
// Append more data
uint8_t more[] = {0x05, 0x06};
buffer.append(more, sizeof(more));
// Debug output
buffer.dump(); // Prints: "Buffer dump (6 bytes): 01 02 03 04 05 06"
// Access data
uint8_t* ptr = buffer.data();
uint32_t size = buffer.size();
Dynamic byte buffer for data storage and manipulation.
Definition Buffer.h:58
uint8_t * data()
Get pointer to buffer data.
Definition Buffer.cpp:149
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
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
Note
Thread-safe for read-only operations when not being modified
Not thread-safe for concurrent modifications
Warning
Pointer returned by data() becomes invalid after resize operations
See also
setSize(), setTo(), append(), data(), size(), clear(), dump()

Definition at line 58 of file Buffer.h.

Constructor & Destructor Documentation

◆ Buffer() [1/2]

swt::Buffer::Buffer ( )

Default constructor - creates empty buffer.

Initializes buffer with empty state and clear flag set to false. The underlying vector is empty and ready for data operations.

Note
No memory allocated until first data operation

Initializes buffer with empty state and clear flag set to false. The underlying vector is empty and ready for data operations.

Definition at line 21 of file Buffer.cpp.

◆ Buffer() [2/2]

swt::Buffer::Buffer ( const Buffer other)

Copy constructor - creates buffer from another buffer.

Parameters
otherSource buffer to copy from

Creates a new buffer by performing deep copy of all data from the source buffer. Both buffers are independent after construction.

Note
Performs deep copy - changes to either buffer don't affect the other
Parameters
otherSource buffer to copy from

Creates a new buffer by copying all data from the source buffer. Uses setTo() method for proper deep copy of vector data.

Definition at line 32 of file Buffer.cpp.

References setTo().

Here is the call graph for this function:

◆ ~Buffer()

swt::Buffer::~Buffer ( )

Destructor - cleans up buffer resources.

Automatically releases all allocated memory. The underlying std::vector destructor handles the actual memory deallocation.

Calls clear() to release all allocated memory and reset state. The vector destructor will handle the actual memory deallocation.

Definition at line 42 of file Buffer.cpp.

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ append()

void swt::Buffer::append ( uint8_t *  buf,
int32_t  len 
)

Append data to existing buffer contents.

Parameters
bufSource byte array to append
lenLength of data to append in bytes

Adds new data to the end of existing buffer contents. Buffer size increases by the length of appended data.

buffer.setTo(data1, len1); // Initial data
buffer.append(data2, len2); // Append more data
// Buffer now contains data1 + data2
Note
Null pointer or zero/negative length is ignored (no-op)
Efficient - uses vector::insert for optimal performance
Previous buffer contents are preserved
See also
append()
Parameters
bufSource byte array to append
lenLength of data to append in bytes

Adds new data to the end of existing buffer contents. Buffer size increases by the length of appended data.

Note
Null pointer or zero/negative length is ignored (no-op)

Definition at line 131 of file Buffer.cpp.

◆ clear()

void swt::Buffer::clear ( )

Clear all buffer contents and mark as cleared.

Removes all data from buffer and sets the clear flag. This frees all allocated memory and resets to empty state.

buffer.clear();
assert(buffer.empty());
assert(buffer.size() == 0);
Note
Sets internal mClear flag to true for debugging purposes
Frees allocated memory (not just marks as empty)
After clear(), data() returns nullptr
See also
empty(), size()

Removes all data from buffer and sets the clear flag. This frees all allocated memory and resets to empty state.

Note
Sets mClear flag to true for debugging purposes

Definition at line 185 of file Buffer.cpp.

Referenced by ~Buffer().

Here is the caller graph for this function:

◆ data()

uint8_t * swt::Buffer::data ( )

Get pointer to buffer data.

Returns
Pointer to internal buffer data, or nullptr if empty

Provides direct access to internal buffer data for reading or writing. The pointer becomes invalid after buffer resize or destruction.

uint8_t* ptr = buffer.data();
if (ptr) {
// Safe to access ptr[0] through ptr[buffer.size()-1]
memcpy(destination, ptr, buffer.size());
}
Warning
Pointer validity tied to buffer lifetime and size changes
Do not access beyond buffer.size() bytes
Note
Returns nullptr for empty buffers to avoid undefined behavior
Pointer may change after resize, append, or setTo operations
See also
size()
Returns
Pointer to internal buffer data, or nullptr if empty

Provides direct access to internal buffer data for reading or writing. The pointer becomes invalid after buffer resize or destruction.

Warning
Pointer validity tied to buffer lifetime and size changes
Note
Returns nullptr for empty buffers to avoid undefined behavior

Definition at line 149 of file Buffer.cpp.

◆ dump() [1/2]

void swt::Buffer::dump ( )

Dump current buffer contents to stdout.

Prints buffer contents in hexadecimal format for debugging. Shows each byte as a two-digit hex value separated by spaces. Does nothing if buffer is empty.

uint8_t data[] = {0x01, 0x02, 0x03};
buffer.setTo(data, 3);
buffer.dump(); // Output: "Buffer dump (3 bytes): 01 02 03"
Note
Non-destructive operation - buffer contents unchanged
Uses uppercase hexadecimal format
Outputs to std::cout with automatic newline
See also
dump()

Prints buffer contents in hexadecimal format for debugging. Shows each byte as a two-digit hex value separated by spaces. Does nothing if buffer is empty.

Definition at line 199 of file Buffer.cpp.

References dump().

Referenced by dump().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dump() [2/2]

void swt::Buffer::dump ( uint8_t *  s,
int32_t  len 
)
static

Static method to dump byte array contents to stdout.

Parameters
sPointer to byte array to dump
lenNumber of bytes to dump

Utility method for dumping arbitrary byte arrays in hex format. Useful for debugging data that's not in a Buffer object.

Format: "Buffer dump (N bytes): XX XX XX ..." where XX represents each byte in uppercase hexadecimal.

uint8_t data[] = {0xDE, 0xAD, 0xBE, 0xEF};
Buffer::dump(data, 4); // Output: "Buffer dump (4 bytes): DE AD BE EF"
Note
Static method - can be called without Buffer instance
Handles null pointer and invalid length gracefully
Same format as instance dump() method
See also
dump()
Parameters
sPointer to byte array to dump
lenNumber of bytes to dump

Utility method for dumping arbitrary byte arrays in hex format. Useful for debugging data that's not in a Buffer object.

Format: "Buffer dump (N bytes): XX XX XX ..." where XX represents each byte in uppercase hexadecimal.

Definition at line 218 of file Buffer.cpp.

◆ empty()

bool swt::Buffer::empty ( )

Check if buffer contains no data.

Returns
true if buffer is empty (size == 0)

Efficient way to check if buffer contains any data without getting the actual size value. Preferred over size() == 0.

if (!buffer.empty()) {
// Process buffer data
processData(buffer.data(), buffer.size());
}
Note
More efficient than size() == 0 check
Constant time operation - O(1)
See also
size()
Returns
true if buffer is empty (size == 0)

Efficient way to check if buffer contains any data without getting the actual size value.

Definition at line 171 of file Buffer.cpp.

◆ operator=()

Buffer & swt::Buffer::operator= ( const Buffer other)

Assignment operator - assigns data from another buffer.

Parameters
otherSource buffer to assign from
Returns
Reference to this buffer for chaining

Performs deep copy assignment with self-assignment protection. Replaces current buffer contents with source buffer data.

Buffer buf1, buf2;
buf1.setTo(data, size);
buf2 = buf1; // Deep copy assignment
Note
Self-assignment safe
Previous buffer contents are lost
See also
setTo()
Parameters
otherSource buffer to assign from
Returns
Reference to this buffer for chaining

Performs deep copy assignment with self-assignment protection. Uses setTo() method for consistent copying behavior.

Definition at line 54 of file Buffer.cpp.

References setTo().

Here is the call graph for this function:

◆ setSize()

void swt::Buffer::setSize ( int32_t  len)

Set buffer size and initialize with zeros.

Parameters
lenNew buffer size in bytes

Resizes the buffer to the specified length and fills all bytes with zeros. This provides a clean initialized buffer for data operations.

Buffer buffer;
buffer.setSize(1024); // 1KB buffer filled with zeros
Note
Previous buffer contents are lost
Negative length is treated as zero (creates empty buffer)
More efficient than resize + manual zero-fill
See also
clear()
Parameters
lenNew buffer size in bytes

Resizes the buffer to the specified length and fills all bytes with zeros. This provides a clean initialized buffer for data operations.

Note
Previous buffer contents are lost
Negative length is treated as zero

Definition at line 73 of file Buffer.cpp.

◆ setTo() [1/3]

void swt::Buffer::setTo ( char *  buf,
int32_t  len 
)

Copy data from char array.

Parameters
bufSource char array pointer
lenLength of data to copy in bytes

Convenience method that casts char pointer to uint8_t and delegates to the byte array version of setTo().

char str[] = "Hello";
buffer.setTo(str, strlen(str));
Note
Same behavior as uint8_t* version
Useful for string data without null terminator
See also
setTo()
Parameters
bufSource char array pointer
lenLength of data to copy in bytes

Convenience method that casts char pointer to uint8_t and delegates to the byte array version of setTo().

Definition at line 117 of file Buffer.cpp.

References setTo().

Here is the call graph for this function:

◆ setTo() [2/3]

void swt::Buffer::setTo ( const Buffer buffer)

Copy data from another buffer.

Parameters
bufferSource buffer to copy from

Performs deep copy of all data from source buffer. This replaces current buffer contents entirely.

Buffer source, dest;
source.setTo(data, size);
dest.setTo(source); // dest now contains copy of source data
Note
Deep copy - source and destination are independent
Previous destination contents are lost
See also
setTo()
Parameters
bufferSource buffer to copy from

Performs deep copy of all data from source buffer. This replaces current buffer contents entirely.

Definition at line 87 of file Buffer.cpp.

Referenced by Buffer(), operator=(), and setTo().

Here is the caller graph for this function:

◆ setTo() [3/3]

void swt::Buffer::setTo ( uint8_t *  buf,
int32_t  len 
)

Copy data from byte array.

Parameters
bufSource byte array pointer
lenLength of data to copy in bytes

Copies specified number of bytes from source array to buffer. Replaces current buffer contents with new data.

uint8_t data[] = {0x01, 0x02, 0x03};
buffer.setTo(data, sizeof(data));
Note
Null pointer or zero/negative length results in empty buffer
Previous buffer contents are lost
Warning
Source pointer must remain valid during copy operation
See also
setTo()
Parameters
bufSource byte array pointer
lenLength of data to copy in bytes

Copies specified number of bytes from source array to buffer. Replaces current buffer contents with new data.

Note
Null pointer or zero/negative length results in empty buffer

Definition at line 101 of file Buffer.cpp.

◆ size()

uint32_t swt::Buffer::size ( ) const

Get current buffer size.

Returns
Number of bytes currently stored in buffer

Returns the actual number of bytes of data in the buffer. This represents the valid data size, not the allocated capacity.

for (uint32_t i = 0; i < buffer.size(); ++i) {
process(buffer.data()[i]);
}
Note
This may be different from the vector's capacity
Constant time operation - O(1)
See also
data()
Returns
Number of bytes currently stored in buffer

Returns the actual number of bytes of data in the buffer. This may be different from the vector's capacity.

Definition at line 160 of file Buffer.cpp.


The documentation for this class was generated from the following files: