refactor bitfield io
This commit is contained in:
@@ -10,10 +10,11 @@
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sp/common/VarInt.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <sp/common/VarInt.h>
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace sp {
|
||||
|
||||
@@ -23,16 +24,18 @@ namespace sp {
|
||||
*/
|
||||
class DataBuffer {
|
||||
private:
|
||||
typedef std::vector<std::uint8_t> Data;
|
||||
using Data = std::vector<std::uint8_t>;
|
||||
|
||||
private:
|
||||
Data m_Buffer;
|
||||
std::size_t m_ReadOffset;
|
||||
|
||||
public:
|
||||
typedef Data::iterator iterator;
|
||||
typedef Data::const_iterator const_iterator;
|
||||
typedef Data::reference reference;
|
||||
typedef Data::const_reference const_reference;
|
||||
typedef Data::difference_type difference_type;
|
||||
using iterator = Data::iterator;
|
||||
using const_iterator = Data::const_iterator;
|
||||
using reference = Data::reference;
|
||||
using const_reference = Data::const_reference;
|
||||
using difference_type = Data::difference_type;
|
||||
|
||||
DataBuffer();
|
||||
DataBuffer(std::size_t a_InitialSize);
|
||||
@@ -46,21 +49,23 @@ class DataBuffer {
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer
|
||||
* \warning No endian checks
|
||||
*/
|
||||
template <typename T>
|
||||
void Append(const T& data) {
|
||||
std::size_t size = sizeof(data);
|
||||
void Append(const T& a_Data) {
|
||||
std::size_t size = sizeof(a_Data);
|
||||
std::size_t end_pos = m_Buffer.size();
|
||||
m_Buffer.resize(m_Buffer.size() + size);
|
||||
std::memcpy(&m_Buffer[end_pos], &data, size);
|
||||
std::memcpy(&m_Buffer[end_pos], &a_Data, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer
|
||||
* \brief Append data to the buffer (converted to big endian)
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(const T& data) {
|
||||
Append(data);
|
||||
DataBuffer& operator<<(T a_Data) {
|
||||
SwapBytes(a_Data);
|
||||
Append(a_Data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -115,14 +120,24 @@ class DataBuffer {
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read data into a_Data
|
||||
* \warning No endian checks
|
||||
*/
|
||||
template <typename T>
|
||||
void Read(T& a_Data) {
|
||||
assert(m_ReadOffset + sizeof(T) <= GetSize());
|
||||
std::memcpy(&a_Data, m_Buffer.data() + m_ReadOffset, sizeof(T));
|
||||
m_ReadOffset += sizeof(T);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to desired variable
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(T& data) {
|
||||
assert(m_ReadOffset + sizeof(T) <= GetSize());
|
||||
data = *(reinterpret_cast<T*>(&m_Buffer[m_ReadOffset]));
|
||||
m_ReadOffset += sizeof(T);
|
||||
DataBuffer& operator>>(T& a_Data) {
|
||||
Read(a_Data);
|
||||
SwapBytes(a_Data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user