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