AdsLib
Loading...
Searching...
No Matches
Frame.h
1// SPDX-License-Identifier: MIT
6#pragma once
7
8#include "wrap_endian.h"
9#include <memory>
10
11struct Frame {
17 Frame(size_t length, const void* data = nullptr);
18
25 uint8_t operator[](size_t index) const;
26
31 size_t capacity() const;
32
38 Frame& clear();
39
47 Frame& limit(size_t newSize);
48
55 Frame& reset(size_t newSize = 4096);
56
61 const uint8_t* data() const;
62
68 template<typename T> T pop()
69 {
70 T value {};
71 if (sizeof(value) <= capacity()) {
72 value = *reinterpret_cast<T*>(m_Pos);
73 }
74 remove(sizeof(T));
75 return value;
76 }
77
82 template<typename T> T pop_letoh()
83 {
84 return bhf::ads::letoh(pop<T>());
85 }
86
94 Frame& prepend(const void* const data, const size_t size);
95
99 template<class T> Frame& prepend(const T& header)
100 {
101 return prepend(&header, sizeof(T));
102 }
103
109 uint8_t* rawData() const;
110
117 Frame& remove(size_t numBytes);
118
122 template<class T> T remove()
123 {
124 const auto p = m_Pos;
125 remove(sizeof(T));
126 return T(p);
127 }
128
133 size_t size() const;
134
135private:
136 std::unique_ptr<uint8_t[]> m_Data;
137 uint8_t* m_Pos;
138 size_t m_Size;
139 size_t m_OriginalSize;
140};
Definition: Frame.h:11
const uint8_t * data() const
data
Definition: Frame.cpp:41
Frame & reset(size_t newSize=4096)
reset Prepare the frame to be reused as a response buffer
Definition: Frame.cpp:53
size_t size() const
size
Definition: Frame.cpp:100
Frame & clear()
clear Reset internal buffer to an empty frame
Definition: Frame.cpp:36
size_t capacity() const
capacity
Definition: Frame.cpp:31
T pop_letoh()
Definition: Frame.h:82
uint8_t operator[](size_t index) const
operator [] bytewise access to the frame bytes
Definition: Frame.cpp:26
T pop()
Definition: Frame.h:68
Frame & prepend(const void *const data, const size_t size)
prepend prepend <data> in front of the frame
Definition: Frame.cpp:70
Frame & prepend(const T &header)
Definition: Frame.h:99
Frame & limit(size_t newSize)
limit If the frame is reused as a response buffer, call limit() to lock the frames state and limit it...
Definition: Frame.cpp:46
T remove()
Definition: Frame.h:122
uint8_t * rawData() const
rawData Reuse the frame as response buffer, use with care!
Definition: Frame.cpp:89