cast-inl.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <concepts>
  10. #include <type_traits>
  11. namespace NYT {
  12. ////////////////////////////////////////////////////////////////////////////////
  13. namespace NDetail {
  14. template <class T, class S>
  15. bool IsInIntegralRange(S value)
  16. requires std::is_signed_v<T> && std::is_signed_v<S>
  17. {
  18. return value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max();
  19. }
  20. template <class T, class S>
  21. bool IsInIntegralRange(S value)
  22. requires std::is_signed_v<T> && std::is_unsigned_v<S>
  23. {
  24. return value <= static_cast<typename std::make_unsigned<T>::type>(std::numeric_limits<T>::max());
  25. }
  26. template <class T, class S>
  27. bool IsInIntegralRange(S value)
  28. requires std::is_unsigned_v<T> && std::is_signed_v<S>
  29. {
  30. return value >= 0 && static_cast<typename std::make_unsigned<S>::type>(value) <= std::numeric_limits<T>::max();
  31. }
  32. template <class T, class S>
  33. bool IsInIntegralRange(S value)
  34. requires std::is_unsigned_v<T> && std::is_unsigned_v<S>
  35. {
  36. return value <= std::numeric_limits<T>::max();
  37. }
  38. template <class T, class S>
  39. bool IsInIntegralRange(S value)
  40. requires std::is_enum_v<S>
  41. {
  42. return IsInIntegralRange<T>(static_cast<std::underlying_type_t<S>>(value));
  43. }
  44. template <class T>
  45. TString FormatInvalidCastValue(T value)
  46. {
  47. return ::ToString(value);
  48. }
  49. inline TString FormatInvalidCastValue(signed char value)
  50. {
  51. return TString("'") + value + TString("'");
  52. }
  53. inline TString FormatInvalidCastValue(unsigned char value)
  54. {
  55. return TString("'") + value + TString("'");
  56. }
  57. #ifdef __cpp_char8_t
  58. inline TString FormatInvalidCastValue(char8_t value)
  59. {
  60. return FormatInvalidCastValue(static_cast<unsigned char>(value));
  61. }
  62. #endif
  63. } // namespace NDetail
  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("Argument value %s is out of expected range",
  79. NYT::NDetail::FormatInvalidCastValue(value).c_str()));
  80. }
  81. return result;
  82. }
  83. template <class T, class S>
  84. bool TryEnumCast(S value, T* result)
  85. {
  86. std::underlying_type_t<T> underlying;
  87. if (!TryIntegralCast<std::underlying_type_t<T>>(value, &underlying)) {
  88. return false;
  89. }
  90. auto candidate = static_cast<T>(underlying);
  91. if (!TEnumTraits<T>::FindLiteralByValue(candidate)) {
  92. return false;
  93. }
  94. *result = candidate;
  95. return true;
  96. }
  97. template <class T, class S>
  98. T CheckedEnumCast(S value)
  99. {
  100. T result;
  101. if (!TryEnumCast<T>(value, &result)) {
  102. throw TSimpleException(Sprintf("Invalid value %d of enum type %s",
  103. static_cast<int>(value),
  104. TEnumTraits<T>::GetTypeName().data()));
  105. }
  106. return result;
  107. }
  108. ////////////////////////////////////////////////////////////////////////////////
  109. } // namespace NYT