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#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
33template <typename T>
34struct IsTriviallyCopyable
35#if defined(ONIXS_CMEMDH_COMPILER_IS_GNU) && !defined(ONIXS_CMEMDH_CXX14)
36 : std::integral_constant<bool, __has_trivial_copy(T) && __has_trivial_assign(T) && __has_trivial_destructor(T)>
37#else
38 : std::is_trivially_copyable<T>
39#endif
40{
41};
42
44template <typename T>
45struct IsTriviallyDefaultConstructible
46#if defined(ONIXS_CMEMDH_COMPILER_IS_GNU) && !defined(ONIXS_CMEMDH_CXX14)
47 : std::integral_constant<bool, __has_trivial_constructor(T)>
48#else
49 : std::is_trivially_default_constructible<T>
50#endif
51{
52};
53
55namespace details {
57 template <class T, class U>
58 struct IsSameSignedness
59 {
60 enum { value = (static_cast<bool>(std::is_signed<T>::value) == static_cast<bool>(std::is_signed<U>::value)) };
61 };
62
64 template<typename T>
65 struct HasMantissa
66 {
67 template<typename U> struct SFINAE {};
68 template<typename U> static char test(SFINAE<typename U::Mantissa>*);
69 template<typename U> static int test(...);
70 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
71 };
72
74 template<typename T>
75 struct HasExponent
76 {
77 template<typename U> struct SFINAE {};
78 template<typename U> static char test(SFINAE<typename U::Exponent>*);
79 template<typename U> static int test(...);
80 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
81 };
82
84 template<typename T>
85 struct IsDecimal
86 {
87 enum { value = HasMantissa<T>::value && HasExponent<T>::value };
88 };
89
91 template
92 <
93 class Decimal1,
94 class Decimal2
95 >
96 struct AreBothDecimals
97 {
98 enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
99 };
100
102 template<typename T>
103 struct HasMemberTraits
104 {
105 template<typename U> struct SFINAE {};
106 template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
107 template<typename U> static int test(...);
108 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
109 };
110
112 template<typename T>
113 struct HasValueStaticMember
114 {
115 template<typename U, typename U::Value (*)()> struct SFINAE {};
116 template<typename U> static char test(SFINAE<U, &U::value>*);
117 template<typename U> static int test(...);
118 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
119 };
120
122 template<typename T>
123 struct HasSerializeMember
124 {
125 template<typename U, void (U::*)(void*) const noexcept> struct SFINAE {};
126 template<typename U> static char test(SFINAE<U, &U::serialize>*);
127 template<typename U> static int test(...);
128 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
129 };
130}
131
133template <class To, class From>
135typename std::enable_if<details::IsSameSignedness<To, From>::value, To>::type
136 numericCast(From from) noexcept
137{
138 const auto to = static_cast<To>(from);
139
140 assert(static_cast<From>(to) == from);
141
142 return to;
143}
144
146template <class To, class From>
148typename std::enable_if<!details::IsSameSignedness<To, From>::value, To>::type
149 numericCast(From from) noexcept
150{
151 const auto to = static_cast<To>(from);
152
153 assert(static_cast<From>(to) == from);
154
155 // The sign is lost during the conversion
156 assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
157
158 return to;
159}
160
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_BEGIN
Definition Bootstrap.h:77
#define ONIXS_CMEMDH_MESSAGING_NAMESPACE_END
Definition Bootstrap.h:78
#define ONIXS_CMEMDH_FORCEINLINE
Definition Compiler.h:176
#define ONIXS_CMEMDH_CXX14
Definition Compiler.h:172
STL namespace.