OnixS C++ CME MDP Premium Market Data Handler 5.10.2
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#pragma once
21
22#include <cassert>
23#include <type_traits>
24
25#include <OnixS/CME/MDH/ABI.h>
26
28
29#define ONIXS_CMEMDH_CHECK_TYPE_INTEGRAL(Type) \
30 static_assert(std::is_integral<Type>::value, #Type " must be an integral type, consider adding MemberTraits");
31
33namespace details {
35 template <class T, class U>
36 struct IsSameSignedness
37 {
38 enum { value = (static_cast<bool>(std::is_signed<T>::value) == static_cast<bool>(std::is_signed<U>::value)) };
39 };
40
42 template<typename T>
43 struct HasMantissa
44 {
45 template<typename U> struct SFINAE {};
46 template<typename U> static char test(SFINAE<typename U::Mantissa>*);
47 template<typename U> static int test(...);
48 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
49 };
50
52 template<typename T>
53 struct HasExponent
54 {
55 template<typename U> struct SFINAE {};
56 template<typename U> static char test(SFINAE<typename U::Exponent>*);
57 template<typename U> static int test(...);
58 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
59 };
60
62 template<typename T>
63 struct IsDecimal
64 {
65 enum { value = HasMantissa<T>::value && HasExponent<T>::value };
66 };
67
69 template
70 <
71 class Decimal1,
72 class Decimal2
73 >
74 struct AreBothDecimals
75 {
76 enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
77 };
78
80 template<typename T>
81 struct HasMemberTraits
82 {
83 template<typename U> struct SFINAE {};
84 template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
85 template<typename U> static int test(...);
86 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
87 };
88
90 template<typename T>
91 struct HasValueStaticMember
92 {
93 template<typename U, typename U::Value (*)()> struct SFINAE {};
94 template<typename U> static char test(SFINAE<U, &U::value>*);
95 template<typename U> static int test(...);
96 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
97 };
98
100 template<typename T>
101 struct HasSerializeMember
102 {
103 template<typename U, void (U::*)(void*) const noexcept> struct SFINAE {};
104 template<typename U> static char test(SFINAE<U, &U::serialize>*);
105 template<typename U> static int test(...);
106 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
107 };
108}
109
111template <class To, class From>
113typename std::enable_if<details::IsSameSignedness<To, From>::value, To>::type
114 numericCast(From from) noexcept
115{
116 const auto to = static_cast<To>(from);
117
118 assert(static_cast<From>(to) == from);
119
120 return to;
121}
122
124template <class To, class From>
126typename std::enable_if<!details::IsSameSignedness<To, From>::value, To>::type
127 numericCast(From from) noexcept
128{
129 const auto to = static_cast<To>(from);
130
131 assert(static_cast<From>(to) == from);
132
133 // The sign is lost during the conversion
134 assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
135
136 return to;
137}
138
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:57
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:58
#define ONIXS_CMEMDH_FORCEINLINE
Definition Compiler.h:161