string.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "public.h"
  3. #include <library/cpp/yt/memory/ref.h>
  4. #include <variant>
  5. namespace NYT::NYson {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //! Contains a sequence of bytes in YSON encoding annotated with EYsonType describing
  8. //! the content. Could be null. Non-owning.
  9. class TYsonStringBuf
  10. {
  11. public:
  12. //! Constructs a null instance.
  13. TYsonStringBuf();
  14. //! Constructs an instance from TYsonString.
  15. TYsonStringBuf(const TYsonString& ysonString);
  16. //! Constructs a non-null instance with given type and content.
  17. explicit TYsonStringBuf(
  18. const TString& data,
  19. EYsonType type = EYsonType::Node);
  20. //! Constructs a non-null instance with given type and content.
  21. explicit TYsonStringBuf(
  22. TStringBuf data,
  23. EYsonType type = EYsonType::Node);
  24. //! Constructs a non-null instance with given type and content
  25. //! (without this overload there is no way to construct TYsonStringBuf from
  26. //! string literal).
  27. explicit TYsonStringBuf(
  28. const char* data,
  29. EYsonType type = EYsonType::Node);
  30. //! Returns |true| if the instance is not null.
  31. explicit operator bool() const;
  32. //! Returns the underlying YSON bytes. The instance must be non-null.
  33. TStringBuf AsStringBuf() const;
  34. //! Returns type of YSON contained here. The instance must be non-null.
  35. EYsonType GetType() const;
  36. protected:
  37. TStringBuf Data_;
  38. EYsonType Type_;
  39. bool Null_;
  40. };
  41. ////////////////////////////////////////////////////////////////////////////////
  42. //! An owning version of TYsonStringBuf.
  43. /*!
  44. * Internally captures the data either via TString or a polymorphic ref-counted holder.
  45. */
  46. class TYsonString
  47. {
  48. public:
  49. //! Constructs a null instance.
  50. TYsonString();
  51. //! Constructs an instance from TYsonStringBuf.
  52. //! Copies the data into a ref-counted payload.
  53. explicit TYsonString(const TYsonStringBuf& ysonStringBuf);
  54. //! Constructs an instance from TStringBuf.
  55. //! Copies the data into a ref-counted payload.
  56. explicit TYsonString(
  57. TStringBuf data,
  58. EYsonType type = EYsonType::Node);
  59. //! Constructs an instance from TString.
  60. //! Zero-copy for CoW TString: retains the reference to TString in payload.
  61. explicit TYsonString(
  62. const TString& data,
  63. EYsonType type = EYsonType::Node);
  64. //! Constructs an instance from TSharedRef.
  65. //! Zero-copy; retains the reference to TSharedRef holder in payload.
  66. explicit TYsonString(
  67. const TSharedRef& ref,
  68. EYsonType type = EYsonType::Node);
  69. //! Returns |true| if the instance is not null.
  70. explicit operator bool() const;
  71. //! Returns type of YSON contained here. The instance must be non-null.
  72. EYsonType GetType() const;
  73. //! Returns the non-owning data. The instance must be non-null.
  74. TStringBuf AsStringBuf() const;
  75. //! Returns the data represented by TString. The instance must be non-null.
  76. //! Copies the data in case the payload is not TString.
  77. TString ToString() const;
  78. //! Returns the data represented by TSharedRef. The instance must be non-null.
  79. //! The data is never copied.
  80. TSharedRef ToSharedRef() const;
  81. //! Computes the hash code.
  82. size_t ComputeHash() const;
  83. //! Allow to serialize/deserialize using the ::Save ::Load functions. See util/ysaveload.h.
  84. void Save(IOutputStream* s) const;
  85. void Load(IInputStream* s);
  86. private:
  87. struct TNullPayload
  88. { };
  89. std::variant<TNullPayload, TSharedRangeHolderPtr, TString> Payload_;
  90. const char* Begin_;
  91. ui64 Size_ : 56;
  92. EYsonType Type_ : 8;
  93. };
  94. ////////////////////////////////////////////////////////////////////////////////
  95. bool operator == (const TYsonString& lhs, const TYsonString& rhs);
  96. bool operator == (const TYsonString& lhs, const TYsonStringBuf& rhs);
  97. bool operator == (const TYsonStringBuf& lhs, const TYsonString& rhs);
  98. bool operator == (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs);
  99. bool operator != (const TYsonString& lhs, const TYsonString& rhs);
  100. bool operator != (const TYsonString& lhs, const TYsonStringBuf& rhs);
  101. bool operator != (const TYsonStringBuf& lhs, const TYsonString& rhs);
  102. bool operator != (const TYsonStringBuf& lhs, const TYsonStringBuf& rhs);
  103. TString ToString(const TYsonString& yson);
  104. TString ToString(const TYsonStringBuf& yson);
  105. ////////////////////////////////////////////////////////////////////////////////
  106. } // namespace NYT::NYson
  107. #define STRING_INL_H_
  108. #include "string-inl.h"
  109. #undef STRING_INL_H_