OnixS C++ B3 BOE Binary Order Entry  1.2.0
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 
25 #include <OnixS/B3/BOE/ABI.h>
26 
27 #ifdef ONIXS_B3_BOE_HAS_TYPE_TRAITS
28 # include <type_traits>
29 #endif
30 
32 
33 #ifdef ONIXS_B3_BOE_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_BOE_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 { value = sizeof(test<T>(ONIXS_B3_BOE_NULLPTR)) == sizeof(char) };
111  };
112 
113  ///\private
114  template<typename T>
115  struct IsDecimal
116  {
117  enum { value = HasMantissa<T>::value && HasExponent<T>::value };
118  };
119 
120  ///\private
121  template
122  <
123  class Decimal1,
124  class Decimal2
125  >
126  struct AreBothDecimals
127  {
128  enum { value = IsDecimal<Decimal1>::value && IsDecimal<Decimal2>::value };
129  };
130 
131  /// \private
132  template<typename T>
133  struct HasMemberTraits
134  {
135  template<typename U> struct SFINAE {};
136  template<typename U> static char test(SFINAE<struct U::MemberTraits>*);
137  template<typename U> static int test(...);
138  enum { value = sizeof(test<T>(ONIXS_B3_BOE_NULLPTR)) == sizeof(char) };
139  };
140 
141  /// \private
142  template<typename T>
143  struct HasValueStaticMember
144  {
145  template<typename U, typename U::Value (*)()> struct SFINAE {};
146  template<typename U> static char test(SFINAE<U, &U::value>*);
147  template<typename U> static int test(...);
148  enum { value = sizeof(test<T>(ONIXS_B3_BOE_NULLPTR)) == sizeof(char) };
149  };
150 
151  /// \private
152  template<typename T>
153  struct HasSerializeMember
154  {
155  template<typename U, void (U::*)(void*) const ONIXS_B3_BOE_NOTHROW> struct SFINAE {};
156  template<typename U> static char test(SFINAE<U, &U::serialize>*);
157  template<typename U> static int test(...);
158  enum { value = sizeof(test<T>(ONIXS_B3_BOE_NULLPTR)) == sizeof(char) };
159  };
160 }
161 
162 ///\private
163 template <class To, class From>
165 inline
166 typename EnableIf<details::IsSameSignedness<To, From>::value, To>::type
167  numericCast(From from) ONIXS_B3_BOE_NOTHROW
168 {
169  const To to =
170  static_cast<To>(from);
171 
172  assert(static_cast<From>(to) == from);
173 
174  return to;
175 }
176 
177 ///\private
178 template <class To, class From>
180 inline
181 typename EnableIf<!details::IsSameSignedness<To, From>::value, To>::type
182  numericCast(From from) ONIXS_B3_BOE_NOTHROW
183 {
184  const To to =
185  static_cast<To>(from);
186 
187  assert(static_cast<From>(to) == from);
188 
189  // The sign is lost during the conversion
190  assert((to > static_cast<To>(0)) == (from > static_cast<From>(0)));
191 
192  return to;
193 }
194 
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_END
Definition: ABI.h:144
#define ONIXS_B3_BOE_NULLPTR
Definition: Compiler.h:188
#define ONIXS_B3_BOE_NOTHROW
Definition: Compiler.h:182
#define ONIXS_B3_BOE_MESSAGING_NAMESPACE_BEGIN
Definition: ABI.h:140
#define ONIXS_B3_BOE_PURE
Definition: Compiler.h:195