OnixS C++ CME MDP Conflated TCP Handler  1.3.1
API Documentation
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 
35 /// \private
36 template <bool B, class T = void>
37 using EnableIf = std::enable_if<B, T>;
38 
39 /// \private
40 template <typename Base, typename Derived>
41 using IsBaseOf = std::is_base_of<Base, Derived>;
42 
43 /// \private
44 template <class T>
45 using 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 
54 /// \private
55 template <bool B, class T = void>
56 struct EnableIf
57 {
58  typedef T type;
59 };
60 
61 /// \private
62 template <class T>
63 struct EnableIf<false, T> {};
64 
65 /// \private
66 template<typename Base, typename Derived>
67 struct IsBaseOf
68 {
69  enum { value = __is_base_of(Base, Derived)};
70 };
71 
72 /// \private
73 template<typename T>
74 struct 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 
83 ///\private
84 namespace 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 
92  ///\private
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 
102  ///\private
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 
112  ///\private
113  template<typename T>
114  struct IsDecimal
115  {
116  enum { value = HasMantissa<T>::value && HasExponent<T>::value };
117  };
118 
119  ///\private
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 
130  /// \private
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 
140  /// \private
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 
150  /// \private
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 
161 ///\private
162 template <class To, class From>
164 inline
165 typename 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 
176 ///\private
177 template <class To, class From>
179 inline
180 typename 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_END
Definition: ABI.h:144
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition: Compiler.h:189
#define ONIXS_CONFLATEDTCP_PURE
Definition: Compiler.h:202
#define ONIXS_CONFLATEDTCP_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140