CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
primitive_types.h
1
3#pragma once
4
5#include <system/boolean.h>
6#include <system/byte.h>
7#include <system/double.h>
8#include <system/int16.h>
9#include <system/int32.h>
10#include <system/int64.h>
11#include <system/sbyte.h>
12#include <system/single.h>
13#include <system/uint16.h>
14#include <system/uint32.h>
15#include <system/uint64.h>
16
17#include <system/exceptions.h>
18#include <system/get_hash_code.h>//do not remove. Translator includes primitive_types.h on using System::GetHashCode(...)
19#include <limits>
20#include <cmath>
21
22namespace System {
23
25namespace Details {
26
27template<typename T>
28struct IsSigned : std::is_signed<T> {};
29
30template<typename T>
31struct IsUnsigned : std::is_unsigned<T> {};
32
33template<>
34struct IsSigned<Decimal> : std::true_type {};
35
36template<>
37struct IsUnsigned<Decimal> : std::false_type {};
38
39template<typename TTo, typename TFrom>
40typename std::enable_if<IsSigned<TFrom>::value && IsSigned<TTo>::value>::type CheckCast(TFrom value)
41{
42 if (value < std::numeric_limits<TTo>::min() || value > std::numeric_limits<TTo>::max())
43 throw OverflowException();
44}
45
46template<typename TTo, typename TFrom>
47typename std::enable_if<IsSigned<TFrom>::value && IsUnsigned<TTo>::value>::type CheckCast(TFrom value)
48{
49 if (value < 0 || std::numeric_limits<TTo>::max() - value < 0)
50 throw OverflowException();
51}
52
53template<typename TTo, typename TFrom>
54typename std::enable_if<IsUnsigned<TFrom>::value && IsSigned<TTo>::value>::type CheckCast(TFrom value)
55{
56 if (static_cast<TTo>(std::numeric_limits<TTo>::max() - value) < 0)
57 throw OverflowException();
58}
59
60template<typename TTo, typename TFrom>
61typename std::enable_if<IsUnsigned<TFrom>::value && IsUnsigned<TTo>::value>::type CheckCast(TFrom value)
62{
63 if (value > std::numeric_limits<TTo>::max())
64 throw OverflowException();
65}
66
67} // Details
69
76template <typename TA, typename TB>
77inline bool Equals(const TA& a, const TB& b)
78{
79 return a == b;
80}
81
89template<>
90inline bool Equals<float, float>(const float& a, const float& b)
91{
92 return (std::isnan(a) && std::isnan(b)) ? true : a == b;
93}
94
99template<>
100inline bool Equals<double, double>(const double& a, const double& b)
101{
102 return (std::isnan(a) && std::isnan(b)) ? true : a == b;
103}
104
111template <typename TA, typename TB>
112std::enable_if_t<!std::is_floating_point<TA>::value && !std::is_floating_point<TB>::value, int>
113Compare(const TA& a, const TB& b)
114{
115 return (a < b) ? -1 : ((b < a) ? 1 : 0);
116}
117
124template <typename TA, typename TB>
125std::enable_if_t<std::is_floating_point<TA>::value && std::is_floating_point<TB>::value, int>
126Compare(const TA& a, const TB& b)
127{
128 if (std::isnan(a))
129 {
130 if (std::isnan(b))
131 return 0;
132 return -1;
133 }
134 if (std::isnan(b))
135 return 1;
136 return (a < b) ? -1 : ((b < a) ? 1 : 0);
137}
138
143template <typename T>
144inline bool IsNaN(const T& value)
145{
146 return std::isnan(value);
147}
148
153template <typename T>
154inline bool IsInfinity(const T& value)
155{
156 return std::isinf(value);
157}
158
163template <typename T>
164inline bool IsPositiveInfinity(const T& value)
165{
166 return (std::isinf(value) && value > 0);
167}
168
173template <typename T>
174inline bool IsNegativeInfinity(const T& value)
175{
176 return (std::isinf(value) && value < 0);
177}
178
185template<typename TTo, typename TFrom>
186TTo CheckedCast(TFrom value)
187{
188 Details::CheckCast<TTo>(value);
189 return static_cast<TTo>(value);
190}
191
192} // namespace System
Definition: db_command.h:9
std::enable_if_t<!std::is_floating_point< TA >::value &&!std::is_floating_point< TB >::value, int > Compare(const TA &a, const TB &b)
Compares two values.
Definition: primitive_types.h:113
bool Equals(const TA &a, const TB &b)
Determines the equality of two values applying operator==() to them.
Definition: primitive_types.h:77
bool IsNegativeInfinity(const T &value)
Determines if the specified value represents negative infinity.
Definition: primitive_types.h:174
@ Decimal
A simple type representing values ranging from 1.0 x 10 -28 to approximately 7.9 x 10 28 with 28-29 s...
TTo CheckedCast(TFrom value)
Determines if the specified value falls into the range of values of type TTo and if it does casts it ...
Definition: primitive_types.h:186
bool Equals< float, float >(const float &a, const float &b)
Specialization for single-precision floating point values. Although two floating point NaNs are defin...
Definition: primitive_types.h:90
bool Equals< double, double >(const double &a, const double &b)
Specialization for double-precision floating point values.
Definition: primitive_types.h:100
bool IsPositiveInfinity(const T &value)
Determines if the specified value represents positive infinity.
Definition: primitive_types.h:164
bool IsInfinity(const T &value)
Determines if the specified value represents infinity.
Definition: primitive_types.h:154
bool IsNaN(const T &value)
Determines if the specified value is Not-A-Number value.
Definition: primitive_types.h:144