29#include <OnixS/CME/MDH/Utils.h>
39template <
class Collection,
class =
void>
42template <
class Collection>
44 typename Utils::MakeVoid<decltype(std::declval<Collection&>().data()),decltype(std::declval<Collection&>().size())>::type>
45 : std::integral_constant<bool,
46 (std::is_same<decltype(std::declval<Collection&>().data()), ByteSpan*>::value
47 || std::is_same<decltype(std::declval<Collection&>().data()), const ByteSpan*>::value)
48 && std::is_integral<typename std::remove_cv<decltype(std::declval<Collection&>().size())>::type>::value
49 && !std::is_same<typename std::remove_cv<decltype(std::declval<Collection&>().size())>::type, bool>::value>
52template <
class Collection>
59 throw std::runtime_error(
"Byte span collection count cannot be negative.");
66std::size_t
checkedSpanCount(Count suppliedCount, std::size_t maxSpansCount, std::size_t minSpansCount = 0)
68 typedef typename std::remove_cv<Count>::type CountType;
71 typedef typename std::common_type<CountType, std::size_t>::type Comparison;
72 const Comparison count =
static_cast<Comparison
>(suppliedCount);
74 if(count <
static_cast<Comparison
>(minSpansCount))
75 throw std::runtime_error(
76 "Byte span collection count is less than the minimum of " + std::to_string(minSpansCount) +
".");
78 if(count >
static_cast<Comparison
>(maxSpansCount))
79 throw std::runtime_error(
80 "Byte span collection count exceeds the maximum of " + std::to_string(maxSpansCount) +
".");
82 return static_cast<std::size_t
>(suppliedCount);
89 if(spansCount != 0 && spans ==
nullptr)
90 throw std::runtime_error(
"Byte span collection data cannot be null when count is nonzero.");
92 for(std::size_t i = 0; i < spansCount; ++i)
93 if(!spans[i].empty() && spans[i].data() ==
nullptr)
94 throw std::runtime_error(
"Byte span at index " + std::to_string(i) +
" has null data and nonzero size.");
97template <
class Collection>
100 const auto count =
checkedSpanCount(spans.size(), maxSpansCount, minSpansCount);
101 const auto data = spans.data();
103 return {data, count};
116 return ByteSpan(
reinterpret_cast<const ByteSpan::value_type*
>(&header),
sizeof(header));
128template <
typename MessageType,
size_t MaxMessageSize>
131 return holder.toNetworkBytes();
142template <
typename MessageType,
size_t MaxMessageSize>
145 return holder.toNetworkBytes();
#define ONIXS_CME_MDH_TESTING_NAMESPACE_END
#define ONIXS_CME_MDH_TESTING_NAMESPACE_BEGIN
Owns fixed-capacity storage for a prefixed CME message.
void validateSpans(const ByteSpan spans[], std::size_t spansCount)
void validateNonNegativeSpanCount(Count count, std::true_type)
std::size_t checkedSpanCount(Count suppliedCount, std::size_t maxSpansCount, std::size_t minSpansCount=0)
SpanRange checkedSpans(Collection &spans, std::size_t maxSpansCount, std::size_t minSpansCount=0)
typename std::enable_if< IsByteSpanCollection< Collection >::value, int >::type EnableIfByteSpanCollection
ByteSpan asSpan(const PacketHeader &header) noexcept
Provides a read-only byte view of a packet header.
std::span< const std::uint8_t > ByteSpan
Provides a non-owning read-only view over contiguous byte storage.