aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2024-11-11 17:25:31 +0300
committerbabenko <babenko@yandex-team.com>2024-11-11 17:38:41 +0300
commit79ac4ed959d879107eba1105fa7dad95567b08b7 (patch)
treeb3b2cdb0645d6d44f1d9c73f1027735c8cf2b865
parent89676eb0165121e856d0cc1a82c62691daeb8ac3 (diff)
downloadydb-79ac4ed959d879107eba1105fa7dad95567b08b7.tar.gz
Add TSharedRef::FromString overloads for std::string
commit_hash:2edff041e77ead18a6bb7efeb13c8163d85c0750
-rw-r--r--library/cpp/yt/memory/ref-inl.h11
-rw-r--r--library/cpp/yt/memory/ref.cpp19
-rw-r--r--library/cpp/yt/memory/ref.h31
3 files changed, 52 insertions, 9 deletions
diff --git a/library/cpp/yt/memory/ref-inl.h b/library/cpp/yt/memory/ref-inl.h
index 7fcd91908c..a2074f9731 100644
--- a/library/cpp/yt/memory/ref-inl.h
+++ b/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());
diff --git a/library/cpp/yt/memory/ref.cpp b/library/cpp/yt/memory/ref.cpp
index 4d02ef6875..de04a83870 100644
--- a/library/cpp/yt/memory/ref.cpp
+++ b/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);
diff --git a/library/cpp/yt/memory/ref.h b/library/cpp/yt/memory/ref.h
index 78ea46675f..fa40cd9afb 100644
--- a/library/cpp/yt/memory/ref.h
+++ b/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);
};
////////////////////////////////////////////////////////////////////////////////