initial import from picomap firmware
This commit is contained in:
32
include/parse_buffer.h
Normal file
32
include/parse_buffer.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
class parse_buffer {
|
||||
const uint8_t* m_data;
|
||||
size_t m_remaining;
|
||||
|
||||
public:
|
||||
parse_buffer(std::span<const uint8_t> data)
|
||||
: m_data(data.data()), m_remaining(data.size()) {}
|
||||
|
||||
template <typename T>
|
||||
const T* consume() {
|
||||
if (m_remaining < sizeof(T)) return nullptr;
|
||||
auto* p = reinterpret_cast<const T*>(m_data);
|
||||
m_data += sizeof(T);
|
||||
m_remaining -= sizeof(T);
|
||||
return p;
|
||||
}
|
||||
|
||||
bool skip(size_t len) {
|
||||
if (m_remaining < len) return false;
|
||||
m_data += len;
|
||||
m_remaining -= len;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::span<const uint8_t> remaining() const { return {m_data, m_remaining}; }
|
||||
size_t remaining_size() const { return m_remaining; }
|
||||
};
|
||||
Reference in New Issue
Block a user