OnixS C++ CME MDP Conflated TCP Handler 1.3.6
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
26
27#ifdef ONIXS_CONFLATEDTCP_HAS_TYPE_TRAITS
28# include <type_traits>
29#endif
30
32
33#ifdef ONIXS_CONFLATEDTCP_HAS_TYPE_TRAITS
34
36template <bool B, class T = void>
37using EnableIf = std::enable_if<B, T>;
38
40template <typename Base, typename Derived>
41using IsBaseOf = std::is_base_of<Base, Derived>;
42
44template <class T>
45using IsSigned = std::is_signed<T>;
46
47#define CHECK_TYPE_INTEGRAL(Type) \
48 static_assert( \
49 std::is_integral<Type>::value, \
50 #Type " must be an integral type, consider adding MemberTraits" \
51 );
52#else
53
55template <bool B, class T = void>
56struct EnableIf
57{
58 typedef T type;
59};
60
62template <class T>
63struct EnableIf<false, T> {};
64
66template<typename Base, typename Derived>
67struct IsBaseOf
68{
69 enum { value = __is_base_of(Base, Derived)};
70};
71
73template<typename T>
74struct IsSigned
75{
76 enum { value = (!(static_cast<T>(-1) > static_cast<T>(0))) };
77};
78
79#define CHECK_TYPE_INTEGRAL(Type)
80
81#endif
82
84namespace details {
85
86 template <class T, class U>
88 {
89 enum { value = (static_cast<bool>(IsSigned<T>::value) == static_cast<bool>(IsSigned<U>::value)) };
90 };
91
93 template<typename T>
94 struct HasMantissa
95 {
96 template<typename U> struct SFINAE {};
97 template<typename U> static char test(SFINAE<typename U::Mantissa>*);
98 template<typename U> static int test(...);
99 enum { value = sizeof(test<T>(ONIXS_CONFLATEDTCP_NULLPTR)) == sizeof(char) };
100 };
101
103 template<typename T>
104 struct HasExponent
105 {
106 template<typename U> struct SFINAE {};
107 template<typename U> static char test(SFINAE<typename U::Exponent>*);
108 template<typename U> static int test(...);
109 enum { value = sizeof(test<T>(ONIXS_CONFLATEDTCP_NULLPTR)) == sizeof(char) };
110 };
111
113 template<typename T>
114 struct IsDecimal
115 {
116 enum { value = HasMantissa<T>::value && HasExponent<T>::value };
117 };
118
120 template
121 <
122 class Decimal1,
123 class Decimal2
124 >
125 struct AreBothDecimals
126 {
127 enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
128 };
129
131 template<typename T>
132 struct HasMemberTraits
133 {
134 template<typename U> struct SFINAE {};
135 template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
136 template<typename U> static int test(...);
137 enum { value = sizeof(test<T>(ONIXS_CONFLATEDTCP_NULLPTR)) == sizeof(char) };
138 };
139
141 template<typename T>
142 struct HasValueStaticMember
143 {
144 template<typename U, typename U::Value (*)()> struct SFINAE {};
145 template<typename U> static char test(SFINAE<U, &U::value>*);
146 template<typename U> static int test(...);
147 enum { value = sizeof(test<T>(ONIXS_CONFLATEDTCP_NULLPTR)) == sizeof(char) };
148 };
149
151 template<typename T>
152 struct HasSerializeMember
153 {
154 template<typename U, void (U::*)(void*) const ONIXS_CONFLATEDTCP_NOTHROW> struct SFINAE {};
155 template<typename U> static char test(SFINAE<U, &U::serialize>*);
156 template<typename U> static int test(...);
157 enum { value = sizeof(test<T>(ONIXS_CONFLATEDTCP_NULLPTR)) == sizeof(char) };
158 };
159}
160
162template <class To, class From>
164inline
165typename EnableIf<details::IsSameSignedness<To, From>::value, To>::type
166 numericCast(From from) ONIXS_CONFLATEDTCP_NOTHROW
167{
168 const To to =
169 static_cast<To>(from);
170
171 assert(static_cast<From>(to) == from);
172
173 return to;
174}
175
177template <class To, class From>
179inline
180typename EnableIf<!details::IsSameSignedness<To, From>::value, To>::type
181 numericCast(From from) ONIXS_CONFLATEDTCP_NOTHROW
182{
183 const To to =
184 static_cast<To>(from);
185
186 assert(static_cast<From>(to) == from);
187
188 // The sign is lost during the conversion
189 assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
190
191 return to;
192}
193
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_END
Definition ABI.h:143
#define ONIXS_CONFLATEDTCP_PURE
Definition Compiler.h:189
#define ONIXS_CONFLATEDTCP_NULLPTR
Definition Compiler.h:182
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition Compiler.h:176