cast-inl.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 std::optional<T> TryCheckedIntegralCast(S value)
  77. {
  78. [[unlikely]] if (!NDetail::IsInIntegralRange<T>(value)) {
  79. return std::nullopt;
  80. }
  81. return static_cast<T>(value);
  82. }
  83. template <class T, class S>
  84. T CheckedIntegralCast(S value)
  85. {
  86. auto result = TryCheckedIntegralCast<T>(value);
  87. if (!result) {
  88. throw TSimpleException(Sprintf("Error casting %s value \"%s\" to %s: value is out of expected range [%s; %s]",
  89. TypeName<S>().c_str(),
  90. NYT::NDetail::FormatInvalidCastValue(value).c_str(),
  91. TypeName<T>().c_str(),
  92. ::ToString(std::numeric_limits<T>::lowest()).c_str(),
  93. ::ToString(std::numeric_limits<T>::max()).c_str()));
  94. }
  95. return *result;
  96. }
  97. template <class T, class S>
  98. requires TEnumTraits<T>::IsEnum
  99. constexpr std::optional<T> TryCheckedEnumCast(S value, bool enableUnknown)
  100. {
  101. auto underlying = TryCheckedIntegralCast<std::underlying_type_t<T>>(value);
  102. [[unlikely]] if (!underlying) {
  103. return std::nullopt;
  104. }
  105. auto candidate = static_cast<T>(*underlying);
  106. [[unlikely]] if (!TEnumTraits<T>::IsValidValue(candidate)) {
  107. if (enableUnknown) {
  108. if constexpr (constexpr auto optionalUnknownValue = TEnumTraits<T>::TryGetUnknownValue()) {
  109. if constexpr (TEnumTraits<T>::IsBitEnum) {
  110. return static_cast<T>(*underlying & ToUnderlying(TEnumTraits<T>::GetAllSetValue())) | *optionalUnknownValue;
  111. } else {
  112. return *optionalUnknownValue;
  113. }
  114. }
  115. }
  116. return std::nullopt;
  117. }
  118. return candidate;
  119. }
  120. template <class T, class S>
  121. requires TEnumTraits<T>::IsEnum
  122. T CheckedEnumCast(S value)
  123. {
  124. auto result = TryCheckedEnumCast<T>(value, /*enableUnknown*/ true);
  125. [[unlikely]] if (!result) {
  126. if constexpr (std::is_signed_v<S>) {
  127. throw TSimpleException(Sprintf("Error casting %s value \"%" PRIi64 "\" to enum %s",
  128. TypeName<S>().c_str(),
  129. static_cast<i64>(value),
  130. TEnumTraits<T>::GetTypeName().data()));
  131. } else {
  132. throw TSimpleException(Sprintf("Error casting %s value \"%" PRIu64 "\" to enum %s",
  133. TypeName<S>().c_str(),
  134. static_cast<ui64>(value),
  135. TEnumTraits<T>::GetTypeName().data()));
  136. }
  137. }
  138. return *result;
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////
  141. } // namespace NYT