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 const_cast<Container&
>(size.Value).resize(temp);
208 template<AsWritable Underlying, ContainerWritable<Underlying> Container>
211 template<AsWritable Underlying, ContainerReadable<Underlying> Container>
214 template<u
int32 BitCount, ContainerWritable<u
int32> Container>
226 template<u
int32 BitCount, ContainerReadable<u
int32> Container>
231 const_cast<Container&
>(size.Value).resize(data.
ReadBits(BitCount));
236 template<u
int32 BitCount, ContainerWritable<u
int32> Container>
239 template<u
int32 BitCount, ContainerReadable<u
int32> Container>
244 &&
requires(
ByteBuffer& data, T
const& string) { data.
WriteString(
static_cast<std::string_view
>(
string)); };
248 && !std::is_const_v<T>
249 && (
requires(T& container) { container.resize(
uint32()); } || std::same_as<T, std::string_view>)
252 namespace SizedString
254 template<u
int32 BitCount, StringWritable Container>
266 template<u
int32 BitCount, StringReadable Container>
272 if (
size_t rpos = data.
rpos(); length > data.
size() - rpos)
275 if constexpr (std::is_same_v<Container, std::string_view>)
277 const_cast<std::string_view&
>(size.Value) = {
reinterpret_cast<char const*
>(data.
data()), length };
279 const_cast<Container&
>(size.Value).resize(length);
285 template<u
int32 BitCount, StringWritable Container>
288 template<u
int32 BitCount, StringReadable Container>
291 template<StringWritable Container>
303 template<StringReadable Container, Strings::Utf8Mode Mode>
310 const_cast<Container&
>(
string.Value) = data.
ReadString(
string.Value.length(),
IsUtf8());
315 template<Strings::Utf8Mode = Strings::Val
idUtf8, StringWritable Container>
318 template<Strings::Utf8Mode Mode = Strings::Val
idUtf8, StringReadable Container>
323 namespace SizedCString
325 template<u
int32 BitCount, StringWritable Container>
332 data.
WriteBits(
static_cast<uint32>(size.Value.length() + 1), BitCount);
337 template<u
int32 BitCount, StringReadable Container>
342 if (
uint32 bytesIncludingNullTerminator = data.
ReadBits(BitCount); bytesIncludingNullTerminator > 1)
344 uint32 length = bytesIncludingNullTerminator - 1;
345 if (
size_t rpos = data.
rpos(); length > data.
size() - rpos)
348 if constexpr (std::is_same_v<Container, std::string_view>)
350 const_cast<std::string_view&
>(size.Value) = {
reinterpret_cast<char const*
>(data.
data()), length };
352 const_cast<Container&
>(size.Value).resize(length);
358 template<u
int32 BitCount, StringWritable Container>
361 template<u
int32 BitCount, StringReadable Container>
364 template<StringWritable Container>
371 if (!
string.
Value.empty())
380 template<StringReadable Container, Strings::Utf8Mode Mode>
387 if (!
string.
Value.empty())
389 const_cast<Container&
>(
string.Value) = data.
ReadString(
string.Value.length(),
IsUtf8());
390 (void)data.
read<
char>();
396 template<Strings::Utf8Mode = Strings::Val
idUtf8, StringWritable Container>
399 template<Strings::Utf8Mode Mode = Strings::Val
idUtf8, StringReadable Container>
405 template<AsWritable Underlying>
412 data << static_cast<Underlying>(size.Value.size());
417 template<AsWritable Underlying>
425 if (
size_t rpos = data.
rpos(); temp > data.
size() - rpos)
429 const_cast<std::span<uint8>&
>(size.Value) = { data.
data(), temp };
434 template<AsWritable Underlying>
437 template<AsWritable Underlying>
440 template<u
int32 BitCount>
452 template<u
int32 BitCount>
458 if (
size_t rpos = data.
rpos(); length > data.
size() - rpos)
462 const_cast<std::span<uint8>&
>(size.Value) = { data.
data(), length };
467 template<u
int32 BitCount>
470 template<u
int32 BitCount>
489 const_cast<std::span<uint8>&
>(
string.Value) = data.
ReadBytes(
string.Value.size());
499 template <
typename T>
523 template <
typename T>
std::optional< T > Optional
Optional helper class to wrap optional values within.
uint32 ReadBits(int32 bits)
void WriteString(std::string const &str)
std::span< uint8 > ReadBytes(size_t length)
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< Underlying > Size(std::span< uint8 > const &value)
BitsSizeWriter< BitCount > BitsSize(std::span< uint8 > const &value)
SizeWriter< BitCount, Container > BitsSize(Container const &value)
SizeWriter< BitCount, Container > BitsSize(Container const &value)
constexpr IgnoredReaderWriter< T > Ignored
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, BitsSizeReaderWriter const &size)
std::span< uint8 > const & Value
friend ByteBuffer & operator<<(ByteBuffer &data, BitsSizeWriter const &size)
friend ByteBuffer & operator>>(ByteBuffer &data, DataReaderWriter const &string)
std::span< uint8 > const & Value
friend ByteBuffer & operator<<(ByteBuffer &data, DataWriter const &span)
friend ByteBuffer & operator>>(ByteBuffer &data, SizeReaderWriter const &size)
std::span< uint8 > const & Value
friend ByteBuffer & operator<<(ByteBuffer &data, SizeWriter const &size)
friend ByteBuffer & operator>>(ByteBuffer &data, IgnoredReaderWriter const &)
friend ByteBuffer & operator<<(ByteBuffer &data, IgnoredReaderWriter const &)
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)