18#ifndef TRINITYCORE_PACKET_OPERATORS_H
19#define TRINITYCORE_PACKET_OPERATORS_H
39 template<
typename T,
typename Underlying>
40 concept AsWritableFor =
requires {
static_cast<Underlying
>(std::declval<T>()); }
41 &&
requires {
static_cast<T
>(Underlying()); };
43 template<AsWritable Underlying, AsWritableFor<Underlying> T>
50 data << static_cast<Underlying>(as.
Value);
55 template<AsWritable Underlying, AsWritableFor<Underlying> T>
62 const_cast<T&
>(as.
Value) =
static_cast<T
>(temp);
67 template<AsWritable Underlying, AsWritableFor<Underlying> T>
70 template<AsWritable Underlying, AsWritableFor<Underlying> T>
76 template<OptionalWritable T>
88 template<OptionalWritable T>
99 template<OptionalWritable T>
102 template<OptionalWritable T>
105 template<OptionalWritable T>
108 std::unique_ptr<T>
const&
Ptr;
117 template<OptionalWritable T>
123 const_cast<std::unique_ptr<T>&
>(opt.
Ptr) = std::make_unique<T>();
128 template<OptionalWritable T>
131 template<OptionalWritable T>
137 template<u
int32 BitCount, BitsWritable T>
144 if constexpr (BitCount != 1)
153 template<u
int32 BitCount, BitsWritable T>
158 if constexpr (BitCount != 1)
159 const_cast<T&
>(bits.
Value) =
static_cast<T
>(data.
ReadBits(BitCount));
161 const_cast<T&
>(bits.
Value) =
static_cast<T
>(data.
ReadBit() ? 1 : 0);
167 template<u
int32 BitCount, BitsWritable T>
170 template<u
int32 BitCount, BitsWritable T>
173 template<
typename T,
typename SizeType>
175 && !std::same_as<T, std::string_view>
176 && !std::same_as<T, std::string>;
178 template<
typename T,
typename SizeType>
180 && !std::is_const_v<T>
181 &&
requires(T & container) { container.resize(SizeType{}); };
183 template<AsWritable Underlying, ContainerWritable<Underlying> Container>
190 data << static_cast<Underlying>(size.Value.size());
195 template<AsWritable Underlying, ContainerReadable<Underlying> Container>
203 if constexpr (std::is_same_v<Container, std::string> || std::is_same_v<Container, std::string_view>)
204 if (
size_t rpos = data.
rpos(); temp > data.
size() - rpos)
207 if constexpr (std::is_same_v<std::remove_cvref_t<Container>, std::string_view>)
209 const_cast<std::string_view&
>(size.Value) = {
reinterpret_cast<char const*
>(data.
data()), temp };
211 const_cast<Container&
>(size.Value).resize(temp);
217 template<AsWritable Underlying, ContainerWritable<Underlying> Container>
220 template<AsWritable Underlying, ContainerReadable<Underlying> Container>
223 template<u
int32 BitCount, ContainerWritable<u
int32> Container>
235 template<u
int32 BitCount, ContainerReadable<u
int32> Container>
240 const_cast<Container&
>(size.Value).resize(data.
ReadBits(BitCount));
245 template<u
int32 BitCount, ContainerWritable<u
int32> Container>
248 template<u
int32 BitCount, ContainerReadable<u
int32> Container>
253 &&
requires(
ByteBuffer& data, T
const& string) { data.
WriteString(
static_cast<std::string_view
>(
string)); };
257 && !std::is_const_v<T>
258 && (
requires(T& container) { container.resize(
uint32()); } || std::same_as<T, std::string_view>)
261 namespace SizedString
263 template<u
int32 BitCount, StringWritable Container>
275 template<u
int32 BitCount, StringReadable Container>
281 if (
size_t rpos = data.
rpos(); length > data.
size() - rpos)
284 if constexpr (std::is_same_v<Container, std::string_view>)
286 const_cast<std::string_view&
>(size.Value) = {
reinterpret_cast<char const*
>(data.
data()), length };
288 const_cast<Container&
>(size.Value).resize(length);
294 template<u
int32 BitCount, StringWritable Container>
297 template<u
int32 BitCount, StringReadable Container>
300 template<StringWritable Container>
312 template<StringReadable Container, Strings::Utf8Mode Mode>
319 const_cast<Container&
>(
string.Value) = data.
ReadString(
string.Value.length(),
IsUtf8());
324 template<Strings::Utf8Mode = Strings::Val
idUtf8, StringWritable Container>
327 template<Strings::Utf8Mode Mode = Strings::Val
idUtf8, StringReadable Container>
332 namespace SizedCString
334 template<u
int32 BitCount, StringWritable Container>
341 data.
WriteBits(
static_cast<uint32>(size.Value.length() + 1), BitCount);
346 template<u
int32 BitCount, StringReadable Container>
351 if (
uint32 bytesIncludingNullTerminator = data.
ReadBits(BitCount); bytesIncludingNullTerminator > 1)
353 uint32 length = bytesIncludingNullTerminator - 1;
354 if (
size_t rpos = data.
rpos(); length > data.
size() - rpos)
357 if constexpr (std::is_same_v<Container, std::string_view>)
359 const_cast<std::string_view&
>(size.Value) = {
reinterpret_cast<char const*
>(data.
data()), length };
361 const_cast<Container&
>(size.Value).resize(length);
367 template<u
int32 BitCount, StringWritable Container>
370 template<u
int32 BitCount, StringReadable Container>
373 template<StringWritable Container>
380 if (!
string.
Value.empty())
389 template<StringReadable Container, Strings::Utf8Mode Mode>
396 if (!
string.
Value.empty())
398 const_cast<Container&
>(
string.Value) = data.
ReadString(
string.Value.length(),
IsUtf8());
399 (void)data.
read<
char>();
405 template<Strings::Utf8Mode = Strings::Val
idUtf8, StringWritable Container>
408 template<Strings::Utf8Mode Mode = Strings::Val
idUtf8, StringReadable Container>
std::optional< T > Optional
Optional helper class to wrap optional values within.
uint32 ReadBits(int32 bits)
void WriteString(std::string const &str)
void OnInvalidPosition(size_t pos, size_t valueSize) const
std::string_view ReadString(uint32 length, bool requireValidUtf8=true)
void WriteBits(uint64 value, int32 bits)
SizeWriter< BitCount, Container > BitsSize(Container const &value)
SizeWriter< BitCount, Container > BitsSize(Container const &value)
SizeWriter< Underlying, Container > Size(Container const &value)
OptionalInitWriter< T > OptionalInit(Optional< T > const &value)
AsWriter< Underlying, T > As(T const &value)
BitsWriter< BitCount, T > Bits(T const &value)
BitsSizeWriter< BitCount, Container > BitsSize(Container const &value)
friend ByteBuffer & operator>>(ByteBuffer &data, AsReaderWriter const &as)
friend ByteBuffer & operator<<(ByteBuffer &data, AsWriter const &as)
friend ByteBuffer & operator>>(ByteBuffer &data, BitsReaderWriter const &bits)
friend ByteBuffer & operator>>(ByteBuffer &data, BitsSizeReaderWriter const &size)
friend ByteBuffer & operator<<(ByteBuffer &data, BitsSizeWriter const &size)
friend ByteBuffer & operator<<(ByteBuffer &data, BitsWriter const &bits)
friend ByteBuffer & operator>>(ByteBuffer &data, OptionalInitReaderWriter const &opt)
friend ByteBuffer & operator<<(ByteBuffer &data, OptionalInitWriter const &opt)
Optional< T > const & Opt
friend ByteBuffer & operator>>(ByteBuffer &data, PtrInitReaderWriter const &opt)
friend ByteBuffer & operator<<(ByteBuffer &data, PtrInitWriter const &opt)
std::unique_ptr< T > const & Ptr
friend ByteBuffer & operator>>(ByteBuffer &data, SizeReaderWriter const &size)
friend ByteBuffer & operator<<(ByteBuffer &data, SizeWriter const &size)
friend ByteBuffer & operator>>(ByteBuffer &data, DataReaderWriter const &string)
static constexpr bool IsUtf8()
friend ByteBuffer & operator<<(ByteBuffer &data, DataWriter const &string)
friend ByteBuffer & operator>>(ByteBuffer &data, SizeReaderWriter const &size)
friend ByteBuffer & operator<<(ByteBuffer &data, SizeWriter const &size)
friend ByteBuffer & operator>>(ByteBuffer &data, DataReaderWriter const &string)
static constexpr bool IsUtf8()
friend ByteBuffer & operator<<(ByteBuffer &data, DataWriter const &string)
friend ByteBuffer & operator>>(ByteBuffer &data, SizeReaderWriter const &size)
friend ByteBuffer & operator<<(ByteBuffer &data, SizeWriter const &size)