Browse Source

Add TSharedRef::FromString overloads for std::string
commit_hash:2edff041e77ead18a6bb7efeb13c8163d85c0750

babenko 4 months ago
parent
commit
79ac4ed959
3 changed files with 52 additions and 9 deletions
  1. 11 0
      library/cpp/yt/memory/ref-inl.h
  2. 18 1
      library/cpp/yt/memory/ref.cpp
  3. 23 8
      library/cpp/yt/memory/ref.h

+ 11 - 0
library/cpp/yt/memory/ref-inl.h

@@ -142,6 +142,17 @@ Y_FORCE_INLINE TSharedRef TSharedRef::FromString(TString str)
     return FromString<TDefaultSharedBlobTag>(std::move(str));
 }
 
+template <class TTag>
+Y_FORCE_INLINE TSharedRef TSharedRef::FromString(std::string str)
+{
+    return FromString(std::move(str), GetRefCountedTypeCookie<TTag>());
+}
+
+Y_FORCE_INLINE TSharedRef TSharedRef::FromString(std::string str)
+{
+    return FromString<TDefaultSharedBlobTag>(std::move(str));
+}
+
 Y_FORCE_INLINE TStringBuf TSharedRef::ToStringBuf() const
 {
     return TStringBuf(Begin(), Size());

+ 18 - 1
library/cpp/yt/memory/ref.cpp

@@ -46,6 +46,7 @@ private:
 
 ////////////////////////////////////////////////////////////////////////////////
 
+template <class TString>
 class TStringHolder
     : public TSharedRangeHolder
 {
@@ -232,11 +233,27 @@ TMutableRef TMutableRef::FromBlob(TBlob& blob)
 
 TSharedRef TSharedRef::FromString(TString str, TRefCountedTypeCookie tagCookie)
 {
-    auto holder = New<TStringHolder>(std::move(str), tagCookie);
+    return FromStringImpl(std::move(str), tagCookie);
+}
+
+TSharedRef TSharedRef::FromString(std::string str, TRefCountedTypeCookie tagCookie)
+{
+    return FromStringImpl(std::move(str), tagCookie);
+}
+
+template <class TString>
+TSharedRef TSharedRef::FromStringImpl(TString str, TRefCountedTypeCookie tagCookie)
+{
+    auto holder = New<TStringHolder<TString>>(std::move(str), tagCookie);
     auto ref = TRef::FromString(holder->String());
     return TSharedRef(ref, std::move(holder));
 }
 
+TSharedRef TSharedRef::FromString(const char* str)
+{
+    return FromString(std::string(str));
+}
+
 TSharedRef TSharedRef::FromBlob(TBlob&& blob)
 {
     auto ref = TRef::FromBlob(blob);

+ 23 - 8
library/cpp/yt/memory/ref.h

@@ -134,22 +134,34 @@ public:
     operator TRef() const;
 
 
-    //! Creates a TSharedRef from a string.
-    //! Since strings are ref-counted, no data is copied.
+    //! Creates a TSharedRef from TString.
+    //! Since strings are ref-counted, no data is being copied.
     //! The memory is marked with a given tag.
     template <class TTag>
     static TSharedRef FromString(TString str);
 
-    //! Creates a TSharedRef from a string.
-    //! Since strings are ref-counted, no data is copied.
-    //! The memory is marked with TDefaultSharedBlobTag.
+    //! Same as above but the memory is marked with TDefaultSharedBlobTag.
     static TSharedRef FromString(TString str);
 
-    //! Creates a TSharedRef reference from a string.
-    //! Since strings are ref-counted, no data is copied.
-    //! The memory is marked with a given tag.
+    //! Same as above but the memory tag is specified in #tagCookie.
     static TSharedRef FromString(TString str, TRefCountedTypeCookie tagCookie);
 
+    //! Creates a TSharedRef from std::string.
+    //! No data is being copied in #FromString itself but since #str is passed by value
+    //! a copy may occur at caller's side.
+    //! The memory is marked with a given tag.
+    template <class TTag>
+    static TSharedRef FromString(std::string str);
+
+    //! Same as above but the memory is marked with TDefaultSharedBlobTag.
+    static TSharedRef FromString(std::string str);
+
+    //! Same as above but the memory tag is specified in #tagCookie.
+    static TSharedRef FromString(std::string str, TRefCountedTypeCookie tagCookie);
+
+    //! Creates a TSharedRef from a zero-terminated C string.
+    static TSharedRef FromString(const char* str);
+
     //! Creates a TSharedRef for a given blob taking ownership of its content.
     static TSharedRef FromBlob(TBlob&& blob);
 
@@ -176,6 +188,9 @@ public:
 
 private:
     friend class TSharedRefArrayImpl;
+
+    template <class TString>
+    static TSharedRef FromStringImpl(TString str, TRefCountedTypeCookie tagCookie);
 };
 
 ////////////////////////////////////////////////////////////////////////////////