cast-inl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. 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. 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. 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. 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. 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. bool TryIntegralCast(S value, T* result)
  66. {
  67. if (!NYT::NDetail::IsInIntegralRange<T>(value)) {
  68. return false;
  69. }
  70. *result = static_cast<T>(value);
  71. return true;
  72. }
  73. template <class T, class S>
  74. T CheckedIntegralCast(S value)
  75. {
  76. T result;
  77. if (!TryIntegralCast<T>(value, &result)) {
  78. throw TSimpleException(Sprintf("Error casting %s value \"%s\" to %s: value is out of expected range [%s; %s]",
  79. TypeName<S>().c_str(),
  80. NYT::NDetail::FormatInvalidCastValue(value).c_str(),
  81. TypeName<T>().c_str(),
  82. ::ToString(std::numeric_limits<T>::lowest()).c_str(),
  83. ::ToString(std::numeric_limits<T>::max()).c_str()));
  84. }
  85. return result;
  86. }
  87. template <class T, class S>
  88. bool TryEnumCast(S value, T* result)
  89. {
  90. std::underlying_type_t<T> underlying;
  91. if (!TryIntegralCast<std::underlying_type_t<T>>(value, &underlying)) {
  92. return false;
  93. }
  94. auto candidate = static_cast<T>(underlying);
  95. if (!TEnumTraits<T>::FindLiteralByValue(candidate)) {
  96. return false;
  97. }
  98. *result = candidate;
  99. return true;
  100. }
  101. template <class T, class S>
  102. T CheckedEnumCast(S value)
  103. {
  104. T result;
  105. if (!TryEnumCast<T>(value, &result)) {
  106. throw TSimpleException(Sprintf("Error casting %s value \"%d\" to enum %s",
  107. TypeName<S>().c_str(),
  108. static_cast<int>(value),
  109. TEnumTraits<T>::GetTypeName().data()));
  110. }
  111. return result;
  112. }
  113. ////////////////////////////////////////////////////////////////////////////////
  114. } // namespace NYT