Max OS 0.3
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1
9#ifndef MAXOS_COMMON_BUFFER_H
10#define MAXOS_COMMON_BUFFER_H
11
12#include <cstdint>
13#include <cstddef>
14#include <memory/memoryIO.h>
15#include <common/logger.h>
16
17
18namespace MaxOS::common {
19
25 class Buffer {
26
27 private:
28 uint8_t* m_bytes = nullptr;
29 size_t m_capacity;
30 bool m_dont_delete = false;
31
32 size_t m_offset = 0;
33
34 public:
35 explicit Buffer(size_t size, bool update_offset = true);
36 Buffer(void* source, size_t size, bool update_offset = true);
37 ~Buffer();
38
39 [[nodiscard]] uint8_t* raw() const;
40
41 void clear();
42 void full(uint8_t byte, size_t offset = 0, size_t amount = 0);
43
44 bool update_offset = true;
45 void set_offset(size_t offset);
46
47 [[nodiscard]] size_t capacity() const;
48 void resize(size_t size);
49
50 void write(uint8_t byte);
51 void write(size_t offset, uint8_t byte);
52 uint8_t read();
53 uint8_t read(size_t offset);
54
55 void copy_from(const Buffer* buffer);
56 void copy_from(const Buffer* buffer, size_t length);
57 void copy_from(const Buffer* buffer, size_t length, size_t offset);
58 void copy_from(const Buffer* buffer, size_t length, size_t offset, size_t offset_other);
59 void copy_from(const void* source, size_t length);
60 void copy_from(const void* source, size_t length, size_t offset);
61
62 void copy_to(Buffer* buffer);
63 void copy_to(Buffer* buffer, size_t length);
64 void copy_to(Buffer* buffer, size_t length, size_t offset);
65 void copy_to(Buffer* buffer, size_t length, size_t offset, size_t offset_other);
66 void copy_to(void* destination, size_t length);
67 void copy_to(void* destination, size_t length, size_t offset);
68 };
69
70 typedef Buffer buffer_t;
71
72}
73
74
75#endif //MAXOS_COMMON_BUFFER_H
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
void write(uint8_t byte)
Safely writes a byte to the buffer at the current offset.
Definition buffer.cpp:140
~Buffer()
Destroy the buffer, freeing the memory.
Definition buffer.cpp:48
void full(uint8_t byte, size_t offset=0, size_t amount=0)
Full the buffer with a specified byte at an offset.
Definition buffer.cpp:78
void set_offset(size_t offset)
Set the offset for where operations should begin from.
Definition buffer.cpp:128
size_t capacity() const
The max bytes the buffer can currently store.
Definition buffer.cpp:95
uint8_t * raw() const
The raw pointer to the bytes stored in memory, use is not recommended.
Definition buffer.cpp:57
uint8_t read()
Safely reads a byte from the buffer at the current offset.
Definition buffer.cpp:168
bool update_offset
Should operations write/read/copy update the offset in the buffer?
Definition buffer.h:44
void copy_to(Buffer *buffer)
Copies all the bytes from this buffer into another buffer.
Definition buffer.cpp:282
void copy_from(const Buffer *buffer)
Copies all the bytes from another buffer into this buffer.
Definition buffer.cpp:195
void resize(size_t size)
Grow the buffer to fit a new size.
Definition buffer.cpp:104
void clear()
Fulls the buffer with 0's and resets the offset.
Definition buffer.cpp:65
Defines a Logger class for logging messages with different severity levels to multiple output streams...
Defines classes for memory input and output operations of various bit widths (8, 16,...