guid.h 3.5 KB

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