format_string-inl.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #ifndef FORMAT_STRING_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include format_string.h"
  3. // For the sake of sane code completion.
  4. #include "format_string.h"
  5. #endif
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <class... TArgs>
  9. template <class T>
  10. requires std::constructible_from<std::string_view, T>
  11. consteval TBasicFormatString<TArgs...>::TBasicFormatString(const T& fmt)
  12. : Format_(fmt)
  13. {
  14. CheckFormattability();
  15. #if !defined(NDEBUG) && !defined(YT_DISABLE_FORMAT_STATIC_ANALYSIS)
  16. NDetail::TFormatAnalyser::ValidateFormat<std::remove_cvref_t<TArgs>...>(Format_);
  17. #endif
  18. }
  19. template <class... TArgs>
  20. TStringBuf TBasicFormatString<TArgs...>::Get() const noexcept
  21. {
  22. return {Format_};
  23. }
  24. template <class... TArgs>
  25. consteval void TBasicFormatString<TArgs...>::CheckFormattability()
  26. {
  27. #if !defined(NDEBUG) && !defined(YT_DISABLE_FORMAT_STATIC_ANALYSIS)
  28. using TTuple = std::tuple<std::remove_cvref_t<TArgs>...>;
  29. [] <size_t... Idx> (std::index_sequence<Idx...>) {
  30. ([] {
  31. if constexpr (!CFormattable<std::tuple_element_t<Idx, TTuple>>) {
  32. CrashCompilerClassIsNotFormattable<std::tuple_element_t<Idx, TTuple>>();
  33. }
  34. } (), ...);
  35. } (std::index_sequence_for<TArgs...>());
  36. #endif
  37. }
  38. template <class... TArgs>
  39. TBasicFormatString<TArgs...>::TBasicFormatString(TRuntimeFormat fmt)
  40. : Format_(fmt.Get())
  41. {
  42. // NB(arkady-e1ppa): StaticFormat performs the
  43. // formattability check of the args in a way
  44. // that provides more useful information
  45. // than a simple static_assert with conjunction.
  46. // Additionally, the latter doesn't work properly
  47. // for older clang version.
  48. static constexpr auto argsChecker = [] {
  49. CheckFormattability();
  50. return 42;
  51. } ();
  52. Y_UNUSED(argsChecker);
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////
  55. } // namespace NYT