diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-05-23 15:20:57 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-05-23 15:20:57 +0200 |
commit | 1027c42352ee49606d72109bd890b4d8f7f7a88c (patch) | |
tree | 7f08f257ca5fb8badf0542d4a8fcf53987ae08f8 /src/server/shared | |
parent | 85e621f01adf84ee66e20f8b75c93be7b4940cb2 (diff) |
Core/PacketIO: Replace static_asserts with concepts and add them to PacketUtilities as well
Diffstat (limited to 'src/server/shared')
-rw-r--r-- | src/server/shared/Packets/ByteBuffer.cpp | 1 | ||||
-rw-r--r-- | src/server/shared/Packets/ByteBuffer.h | 70 |
2 files changed, 31 insertions, 40 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.cpp b/src/server/shared/Packets/ByteBuffer.cpp index c8980a93fe4..f5d6a5ab4b5 100644 --- a/src/server/shared/Packets/ByteBuffer.cpp +++ b/src/server/shared/Packets/ByteBuffer.cpp @@ -198,6 +198,7 @@ void ByteBuffer::OnInvalidPosition(size_t pos, size_t valueSize) const throw ByteBufferPositionException(pos, _storage.size(), valueSize); } +template TC_SHARED_API char ByteBuffer::read<char>(); template TC_SHARED_API uint8 ByteBuffer::read<uint8>(); template TC_SHARED_API uint16 ByteBuffer::read<uint16>(); template TC_SHARED_API uint32 ByteBuffer::read<uint32>(); diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h index 94c22206aa0..df6fc935a58 100644 --- a/src/server/shared/Packets/ByteBuffer.h +++ b/src/server/shared/Packets/ByteBuffer.h @@ -18,8 +18,9 @@ #ifndef TRINITYCORE_BYTE_BUFFER_H #define TRINITYCORE_BYTE_BUFFER_H -#include "Define.h" #include "ByteConverter.h" +#include "Concepts.h" +#include "Define.h" #include <array> #include <string> #include <vector> @@ -50,6 +51,14 @@ public: ByteBufferInvalidValueException(char const* type, std::string_view value); }; +template <typename T> +concept ByteBufferNumeric = std::same_as<T, int8> || std::same_as<T, uint8> + || std::same_as<T, int16> || std::same_as<T, uint16> + || std::same_as<T, int32> || std::same_as<T, uint32> + || std::same_as<T, int64> || std::same_as<T, uint64> + || std::same_as<T, float> || std::same_as<T, double> + || std::same_as<T, char> || std::is_enum_v<T>; + class TC_SHARED_API ByteBuffer { public: @@ -117,10 +126,9 @@ class TC_SHARED_API ByteBuffer _storage.clear(); } - template <typename T> + template <ByteBufferNumeric T> void append(T value) { - static_assert(std::is_trivially_copyable_v<T>, "append(T) must be used with trivially copyable types"); EndianConvert(value); append(reinterpret_cast<uint8 const*>(&value), sizeof(value)); } @@ -248,10 +256,9 @@ class TC_SHARED_API ByteBuffer return value; } - template <typename T> + template <ByteBufferNumeric T> void put(std::size_t pos, T value) { - static_assert(std::is_trivially_copyable_v<T>, "put(size_t, T) must be used with trivially copyable types"); EndianConvert(value); put(pos, reinterpret_cast<uint8 const*>(&value), sizeof(value)); } @@ -469,7 +476,7 @@ class TC_SHARED_API ByteBuffer return _wpos * 8 + 8 - _bitpos; } - template <typename T> + template <ByteBufferNumeric T> void read_skip() { read_skip(sizeof(T)); } void read_skip(size_t skip) @@ -481,7 +488,7 @@ class TC_SHARED_API ByteBuffer _rpos += skip; } - template <typename T> + template <ByteBufferNumeric T> T read() { ResetBitPos(); @@ -490,7 +497,7 @@ class TC_SHARED_API ByteBuffer return r; } - template <typename T> + template <ByteBufferNumeric T> T read(size_t pos) const { if (pos + sizeof(T) > _storage.size()) @@ -502,7 +509,7 @@ class TC_SHARED_API ByteBuffer return val; } - template <typename T> + template <ByteBufferNumeric T> void read(T* dest, size_t count) { static_assert(std::is_trivially_copyable_v<T>, "read(T*, size_t) must be used with trivially copyable types"); @@ -523,8 +530,8 @@ class TC_SHARED_API ByteBuffer _rpos += len; } - template <size_t Size> - void read(std::array<uint8, Size>& arr) + template <ByteBufferNumeric T, size_t Size> + void read(std::array<T, Size>& arr) { read(arr.data(), Size); } @@ -549,6 +556,8 @@ class TC_SHARED_API ByteBuffer append(str, len); } + void ReadSkipCString(bool requireValidUtf8 = true) { (void)ReadCString(requireValidUtf8); } + std::string_view ReadCString(bool requireValidUtf8 = true); std::string_view ReadString(uint32 length, bool requireValidUtf8 = true); @@ -563,12 +572,12 @@ class TC_SHARED_API ByteBuffer { _storage.resize(newsize, 0); _rpos = 0; - _wpos = size(); + _wpos = _storage.size(); } void reserve(size_t ressize) { - if (ressize > size()) + if (ressize > _storage.size()) _storage.reserve(ressize); } @@ -577,7 +586,7 @@ class TC_SHARED_API ByteBuffer _storage.shrink_to_fit(); } - template <typename T> + template <ByteBufferNumeric T> void append(T const* src, size_t cnt) { #if TRINITY_ENDIAN == TRINITY_LITTLEENDIAN @@ -596,8 +605,8 @@ class TC_SHARED_API ByteBuffer append(buffer.data(), buffer.size()); } - template <size_t Size> - void append(std::array<uint8, Size> const& arr) + template <ByteBufferNumeric T, std::size_t Size> + void append(std::array<T, Size> const& arr) { append(arr.data(), Size); } @@ -619,30 +628,7 @@ class TC_SHARED_API ByteBuffer std::vector<uint8> _storage; }; -template <> -inline std::string ByteBuffer::read<std::string>() -{ - return std::string(ReadCString()); -} - -template <> -inline void ByteBuffer::read_skip<char*>() -{ - (void)ReadCString(); -} - -template <> -inline void ByteBuffer::read_skip<char const*>() -{ - read_skip<char*>(); -} - -template <> -inline void ByteBuffer::read_skip<std::string>() -{ - read_skip<char*>(); -} - +extern template char ByteBuffer::read<char>(); extern template uint8 ByteBuffer::read<uint8>(); extern template uint16 ByteBuffer::read<uint16>(); extern template uint32 ByteBuffer::read<uint32>(); @@ -654,4 +640,8 @@ extern template int64 ByteBuffer::read<int64>(); extern template float ByteBuffer::read<float>(); extern template double ByteBuffer::read<double>(); +template <typename T> +concept HasByteBufferShiftOperators = requires(ByteBuffer& data, T const& value) { { data << value } -> std::convertible_to<ByteBuffer&>; } + && requires(ByteBuffer& data, T& value) { { data >> value } -> std::convertible_to<ByteBuffer&>; }; + #endif |