guid.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <util/generic/typetraits.h>
  4. #include <library/cpp/yt/exception/exception.h>
  5. #include <array>
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. //! TGuid is 16-byte value that might be interpreted as four little-endian 32-bit integers or two 64-bit little-endian integers.
  9. /*!
  10. * *-------------------------*-------------------------*
  11. * | Parts64[0] | Parts64[1] |
  12. * *------------*------------*------------*------------*
  13. * | Parts32[0] | Parts32[1] | Parts32[2] | Parts32[3] |
  14. * *------------*------------*------------*------------*
  15. * | 15..............................................0 |
  16. * *---------------------------------------------------*
  17. *
  18. * Note, that bytes are kept in memory in reverse order.
  19. *
  20. * Canonical text representation of guid consists of four base-16 numbers.
  21. * In text form, Parts32[3] comes first, and Parts32[0] comes last.
  22. *
  23. * For example:
  24. *
  25. * xxyyzzaa-0-1234-ff
  26. *
  27. * xx is byte [0]
  28. * yy is byte [1]
  29. * zz is byte [2]
  30. * 12 is byte [8]
  31. * 34 is byte [9]
  32. * ff is byte [15]
  33. */
  34. struct TGuid
  35. {
  36. union
  37. {
  38. ui32 Parts32[4];
  39. ui64 Parts64[2];
  40. ui8 ReversedParts8[16];
  41. };
  42. //! Constructs a null (zero) guid.
  43. constexpr TGuid();
  44. //! Constructs guid from parts.
  45. constexpr TGuid(ui32 part0, ui32 part1, ui32 part2, ui32 part3);
  46. //! Constructs guid from parts.
  47. constexpr TGuid(ui64 part0, ui64 part1);
  48. //! Copies an existing guid.
  49. constexpr TGuid(const TGuid& other) noexcept = default;
  50. constexpr TGuid& operator=(const TGuid& other) noexcept = default;
  51. //! Checks if TGuid is zero.
  52. bool IsEmpty() const;
  53. //! Converts TGuid to bool, returns |false| iff TGuid is zero.
  54. explicit operator bool() const;
  55. //! Creates a new instance.
  56. static TGuid Create();
  57. //! Parses guid from TStringBuf, throws an exception if something went wrong.
  58. static TGuid FromString(TStringBuf str);
  59. //! Parses guid from TStringBuf, returns |true| if everything was ok.
  60. static bool FromString(TStringBuf str, TGuid* guid);
  61. //! Same as FromString, but expects exactly 32 hex digits without dashes.
  62. static TGuid FromStringHex32(TStringBuf str);
  63. //! Same as TryFromString, but expects exactly 32 hex digits without dashes.
  64. static bool FromStringHex32(TStringBuf str, TGuid* guid);
  65. };
  66. bool operator == (TGuid lhs, TGuid rhs) noexcept;
  67. std::strong_ordering operator <=> (TGuid lhs, TGuid rhs) noexcept;
  68. ////////////////////////////////////////////////////////////////////////////////
  69. constexpr int MaxGuidStringSize = 4 * 8 + 3;
  70. char* WriteGuidToBuffer(char* ptr, TGuid value);
  71. ////////////////////////////////////////////////////////////////////////////////
  72. //! Enables TGuid-to-TStringBuf conversion without allocation.
  73. class TFormattableGuid
  74. {
  75. public:
  76. explicit TFormattableGuid(TGuid guid);
  77. TStringBuf ToStringBuf() const;
  78. private:
  79. std::array<char, MaxGuidStringSize> Buffer_;
  80. const char* const End_;
  81. };
  82. ////////////////////////////////////////////////////////////////////////////////
  83. } // namespace NYT
  84. ////////////////////////////////////////////////////////////////////////////////
  85. Y_DECLARE_PODTYPE(NYT::TGuid);
  86. //! A hasher for TGuid.
  87. template <>
  88. struct THash<NYT::TGuid>
  89. {
  90. size_t operator()(const NYT::TGuid& guid) const;
  91. };
  92. ////////////////////////////////////////////////////////////////////////////////
  93. #define GUID_INL_H_
  94. #include "guid-inl.h"
  95. #undef GUID_INL_H_