number_conversion.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /************************************************************************************
  2. * *
  3. * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
  4. * *
  5. * This file is part of RTTR (Run Time Type Reflection) *
  6. * License: MIT License *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining *
  9. * a copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
  24. * SOFTWARE. *
  25. * *
  26. *************************************************************************************/
  27. #ifndef RTTR_NUMBER_CONVERSION_H_
  28. #define RTTR_NUMBER_CONVERSION_H_
  29. #include "rttr/detail/base/core_prerequisites.h"
  30. #include <limits>
  31. namespace rttr
  32. {
  33. namespace detail
  34. {
  35. /////////////////////////////////////////////////////////////////////////////////////////
  36. /*!
  37. * \brief Identity function
  38. *
  39. */
  40. template<typename F, typename T>
  41. typename std::enable_if<std::is_same<F, T>::value, bool>::type
  42. convert_to(const F& from, T& to)
  43. {
  44. to = from;
  45. return true;
  46. }
  47. template<typename F, typename T>
  48. using is_integer = std::integral_constant<bool, !std::is_same<F, T>::value &&
  49. std::is_integral<F>::value && std::is_integral<T>::value>;
  50. /////////////////////////////////////////////////////////////////////////////////////////
  51. template<typename F, typename T>
  52. typename std::enable_if<is_integer<F, T>::value &&
  53. std::numeric_limits<F>::is_signed &&
  54. !std::numeric_limits<T>::is_signed,
  55. bool>::type
  56. convert_to(const F& from, T& to)
  57. {
  58. if (from < 0)
  59. return false; // value too small
  60. if (static_cast<typename std::make_unsigned<F>::type>(from) > (std::numeric_limits<T>::max)())
  61. return false; // value too large
  62. to = static_cast<T>(from);
  63. return true;
  64. }
  65. /////////////////////////////////////////////////////////////////////////////////////////
  66. template<typename F, typename T>
  67. typename std::enable_if<is_integer<F, T>::value &&
  68. !std::numeric_limits<F>::is_signed &&
  69. std::numeric_limits<T>::is_signed,
  70. bool>::type
  71. convert_to(const F& from, T& to)
  72. {
  73. if (from > static_cast<typename std::make_unsigned<T>::type>((std::numeric_limits<T>::max)()))
  74. return false; // value too large
  75. to = static_cast<T>(from);
  76. return true;
  77. }
  78. /////////////////////////////////////////////////////////////////////////////////////////
  79. template<typename F, typename T>
  80. typename std::enable_if<is_integer<F, T>::value &&
  81. std::numeric_limits<F>::is_signed &&
  82. std::numeric_limits<T>::is_signed,
  83. bool>::type
  84. convert_to(const F& from, T& to)
  85. {
  86. if (from > (std::numeric_limits<T>::max)())
  87. return false; // value too large
  88. else if (from < (std::numeric_limits<T>::min)())
  89. return false; // value too small
  90. to = static_cast<T>(from);
  91. return true;
  92. }
  93. /////////////////////////////////////////////////////////////////////////////////////////
  94. template<typename F, typename T>
  95. typename std::enable_if<is_integer<F, T>::value &&
  96. !std::numeric_limits<F>::is_signed &&
  97. !std::numeric_limits<T>::is_signed,
  98. bool>::type
  99. convert_to(const F& from, T& to)
  100. {
  101. if (from > (std::numeric_limits<T>::max)())
  102. return false; // value too large
  103. to = static_cast<T>(from);
  104. return true;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////
  107. /////////////////////////////////////////////////////////////////////////////////////////
  108. /////////////////////////////////////////////////////////////////////////////////////////
  109. // floating point conversion
  110. template<typename F, typename T>
  111. typename std::enable_if<std::is_floating_point<F>::value &&
  112. std::is_integral<T>::value && std::numeric_limits<T>::is_signed,
  113. bool>::type
  114. convert_to(const F& from, T& to)
  115. {
  116. if (from > (std::numeric_limits<T>::max)())
  117. return false; // value too large
  118. else if (from < - (std::numeric_limits<T>::max)())
  119. return false; // value to small
  120. to = static_cast<T>(from);
  121. return true;
  122. }
  123. /////////////////////////////////////////////////////////////////////////////////////////
  124. template<typename F, typename T>
  125. typename std::enable_if<std::is_floating_point<F>::value &&
  126. std::is_integral<T>::value && !std::numeric_limits<T>::is_signed,
  127. bool>::type
  128. convert_to(const F& from, T& to)
  129. {
  130. if (from < 0 || from > (std::numeric_limits<T>::max)())
  131. return false; // value too large
  132. to = static_cast<T>(from);
  133. return true;
  134. }
  135. /////////////////////////////////////////////////////////////////////////////////////////
  136. /////////////////////////////////////////////////////////////////////////////////////////
  137. /////////////////////////////////////////////////////////////////////////////////////////
  138. // string conversion
  139. template<typename F, typename T>
  140. typename std::enable_if<std::is_same<T, std::string>::value,
  141. bool>::type
  142. convert_to(const F& from, T& to)
  143. {
  144. bool ok = false;
  145. to = to_string(from, &ok);
  146. return ok;
  147. }
  148. /////////////////////////////////////////////////////////////////////////////////////////
  149. } // end namespace detail
  150. } // end namespace rttr
  151. #endif // RTTR_NUMBER_CONVERSION_H_