OnixS C++ B3 Binary UMDF Market Data Handler 1.10.0
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 <cassert>
24#include <type_traits>
25
27
29
30#define CHECK_TYPE_INTEGRAL(Type) \
31 static_assert( \
32 std::is_integral<Type>::value, \
33 #Type " must be an integral type, consider adding MemberTraits" \
34 );
35
37namespace details {
38
40 template <class T, class U>
41 struct IsSameSignedness
42 {
43 enum { value = (static_cast<bool>(std::is_signed<T>::value) == static_cast<bool>(std::is_signed<U>::value)) };
44 };
45
47 template<typename T>
48 struct HasMantissa
49 {
50 template<typename U> struct SFINAE {};
51 template<typename U> static char test(SFINAE<typename U::Mantissa>*);
52 template<typename U> static int test(...);
53 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
54 };
55
57 template<typename T>
58 struct HasExponent
59 {
60 template<typename U> struct SFINAE {};
61 template<typename U> static char test(SFINAE<typename U::Exponent>*);
62 template<typename U> static int test(...);
63 enum
64 {
65 value = sizeof(test<T>(nullptr)) == sizeof(char)
66 };
67 };
68
70 template<typename T>
71 struct IsDecimal
72 {
73 enum { value = HasMantissa<T>::value && HasExponent<T>::value };
74 };
75
77 template
78 <
79 class Decimal1,
80 class Decimal2
81 >
82 struct AreBothDecimals
83 {
84 enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
85 };
86
88 template<typename T>
89 struct HasMemberTraits
90 {
91 template<typename U> struct SFINAE {};
92 template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
93 template<typename U> static int test(...);
94 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
95 };
96
98 template<typename T>
99 struct HasValueStaticMember
100 {
101 template<typename U, typename U::Value (*)()> struct SFINAE {};
102 template<typename U> static char test(SFINAE<U, &U::value>*);
103 template<typename U> static int test(...);
104 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
105 };
106
108 template<typename T>
109 struct HasSerializeMember
110 {
111 template<typename U, void (U::*)(void*) const noexcept> struct SFINAE {};
112 template<typename U> static char test(SFINAE<U, &U::serialize>*);
113 template<typename U> static int test(...);
114 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
115 };
116
118 template<typename T>
119 struct HasBitsMember
120 {
121 template<typename U> struct SFINAE {};
122 template<typename U> static char test(SFINAE<typename U::Bits>*);
123 template<typename U> static int test(...);
124 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
125 };
126}
127
129template <typename Set, typename NullValue>
130typename std::enable_if<details::HasBitsMember<Set>::value, bool>::type
131operator != (NullValue null, Set set) noexcept
132{
133 ONIXS_B3_UMDF_MD_STATIC_ASSERT((sizeof(typename Set::Bits) == sizeof(typename NullValue::Value)));
134 ONIXS_B3_UMDF_MD_STATIC_ASSERT((details::IsSameSignedness<typename Set::Bits, typename NullValue::Value>::value));
135
136 return set.bits() != null;
137}
138
140template <class To, class From>
141ONIXS_B3_UMDF_MD_PURE
142inline
143typename std::enable_if<details::IsSameSignedness<To, From>::value, To>::type
144 numericCast(From from) noexcept
145{
146 const To to = static_cast<To>(from);
147
148 assert(static_cast<From>(to) == from);
149
150 return to;
151}
152
154template <class To, class From>
155ONIXS_B3_UMDF_MD_PURE
156inline
157typename std::enable_if<!details::IsSameSignedness<To, From>::value, To>::type
158 numericCast(From from) noexcept
159{
160 const To to = static_cast<To>(from);
161
162 assert(static_cast<From>(to) == from);
163
164 // The sign is lost during the conversion
165 assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
166
167 return to;
168}
169
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_END
Definition ABI.h:158
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:153
#define ONIXS_B3_UMDF_MD_STATIC_ASSERT(X)
Definition Compiler.h:117