OnixS C++ Euronext Optiq MDG Handler 1.5.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#include <type_traits>
25
27
29
31template <bool B, class T = void>
32using EnableIf = std::enable_if<B, T>;
33
35template <typename Base, typename Derived>
36using IsBaseOf = std::is_base_of<Base, Derived>;
37
39template <class T>
40using IsSigned = std::is_signed<T>;
41
42#define CHECK_TYPE_INTEGRAL(Type) \
43 static_assert( \
44 std::is_integral<Type>::value, \
45 #Type " must be an integral type, consider adding MemberTraits" \
46 );
47
49namespace details {
50
52 template <class T, class U>
53 struct IsSameSignedness
54 {
55 enum { value = (static_cast<bool>(IsSigned<T>::value) == static_cast<bool>(IsSigned<U>::value)) };
56 };
57
59 template<typename T>
60 struct HasMemberTraits
61 {
62 template<typename U> struct SFINAE {};
63 template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
64 template<typename U> static int test(...);
65 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
66 };
67
69 template<typename T>
70 struct HasValueStaticMember
71 {
72 template<typename U, typename U::Value (*)()> struct SFINAE {};
73 template<typename U> static char test(SFINAE<U, &U::value>*);
74 template<typename U> static int test(...);
75 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
76 };
77
79 template<typename T>
80 struct HasSerializeMember
81 {
82 template<typename U, void (U::*)(void*) const noexcept> struct SFINAE {};
83 template<typename U> static char test(SFINAE<U, &U::serialize>*);
84 template<typename U> static int test(...);
85 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
86 };
87
89 template<typename T>
90 struct HasBitsMember
91 {
92 template<typename U> struct SFINAE {};
93 template<typename U> static char test(SFINAE<typename U::Bits>*);
94 template<typename U> static int test(...);
95 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) };
96 };
97}
98
100template <typename Set, typename NullValue>
101typename EnableIf<details::HasBitsMember<Set>::value, bool>::type
102operator != (NullValue null, Set set) noexcept
103{
104 ONIXS_EURONEXT_OPTIQMDG_STATIC_ASSERT((sizeof(typename Set::Bits) == sizeof(typename NullValue::Value)));
105 ONIXS_EURONEXT_OPTIQMDG_STATIC_ASSERT((details::IsSameSignedness<typename Set::Bits, typename NullValue::Value>::value));
106
107 return set.bits() != null;
108}
109
111template <class To, class From>
113inline
114typename EnableIf<details::IsSameSignedness<To, From>::value, To>::type
115 numericCast(From from) noexcept
116{
117 const To to =
118 static_cast<To>(from);
119
120 assert(static_cast<From>(to) == from);
121
122 return to;
123}
124
126template <class To, class From>
128inline
129typename EnableIf<!details::IsSameSignedness<To, From>::value, To>::type
130 numericCast(From from) noexcept
131{
132 const To to =
133 static_cast<To>(from);
134
135 assert(static_cast<From>(to) == from);
136
137 // The sign is lost during the conversion
138 assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
139
140 return to;
141}
142
#define ONIXS_EURONEXT_OPTIQMDG_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:146
#define ONIXS_EURONEXT_OPTIQMDG_MESSAGING_NAMESPACE_END
Definition ABI.h:151
#define ONIXS_EURONEXT_OPTIQMDG_PURE
Definition Compiler.h:123