9 #ifndef _gu_convert_hpp_
10 #define _gu_convert_hpp_
12 #include "gu_macros.h"
13 #include "gu_throw.hpp"
26 template <
typename FROM,
typename TO>
inline
27 TO
convert (
const FROM& from,
const TO& to)
29 if (gu_unlikely(from > std::numeric_limits<TO>::max() ||
30 from < std::numeric_limits<TO>::min()))
33 gu_throw_error (ERANGE) << from <<
" is unrepresentable with "
34 << (std::numeric_limits<TO>::is_signed ?
35 "signed" :
"unsigned") <<
" "
36 <<
sizeof(TO) <<
" bytes.";
39 return static_cast<TO
>(from);
45 long long convert (
const unsigned long long& from,
const long long& to)
47 if (gu_unlikely(from > static_cast<unsigned long long>
48 (std::numeric_limits<long long>::max())))
50 gu_throw_error (ERANGE) << from
51 <<
" is unrepresentable with 'long long'";
54 return static_cast<long long>(from);
58 unsigned long long convert (
const long long& from,
59 const unsigned long long& to)
61 if (gu_unlikely(from < 0))
63 gu_throw_error (ERANGE) << from
64 <<
" is unrepresentable with 'unsigned long long'";
67 return static_cast<unsigned long long>(from);
71 long convert (
const unsigned long& from,
const long& to)
73 if (gu_unlikely(from > static_cast<unsigned long>
74 (std::numeric_limits<long>::max())))
76 gu_throw_error (ERANGE) << from
77 <<
" is unrepresentable with 'long'";
80 return static_cast<long long>(from);
84 unsigned long convert (
const long& from,
const unsigned long& to)
86 if (gu_unlikely(from < 0))
88 gu_throw_error (ERANGE) << from
89 <<
" is unrepresentable with 'unsigned long'";
92 return static_cast<unsigned long>(from);
96 int convert (
const unsigned int& from,
const int& to)
98 if (gu_unlikely(from > static_cast<unsigned int>
99 (std::numeric_limits<int>::max())))
101 gu_throw_error (ERANGE) << from
102 <<
" is unrepresentable with 'long'";
105 return static_cast<int>(from);
109 unsigned int convert (
const int& from,
const unsigned int& to)
111 if (gu_unlikely(from < 0))
113 gu_throw_error (ERANGE) << from
114 <<
" is unrepresentable with 'unsigned long'";
117 return static_cast<unsigned int>(from);
TO convert(const FROM &from, const TO &to)
Definition: gu_convert.hpp:27