18#ifndef PacketUtilities_h__
19#define PacketUtilities_h__
25#include <short_alloc/short_alloc.h>
64 template<std::
size_t MaxBytesWithoutNullTerminator>
65 struct ByteSize {
static bool Validate(std::string_view value) {
return value.size() <= MaxBytesWithoutNullTerminator; } };
74 template<std::size_t MaxBytesWithoutNullTerminator,
typename... Validators>
78 std::tuple<Strings::ByteSize<MaxBytesWithoutNullTerminator>,
Strings::Utf8, Validators...>,
79 std::tuple<Strings::ByteSize<MaxBytesWithoutNullTerminator>, Validators...>>;
86 operator std::string_view()
const {
return _storage; }
88 operator std::string
const&()
const {
return _storage; }
121 return *
this = std::string_view(value);
127 return ValidateNth(value, std::make_index_sequence<std::tuple_size_v<ValidatorList>>{});
130 template<std::size_t... indexes>
131 static bool ValidateNth(std::string_view value, std::index_sequence<indexes...>)
133 return (std::tuple_element_t<indexes, ValidatorList>::Validate(value) && ...);
148 template<
typename T, std::
size_t N>
152 using allocator_type = short_alloc::short_alloc<T, (N *
sizeof(T) + (
alignof(std::max_align_t) - 1)) & ~(
alignof(std::max_align_t) - 1)>;
161 using pointer =
typename storage_type::pointer;
172 for (T
const& element : other)
184 for (T
const& element : other)
209 if (newSize > max_capacity::value)
217 if (
_storage.size() >= max_capacity::value)
225 if (
_storage.size() >= max_capacity::value)
228 _storage.push_back(std::forward<value_type>(value));
231 template<
typename... Args>
234 _storage.emplace_back(std::forward<Args>(args)...);
253 template<
typename Underlying =
int64>
269 _value = std::chrono::system_clock::to_time_t(systemTime);
273 operator time_t()
const
280 return static_cast<Underlying
>(
_value);
285 data << static_cast<Underlying>(timestamp.
_value);
291 timestamp.
_value =
static_cast<time_t
>(data.
read<Underlying>());
299 template<
typename ChronoDuration,
typename Underlying =
int64>
312 operator ChronoDuration()
const
319 data << static_cast<Underlying>(duration.
_value.count());
325 duration.
_value = ChronoDuration(data.
read<Underlying>());
330 ChronoDuration
_value = ChronoDuration::zero();
333 template<
typename Underlying,
typename T>
340 data << Underlying(opt.
Value);
345 template<
typename Underlying,
typename T>
352 const_cast<T&
>(opt.
Value) =
static_cast<T
>(temp);
357 template<
typename Underlying,
typename T>
360 template<
typename Underlying,
typename T>
395 std::unique_ptr<T>
const&
Ptr;
410 const_cast<std::unique_ptr<T>&
>(opt.
Ptr) = std::make_unique<T>();
421 template<u
int32 BitCount,
typename T>
433 template<u
int32 BitCount,
typename T>
438 const_cast<T&
>(bits.
Value) =
static_cast<T
>(data.
ReadBits(BitCount));
443 template<u
int32 BitCount,
typename T>
446 template<u
int32 BitCount,
typename T>
449 template<u
int32 BitCount,
typename Container>
461 template<u
int32 BitCount,
typename Container>
466 const_cast<Container&
>(bits.
Value).resize(data.
ReadBits(BitCount));
471 template<u
int32 BitCount,
typename Container>
474 template<u
int32 BitCount,
typename Container>
477 namespace SizedString
479 template<u
int32 BitCount,
typename Container>
482 template<u
int32 BitCount,
typename Container>
485 template<
typename Container>
497 template<
typename Container>
502 const_cast<Container&
>(
string.Value) = data.
ReadString(
string.Value.length());
507 template<
typename Container>
510 template<
typename Container>
515 namespace SizedCString
517 template<u
int32 BitCount,
typename Container>
529 template<u
int32 BitCount,
typename Container>
534 if (
uint32 bytesIncludingNullTerminator = data.
ReadBits(BitCount); bytesIncludingNullTerminator > 1)
535 const_cast<Container&
>(bits.
Value).resize(bytesIncludingNullTerminator - 1);
540 template<u
int32 BitCount,
typename Container>
543 template<u
int32 BitCount,
typename Container>
546 template<
typename Container>
553 if (!
string.
Value.empty())
554 data <<
string.Value;
559 template<
typename Container>
564 const_cast<Container&
>(
string.Value) = data.
ReadString(
string.Value.length());
570 template<
typename Container>
573 template<
typename Container>
std::chrono::system_clock::time_point SystemTimePoint
std::optional< T > Optional
Optional helper class to wrap optional values within.
uint32 ReadBits(int32 bits)
void WriteString(std::string const &str)
std::string_view ReadCString(bool requireValidUtf8=true)
std::string_view ReadString(uint32 length, bool requireValidUtf8=true)
void WriteBits(uint64 value, int32 bits)
typename storage_type::iterator iterator
short_alloc::short_alloc< T,(N *sizeof(T)+(alignof(std::max_align_t) - 1)) &~(alignof(std::max_align_t) - 1)> allocator_type
const_pointer data() const
iterator erase(const_iterator first, const_iterator last)
T & emplace_back(Args &&... args)
Array & operator=(Array &&other) noexcept=delete
std::vector< T, allocator_type > storage_type
typename storage_type::const_pointer const_pointer
typename storage_type::const_reference const_reference
void push_back(value_type const &value)
const_iterator end() const
typename storage_type::reference reference
typename storage_type::size_type size_type
Array & operator=(Array const &other)
typename storage_type::value_type value_type
const_reference operator[](size_type i) const
void push_back(value_type &&value)
Array(Array &&other) noexcept=delete
typename storage_type::const_iterator const_iterator
typename storage_type::pointer pointer
std::integral_constant< std::size_t, N > max_capacity
typename allocator_type::arena_type arena_type
reference operator[](size_type i)
const_iterator begin() const
void resize(size_type newSize)
Array(Array const &other)
Duration & operator=(ChronoDuration value)
friend ByteBuffer & operator>>(ByteBuffer &data, Duration &duration)
Duration(ChronoDuration value)
friend ByteBuffer & operator<<(ByteBuffer &data, Duration duration)
IllegalHyperlinkException(std::string_view value)
InvalidHyperlinkException(std::string_view value)
InvalidStringValueException(std::string_view value)
std::string const & GetInvalidValue() const
InvalidUtf8ValueException(std::string_view value)
PacketArrayMaxCapacityException(std::size_t requestedSize, std::size_t sizeLimit)
char const * c_str() const
static bool Validate(std::string_view value)
std::conditional_t<!Trinity::has_type< Strings::RawBytes, std::tuple< Validators... > >::value, std::tuple< Strings::ByteSize< MaxBytesWithoutNullTerminator >, Strings::Utf8, Validators... >, std::tuple< Strings::ByteSize< MaxBytesWithoutNullTerminator >, Validators... > > ValidatorList
String & operator=(std::string const &value)
String & operator=(std::string_view value)
std::size_t length() const
String & operator=(std::string &&value)
static bool ValidateNth(std::string_view value, std::index_sequence< indexes... >)
friend ByteBuffer & operator>>(ByteBuffer &data, String &value)
String & operator=(char const *value)
Underlying AsUnderlyingType() const
Timestamp & operator=(time_t value)
friend ByteBuffer & operator<<(ByteBuffer &data, Timestamp timestamp)
Timestamp & operator=(SystemTimePoint const &systemTime)
friend ByteBuffer & operator>>(ByteBuffer &data, Timestamp ×tamp)
Timestamp(SystemTimePoint const &systemTime)
DataWriter< Container > Data(Container const &value)
SizeWriter< BitCount, Container > BitsSize(Container const &value)
SizedStringWriter< Container > Data(Container const &value)
BitsSizeWriter< BitCount, Container > BitsSize(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 &opt)
friend ByteBuffer & operator<<(ByteBuffer &data, AsWriter const &opt)
friend ByteBuffer & operator>>(ByteBuffer &data, BitsReaderWriter const &bits)
friend ByteBuffer & operator>>(ByteBuffer &data, BitsSizeReaderWriter const &bits)
friend ByteBuffer & operator<<(ByteBuffer &data, BitsSizeWriter const &bits)
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, DataReaderWriter const &string)
friend ByteBuffer & operator<<(ByteBuffer &data, DataWriter const &string)
friend ByteBuffer & operator>>(ByteBuffer &data, SizeReaderWriter const &bits)
friend ByteBuffer & operator<<(ByteBuffer &data, SizeWriter const &bits)
friend ByteBuffer & operator>>(ByteBuffer &data, SizedStringReaderWriter const &string)
friend ByteBuffer & operator<<(ByteBuffer &data, SizedStringWriter const &string)
static bool Validate(std::string_view value)
static bool Validate(std::string_view value)
static bool Validate(std::string_view value)
static bool Validate(std::string_view)
static bool Validate(std::string_view value)