cast-inl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef CAST_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include cast.h"
  3. // For the sake of sane code completion.
  4. #include "cast.h"
  5. #endif
  6. #include "enum.h"
  7. #include <util/string/cast.h>
  8. #include <util/string/printf.h>
  9. #include <type_traits>
  10. namespace NYT {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. namespace NDetail {
  13. template <class T, class S>
  14. constexpr bool IsInIntegralRange(S value)
  15. requires std::is_signed_v<T> && std::is_signed_v<S>
  16. {
  17. return value >= std::numeric_limits<T>::lowest() && value <= std::numeric_limits<T>::max();
  18. }
  19. template <class T, class S>
  20. constexpr bool IsInIntegralRange(S value)
  21. requires std::is_signed_v<T> && std::is_unsigned_v<S>
  22. {
  23. return value <= static_cast<typename std::make_unsigned<T>::type>(std::numeric_limits<T>::max());
  24. }
  25. template <class T, class S>
  26. constexpr bool IsInIntegralRange(S value)
  27. requires std::is_unsigned_v<T> && std::is_signed_v<S>
  28. {
  29. return value >= 0 && static_cast<typename std::make_unsigned<S>::type>(value) <= std::numeric_limits<T>::max();
  30. }
  31. template <class T, class S>
  32. constexpr bool IsInIntegralRange(S value)
  33. requires std::is_unsigned_v<T> && std::is_unsigned_v<S>
  34. {
  35. return value <= std::numeric_limits<T>::max();
  36. }
  37. template <class T, class S>
  38. constexpr bool IsInIntegralRange(S value)
  39. requires std::is_enum_v<S>
  40. {
  41. return IsInIntegralRange<T>(static_cast<std::underlying_type_t<S>>(value));
  42. }
  43. template <class T>
  44. TString FormatInvalidCastValue(T value)
  45. {
  46. return ::ToString(value);
  47. }
  48. inline TString FormatInvalidCastValue(signed char value)
  49. {
  50. return TString("'") + value + TString("'");
  51. }
  52. inline TString FormatInvalidCastValue(unsigned char value)
  53. {
  54. return TString("'") + value + TString("'");
  55. }
  56. #ifdef __cpp_char8_t
  57. inline TString FormatInvalidCastValue(char8_t value)
  58. {
  59. return FormatInvalidCastValue(static_cast<unsigned char>(value));
  60. }
  61. #endif
  62. } // namespace NDetail
  63. ////////////////////////////////////////////////////////////////////////////////
  64. template <class T, class S>
  65. constexpr bool CanFitSubtype()
  66. {
  67. return NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::min()) &&
  68. NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::max());
  69. }
  70. template <class T, class S>
  71. constexpr bool IsInIntegralRange(S value)
  72. {
  73. return NDetail::IsInIntegralRange<T>(value);
  74. }
  75. template <class T, class S>
  76. constexpr bool TryIntegralCast(S value, T* result)
  77. {
  78. if (!NDetail::IsInIntegralRange<T>(value)) {
  79. return false;
  80. }
  81. *result = static_cast<T>(value);
  82. return true;
  83. }
  84. template <class T, class S>
  85. T CheckedIntegralCast(S value)
  86. {
  87. T result;
  88. if (!TryIntegralCast<T>(value, &result)) {
  89. throw TSimpleException(Sprintf("Error casting %s value \"%s\" to %s: value is out of expected range [%s; %s]",
  90. TypeName<S>().c_str(),
  91. NYT::NDetail::FormatInvalidCastValue(value).c_str(),
  92. TypeName<T>().c_str(),
  93. ::ToString(std::numeric_limits<T>::lowest()).c_str(),
  94. ::ToString(std::numeric_limits<T>::max()).c_str()));
  95. }
  96. return result;
  97. }
  98. template <class T, class S>
  99. constexpr bool TryEnumCast(S value, T* result)
  100. {
  101. std::underlying_type_t<T> underlying;
  102. if (!TryIntegralCast<std::underlying_type_t<T>>(value, &underlying)) {
  103. return false;
  104. }
  105. auto candidate = static_cast<T>(underlying);
  106. if (!TEnumTraits<T>::FindLiteralByValue(candidate)) {
  107. return false;
  108. }
  109. *result = candidate;
  110. return true;
  111. }
  112. template <class T, class S>
  113. T CheckedEnumCast(S value)
  114. {
  115. T result;
  116. if (!TryEnumCast<T>(value, &result)) {
  117. throw TSimpleException(Sprintf("Error casting %s value \"%d\" to enum %s",
  118. TypeName<S>().c_str(),
  119. static_cast<int>(value),
  120. TEnumTraits<T>::GetTypeName().data()));
  121. }
  122. return result;
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////
  125. } // namespace NYT