OnixS C++ B3 Binary UMDF Market Data Handler  1.4.2
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_B3_UMDF_MD_HAS_TYPE_TRAITS
28 # include <type_traits>
29 #endif
30 
32 
33 #ifdef ONIXS_B3_UMDF_MD_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  ///\private
87  template <class T, class U>
88  struct IsSameSignedness
89  {
90  enum { value = (static_cast<bool>(IsSigned<T>::value) == static_cast<bool>(IsSigned<U>::value)) };
91  };
92 
93  ///\private
94  template<typename T>
95  struct HasMantissa
96  {
97  template<typename U> struct SFINAE {};
98  template<typename U> static char test(SFINAE<typename U::Mantissa>*);
99  template<typename U> static int test(...);
100  enum { value = sizeof(test<T>(ONIXS_B3_UMDF_MD_NULLPTR)) == sizeof(char) };
101  };
102 
103  ///\private
104  template<typename T>
105  struct HasExponent
106  {
107  template<typename U> struct SFINAE {};
108  template<typename U> static char test(SFINAE<typename U::Exponent>*);
109  template<typename U> static int test(...);
110  enum
111  {
112  value = sizeof(test<T>(ONIXS_B3_UMDF_MD_NULLPTR)) == sizeof(char)
113  };
114  };
115 
116  ///\private
117  template<typename T>
118  struct IsDecimal
119  {
120  enum { value = HasMantissa<T>::value && HasExponent<T>::value };
121  };
122 
123  ///\private
124  template
125  <
126  class Decimal1,
127  class Decimal2
128  >
129  struct AreBothDecimals
130  {
131  enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
132  };
133 
134  /// \private
135  template<typename T>
136  struct HasMemberTraits
137  {
138  template<typename U> struct SFINAE {};
139  template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
140  template<typename U> static int test(...);
141  enum { value = sizeof(test<T>(ONIXS_B3_UMDF_MD_NULLPTR)) == sizeof(char) };
142  };
143 
144  /// \private
145  template<typename T>
146  struct HasValueStaticMember
147  {
148  template<typename U, typename U::Value (*)()> struct SFINAE {};
149  template<typename U> static char test(SFINAE<U, &U::value>*);
150  template<typename U> static int test(...);
151  enum { value = sizeof(test<T>(ONIXS_B3_UMDF_MD_NULLPTR)) == sizeof(char) };
152  };
153 
154  /// \private
155  template<typename T>
156  struct HasSerializeMember
157  {
158  template<typename U, void (U::*)(void*) const ONIXS_B3_UMDF_MD_NOTHROW> struct SFINAE {};
159  template<typename U> static char test(SFINAE<U, &U::serialize>*);
160  template<typename U> static int test(...);
161  enum { value = sizeof(test<T>(ONIXS_B3_UMDF_MD_NULLPTR)) == sizeof(char) };
162  };
163 
164  /// \private
165  template<typename T>
166  struct HasBitsMember
167  {
168  template<typename U> struct SFINAE {};
169  template<typename U> static char test(SFINAE<typename U::Bits>*);
170  template<typename U> static int test(...);
171  enum { value = sizeof(test<T>(ONIXS_B3_UMDF_MD_NULLPTR)) == sizeof(char) };
172  };
173 }
174 
175 ///\private
176 template <typename Set, typename NullValue>
177 typename EnableIf<details::HasBitsMember<Set>::value, bool>::type
178 operator != (NullValue null, Set set) ONIXS_B3_UMDF_MD_NOTHROW
179 {
180  ONIXS_B3_UMDF_MD_STATIC_ASSERT((sizeof(typename Set::Bits) == sizeof(typename NullValue::Value)));
181  ONIXS_B3_UMDF_MD_STATIC_ASSERT((details::IsSameSignedness<typename Set::Bits, typename NullValue::Value>::value));
182 
183  return set.bits() != null;
184 }
185 
186 ///\private
187 template <class To, class From>
188 ONIXS_B3_UMDF_MD_PURE
189 inline
190 typename EnableIf<details::IsSameSignedness<To, From>::value, To>::type
191  numericCast(From from) ONIXS_B3_UMDF_MD_NOTHROW
192 {
193  const To to =
194  static_cast<To>(from);
195 
196  assert(static_cast<From>(to) == from);
197 
198  return to;
199 }
200 
201 ///\private
202 template <class To, class From>
203 ONIXS_B3_UMDF_MD_PURE
204 inline
205 typename EnableIf<!details::IsSameSignedness<To, From>::value, To>::type
206  numericCast(From from) ONIXS_B3_UMDF_MD_NOTHROW
207 {
208  const To to =
209  static_cast<To>(from);
210 
211  assert(static_cast<From>(to) == from);
212 
213  // The sign is lost during the conversion
214  assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
215 
216  return to;
217 }
218 
#define ONIXS_B3_UMDF_MD_NOTHROW
Definition: Compiler.h:114
#define ONIXS_B3_UMDF_MD_NULLPTR
Definition: Compiler.h:128
#define ONIXS_B3_UMDF_MD_STATIC_ASSERT(X)
Definition: Compiler.h:136
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_END
Definition: ABI.h:151
#define ONIXS_B3_UMDF_MD_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:146