OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and API documentation
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <cstddef>
24#include <stdexcept>
25#include <string>
26#include <type_traits>
27#include <utility>
28
29#include <OnixS/CME/MDH/Utils.h>
33
35
37namespace details {
38
39template <class Collection, class = void>
40struct IsByteSpanCollection : std::false_type {};
41
42template <class Collection>
43struct IsByteSpanCollection<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>
50{};
51
52template <class Collection>
53using EnableIfByteSpanCollection = typename std::enable_if<IsByteSpanCollection<Collection>::value, int>::type;
54
55template <class Count>
56void validateNonNegativeSpanCount(Count count, std::true_type)
57{
58 if(count < 0)
59 throw std::runtime_error("Byte span collection count cannot be negative.");
60}
61
62template <class Count>
63void validateNonNegativeSpanCount(Count, std::false_type) noexcept {}
64
65template <class Count>
66std::size_t checkedSpanCount(Count suppliedCount, std::size_t maxSpansCount, std::size_t minSpansCount = 0)
67{
68 typedef typename std::remove_cv<Count>::type CountType;
69 validateNonNegativeSpanCount(suppliedCount, std::is_signed<CountType>{});
70
71 typedef typename std::common_type<CountType, std::size_t>::type Comparison;
72 const Comparison count = static_cast<Comparison>(suppliedCount);
73
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) + ".");
77
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) + ".");
81
82 return static_cast<std::size_t>(suppliedCount);
83}
84
85struct SpanRange { const ByteSpan* data; std::size_t count; };
86
87inline void validateSpans(const ByteSpan spans[], std::size_t spansCount)
88{
89 if(spansCount != 0 && spans == nullptr)
90 throw std::runtime_error("Byte span collection data cannot be null when count is nonzero.");
91
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.");
95}
96
97template <class Collection>
98SpanRange checkedSpans(Collection& spans, std::size_t maxSpansCount, std::size_t minSpansCount = 0)
99{
100 const auto count = checkedSpanCount(spans.size(), maxSpansCount, minSpansCount);
101 const auto data = spans.data();
102 validateSpans(data, count);
103 return {data, count};
104}
105
106} // namespace details
107
114inline ByteSpan asSpan(const PacketHeader& header) noexcept
115{
116 return ByteSpan(reinterpret_cast<const ByteSpan::value_type*>(&header), sizeof(header));
117}
118
128template <typename MessageType, size_t MaxMessageSize>
130{
131 return holder.toNetworkBytes();
132}
133
142template <typename MessageType, size_t MaxMessageSize>
144{
145 return holder.toNetworkBytes();
146}
147
#define ONIXS_CME_MDH_TESTING_NAMESPACE_END
Definition Bootstrap.h:84
#define ONIXS_CME_MDH_TESTING_NAMESPACE_BEGIN
Definition Bootstrap.h:83
Represents the sequence number and sending time stored in an MDP packet header.
Owns fixed-capacity storage for a prefixed CME message.
void validateSpans(const ByteSpan spans[], std::size_t spansCount)
Definition Utils.h:87
void validateNonNegativeSpanCount(Count count, std::true_type)
Definition Utils.h:56
std::size_t checkedSpanCount(Count suppliedCount, std::size_t maxSpansCount, std::size_t minSpansCount=0)
Definition Utils.h:66
SpanRange checkedSpans(Collection &spans, std::size_t maxSpansCount, std::size_t minSpansCount=0)
Definition Utils.h:98
typename std::enable_if< IsByteSpanCollection< Collection >::value, int >::type EnableIfByteSpanCollection
Definition Utils.h:53
ByteSpan asSpan(const PacketHeader &header) noexcept
Provides a read-only byte view of a packet header.
Definition Utils.h:114
std::span< const std::uint8_t > ByteSpan
Provides a non-owning read-only view over contiguous byte storage.
Definition ByteSpan.h:44