mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-18 00:18:43 +01:00
Core/PacketIO: Added new packet reading/writing utilities for type casting, optionals and bits
(cherry picked from commit f41ab921b9)
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "ByteBuffer.h"
|
||||
#include "Duration.h"
|
||||
#include "Optional.h"
|
||||
#include "Tuples.h"
|
||||
#include <short_alloc/short_alloc.h>
|
||||
#include <string_view>
|
||||
@@ -317,6 +318,121 @@ namespace WorldPackets
|
||||
private:
|
||||
ChronoDuration _value = ChronoDuration::zero();
|
||||
};
|
||||
|
||||
template<typename Underlying, typename T>
|
||||
struct AsWriter
|
||||
{
|
||||
T const& Value;
|
||||
|
||||
friend inline ByteBuffer& operator<<(ByteBuffer& data, AsWriter const& opt)
|
||||
{
|
||||
data << Underlying(opt.Value);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Underlying, typename T>
|
||||
struct AsReaderWriter : AsWriter<Underlying, T>
|
||||
{
|
||||
friend inline ByteBuffer& operator>>(ByteBuffer& data, AsReaderWriter const& opt)
|
||||
{
|
||||
Underlying temp;
|
||||
data >> temp;
|
||||
const_cast<T&>(opt.Value) = static_cast<T>(temp);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Underlying, typename T>
|
||||
inline AsWriter<Underlying, T> As(T const& value) { return { value }; }
|
||||
|
||||
template<typename Underlying, typename T>
|
||||
inline AsReaderWriter<Underlying, T> As(T& value) { return { value }; }
|
||||
|
||||
template<typename T>
|
||||
struct OptionalInitWriter
|
||||
{
|
||||
Optional<T> const& Opt;
|
||||
|
||||
friend inline ByteBuffer& operator<<(ByteBuffer& data, OptionalInitWriter const& opt)
|
||||
{
|
||||
data.WriteBit(opt.Opt.has_value());
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct OptionalInitReaderWriter : OptionalInitWriter<T>
|
||||
{
|
||||
friend inline ByteBuffer& operator>>(ByteBuffer& data, OptionalInitReaderWriter const& opt)
|
||||
{
|
||||
if (data.ReadBit())
|
||||
const_cast<Optional<T>&>(opt.Opt).emplace();
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline OptionalInitWriter<T> OptionalInit(Optional<T> const& value) { return { value }; }
|
||||
|
||||
template<typename T>
|
||||
inline OptionalInitReaderWriter<T> OptionalInit(Optional<T>& value) { return { value }; }
|
||||
|
||||
template<uint32 BitCount, typename T>
|
||||
struct BitsWriter
|
||||
{
|
||||
T const& Value;
|
||||
|
||||
friend inline ByteBuffer& operator<<(ByteBuffer& data, BitsWriter const& bits)
|
||||
{
|
||||
data.WriteBits(static_cast<uint32>(bits.Value), BitCount);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<uint32 BitCount, typename T>
|
||||
struct BitsReaderWriter : BitsWriter<BitCount, T>
|
||||
{
|
||||
friend inline ByteBuffer& operator>>(ByteBuffer& data, BitsReaderWriter const& bits)
|
||||
{
|
||||
const_cast<T&>(bits.Value) = static_cast<T>(data.ReadBits(BitCount));
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<uint32 BitCount, typename T>
|
||||
inline BitsWriter<BitCount, T> Bits(T const& value) { return { value }; }
|
||||
|
||||
template<uint32 BitCount, typename T>
|
||||
inline BitsReaderWriter<BitCount, T> Bits(T& value) { return { value }; }
|
||||
|
||||
template<uint32 BitCount, typename Container>
|
||||
struct BitsSizeWriter
|
||||
{
|
||||
Container const& Value;
|
||||
|
||||
friend inline ByteBuffer& operator<<(ByteBuffer& data, BitsSizeWriter const& bits)
|
||||
{
|
||||
data.WriteBits(static_cast<uint32>(bits.Value.size()), BitCount);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<uint32 BitCount, typename Container>
|
||||
struct BitsSizeReaderWriter : BitsSizeWriter<BitCount, Container>
|
||||
{
|
||||
friend inline ByteBuffer& operator>>(ByteBuffer& data, BitsSizeReaderWriter const& bits)
|
||||
{
|
||||
const_cast<Container&>(bits.Value).resize(data.ReadBits(BitCount));
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
template<uint32 BitCount, typename Container>
|
||||
inline BitsSizeWriter<BitCount, Container> BitsSize(Container const& value) { return { value }; }
|
||||
|
||||
template<uint32 BitCount, typename Container>
|
||||
inline BitsSizeReaderWriter<BitCount, Container> BitsSize(Container& value) { return { value }; }
|
||||
}
|
||||
|
||||
#endif // PacketUtilities_h__
|
||||
|
||||
Reference in New Issue
Block a user