Files
Persson-dev 0d26879152
All checks were successful
Linux arm64 / Build (push) Successful in 16s
feat: add streams
2025-06-26 19:17:52 +02:00

66 lines
1.4 KiB
C++

#pragma once
/**
* \file VarInt.h
* \brief File containing the sp::VarInt class
*/
#include <cstddef>
#include <cstdint>
#include <functional>
namespace sp {
class DataBuffer;
using ReadFunc = std::function<void(std::uint8_t&)>;
/**
* \class VarInt
* \brief Variable-length format such that smaller numbers use fewer bytes.
*/
class VarInt {
private:
std::uint64_t m_Value;
public:
static const std::uint64_t MAX_VALUE = static_cast<std::uint64_t>(-1) >> 8;
VarInt() : m_Value(0) {}
/**
* \brief Construct a variable integer from a value
* \param value The value of the variable integer
*/
VarInt(std::uint64_t value) : m_Value(value) {}
/**
* \brief Get the value of the variable integer
*/
std::uint64_t GetValue() const {
return m_Value;
}
/**
* \brief Get the length of the serialized variable integer
*/
std::size_t GetSerializedLength() const;
/**
* \brief Serialize the variable integer
* \param out The buffer to write the serialized variable integer to
* \param var The variable integer to serialize
*/
friend DataBuffer& operator<<(DataBuffer& out, const VarInt& var);
/**
* \brief Deserialize the variable integer
* \param in The buffer to read the serialized variable integer from
* \param var The variable integer to deserialize
*/
friend DataBuffer& operator>>(DataBuffer& in, VarInt& var);
void Read(const ReadFunc& read);
};
} // namespace sp