Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ByteBuffer.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_BYTEBUFFER_HXX_
37#define _UTILS_BYTEBUFFER_HXX_
38
39#include "utils/Buffer.hxx"
40
42static constexpr unsigned RAWBUFFER_SIZE = 1024;
43
45extern Pool *rawBufferPool;
46
48struct RawData
49{
50 uint8_t payload[RAWBUFFER_SIZE];
52 static constexpr size_t MAX_SIZE = RAWBUFFER_SIZE;
53};
54
59
62
65{
69
71 uint8_t *data_ {nullptr};
72
74 size_t size_ {0};
75
77 size_t size() const
78 {
79 return size_;
80 }
81
85 void advance(size_t num_bytes)
86 {
87 HASSERT(num_bytes <= size_);
88 data_ += num_bytes;
89 size_ -= num_bytes;
90 }
91
99 void set_from(RawBufferPtr buf, size_t len, size_t ofs = 0)
100 {
101 ownedData_ = std::move(buf);
102 size_ = len;
103 data_ = ownedData_->data()->payload + ofs;
104 }
105
109 void set_from(const string *data)
110 {
111 ownedData_.reset(); // no need for this anymore
112 size_ = data->size();
113 data_ = (uint8_t *)data->data();
114 }
115
121 void set_from(const void *data, size_t len)
122 {
123 ownedData_.reset(); // no need for this anymore
124 data_ = (uint8_t *)data;
125 size_ = len;
126 }
127
134 size_t append(const void *data, size_t len)
135 {
136 HASSERT(ownedData_.get());
137 uint8_t *end = data_ + size_;
138 uint8_t *max_end = ownedData_->data()->payload + RawData::MAX_SIZE;
139 size_t max_len = max_end - end;
140 if (max_len < len)
141 {
142 len = max_len;
143 }
144 memcpy(end, data, len);
145 size_ += len;
146 return len;
147 }
148
152 uint8_t *append_ptr()
153 {
154 HASSERT(ownedData_.get());
155 uint8_t *end = data_ + size_;
156 return end;
157 }
158
163 void append_complete(size_t len)
164 {
165 size_ += len;
166 }
167
172 size_t free_space()
173 {
174 if (!ownedData_)
175 {
176 return 0;
177 }
178 uint8_t *end = data_ + size_;
179 uint8_t *max_end = ownedData_->data()->payload + RawData::MAX_SIZE;
180 size_t max_len = max_end - end;
181 return max_len;
182 }
183};
184
189
190template <class T> class FlowInterface;
191
194
195#endif // _UTILS_BYTEBUFFER_HXX_
AutoReleaseBuffer< T > BufferPtr
Smart pointer for buffers.
Definition Buffer.hxx:259
BufferPtr< RawData > RawBufferPtr
Holds a raw buffer.
Pool * rawBufferPool
Use this BufferPool to allocate raw buffers.
Definition Buffer.cxx:38
static constexpr unsigned RAWBUFFER_SIZE
This is how many bytes we have in each raw buffer allocation.
BufferPtr< ByteChunk > ByteBufferPtr
Buffer pointer type for references.
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
Abstract class for message recipients.
Pool of previously allocated, but currently unused, items.
Definition Buffer.hxx:278
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
Holds a reference to a raw buffer, with the start and size information.
void append_complete(size_t len)
Notifies that a certain number of bytes have been appended, i.e., written into append_ptr().
size_t append(const void *data, size_t len)
Adds more data to the end of the buffer.
size_t free_space()
size_t size_
How many bytes from data_ does this chunk represent.
void set_from(RawBufferPtr buf, size_t len, size_t ofs=0)
Overwrites this chunk from a raw buffer.
uint8_t * append_ptr()
void set_from(const void *data, size_t len)
Overwrites this chunk from an externally owned memory area.
size_t size() const
void advance(size_t num_bytes)
Moves forward the data beginning pointer by a given number of bytes.
uint8_t * data_
Points to the first byte of the useful data.
RawBufferPtr ownedData_
Owns a ref for a RawData buffer.
void set_from(const string *data)
Overwrites this chunk from a string.
Container for holding an arbitrary untyped data stream.
static constexpr size_t MAX_SIZE
Maximum length that can be stored in a single RawBuffer.