Files
Simple-Protocol-Lib/include/sp/common/VarInt.h
2025-03-04 11:44:40 +01:00

67 lines
1.4 KiB
C++

#pragma once
/**
* \file VarInt.h
* \brief File containing the sp::VarInt class
*/
#include <cstddef>
#include <cstdint>
#include <ostream>
namespace sp {
class DataBuffer;
/**
* \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);
/**
* \brief overriding stream operator
*/
friend std::ostream& operator<<(std::ostream& a_Stream, const sp::VarInt& a_VarInt);
};
} // namespace sp