error_attribute.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include "public.h"
  3. #include <library/cpp/yt/misc/guid.h>
  4. #include <library/cpp/yt/misc/tag_invoke_cpo.h>
  5. #include <string>
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. template <class T>
  9. concept CPrimitiveConvertible =
  10. std::same_as<T, i8> ||
  11. std::same_as<T, i32> ||
  12. std::same_as<T, i64> ||
  13. std::same_as<T, ui8> ||
  14. std::same_as<T, ui32> ||
  15. std::same_as<T, ui64> ||
  16. std::same_as<T, float> ||
  17. std::same_as<T, double> ||
  18. std::constructible_from<TStringBuf, const T&> ||
  19. std::same_as<T, TDuration> ||
  20. std::same_as<T, TInstant> ||
  21. std::same_as<T, bool> ||
  22. std::same_as<T, TGuid>;
  23. ////////////////////////////////////////////////////////////////////////////////
  24. namespace NAttributeValueConversionImpl {
  25. struct TTo
  26. : public TTagInvokeCpoBase<TTo>
  27. { };
  28. ////////////////////////////////////////////////////////////////////////////////
  29. template <class U>
  30. struct TFrom
  31. : public TTagInvokeCpoBase<TFrom<U>>
  32. { };
  33. } // namespace NAttributeValueConversionImpl
  34. ////////////////////////////////////////////////////////////////////////////////
  35. inline constexpr NAttributeValueConversionImpl::TTo ToErrorAttributeValue = {};
  36. template <class U>
  37. inline constexpr NAttributeValueConversionImpl::TFrom<U> FromErrorAttributeValue = {};
  38. ////////////////////////////////////////////////////////////////////////////////
  39. template <class T>
  40. concept CConvertibleToAttributeValue = requires (const T& value) {
  41. { NYT::ToErrorAttributeValue(value) } -> std::same_as<std::string>;
  42. };
  43. template <class T>
  44. concept CConvertibleFromAttributeValue = requires (TStringBuf value) {
  45. { NYT::FromErrorAttributeValue<T>(value) } -> std::same_as<T>;
  46. };
  47. ////////////////////////////////////////////////////////////////////////////////
  48. struct TErrorAttribute
  49. {
  50. using TKey = std::string;
  51. using TValue = std::string;
  52. template <CConvertibleToAttributeValue T>
  53. TErrorAttribute(const TKey& key, const T& value)
  54. : Key(key)
  55. , Value(NYT::ToErrorAttributeValue(value))
  56. { }
  57. TKey Key;
  58. TValue Value;
  59. };
  60. ////////////////////////////////////////////////////////////////////////////////
  61. } // namespace NYT
  62. #define ERROR_ATTRIBUTE_INL_H_
  63. #include "error_attribute-inl.h"
  64. #undef ERROR_ATTRIBUTE_INL_H_