OnixS C++ CME iLink 3 Binary Order Entry Handler 1.19.0
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#include <type_traits>
28
30
32template <bool B, class T = void>
33using EnableIf = std::enable_if<B, T>;
34
36template <typename Base, typename Derived>
37using IsBaseOf = std::is_base_of<Base, Derived>;
38
40template <class T>
41using IsSigned = std::is_signed<T>;
42
43#define CHECK_TYPE_INTEGRAL(Type) \
44 static_assert( \
45 std::is_integral<Type>::value, \
46 #Type " must be an integral type, consider adding MemberTraits" \
47 );
48
50namespace details {
51
53 template <class T, class U>
54 struct IsSameSignedness
55 {
56 enum { value = (static_cast<bool>(IsSigned<T>::value) == static_cast<bool>(IsSigned<U>::value)) };
57 };
58
60 template<typename T>
61 struct HasMantissa
62 {
63 template<typename U> struct SFINAE {};
64 template<typename U> static char test(SFINAE<typename U::Mantissa>*);
65 template<typename U> static int test(...);
66 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
67 };
68
70 template<typename T>
71 struct HasExponent
72 {
73 template<typename U> struct SFINAE {};
74 template<typename U> static char test(SFINAE<typename U::Exponent>*);
75 template<typename U> static int test(...);
76 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
77 };
78
80 template<typename T>
81 struct IsDecimal
82 {
83 enum { value = HasMantissa<T>::value && HasExponent<T>::value };
84 };
85
87 template
88 <
89 class Decimal1,
90 class Decimal2
91 >
92 struct AreBothDecimals
93 {
94 enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
95 };
96
98 template<typename T>
99 struct HasMemberTraits
100 {
101 template<typename U> struct SFINAE {};
102 template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
103 template<typename U> static int test(...);
104 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
105 };
106
108 template<typename T>
109 struct HasValueStaticMember
110 {
111 template<typename U, typename U::Value (*)()> struct SFINAE {};
112 template<typename U> static char test(SFINAE<U, &U::value>*);
113 template<typename U> static int test(...);
114 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
115 };
116
118 template<typename T>
119 struct HasSerializeMember
120 {
121 template<typename U, void (U::*)(void*) const noexcept> struct SFINAE {};
122 template<typename U> static char test(SFINAE<U, &U::serialize>*);
123 template<typename U> static int test(...);
124 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
125 };
126}
127
129template <class To, class From>
131inline
132typename EnableIf<details::IsSameSignedness<To, From>::value, To>::type
133 numericCast(From from) noexcept
134{
135 const To to =
136 static_cast<To>(from);
137
138 assert(static_cast<From>(to) == from);
139
140 return to;
141}
142
144template <class To, class From>
146inline
147typename EnableIf<!details::IsSameSignedness<To, From>::value, To>::type
148 numericCast(From from) noexcept
149{
150 const To to =
151 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_ILINK3_MESSAGING_NAMESPACE_END
Definition ABI.h:144
#define ONIXS_ILINK3_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:140
#define ONIXS_ILINK3_PURE
Definition Compiler.h:150