Skip to content

Commit 79ac4ed

Browse files
author
babenko
committed
Add TSharedRef::FromString overloads for std::string
commit_hash:2edff041e77ead18a6bb7efeb13c8163d85c0750
1 parent 89676eb commit 79ac4ed

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

library/cpp/yt/memory/ref-inl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ Y_FORCE_INLINE TSharedRef TSharedRef::FromString(TString str)
142142
return FromString<TDefaultSharedBlobTag>(std::move(str));
143143
}
144144

145+
template <class TTag>
146+
Y_FORCE_INLINE TSharedRef TSharedRef::FromString(std::string str)
147+
{
148+
return FromString(std::move(str), GetRefCountedTypeCookie<TTag>());
149+
}
150+
151+
Y_FORCE_INLINE TSharedRef TSharedRef::FromString(std::string str)
152+
{
153+
return FromString<TDefaultSharedBlobTag>(std::move(str));
154+
}
155+
145156
Y_FORCE_INLINE TStringBuf TSharedRef::ToStringBuf() const
146157
{
147158
return TStringBuf(Begin(), Size());

library/cpp/yt/memory/ref.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class TBlobHolder
4646

4747
////////////////////////////////////////////////////////////////////////////////
4848

49+
template <class TString>
4950
class TStringHolder
5051
: public TSharedRangeHolder
5152
{
@@ -232,11 +233,27 @@ TMutableRef TMutableRef::FromBlob(TBlob& blob)
232233

233234
TSharedRef TSharedRef::FromString(TString str, TRefCountedTypeCookie tagCookie)
234235
{
235-
auto holder = New<TStringHolder>(std::move(str), tagCookie);
236+
return FromStringImpl(std::move(str), tagCookie);
237+
}
238+
239+
TSharedRef TSharedRef::FromString(std::string str, TRefCountedTypeCookie tagCookie)
240+
{
241+
return FromStringImpl(std::move(str), tagCookie);
242+
}
243+
244+
template <class TString>
245+
TSharedRef TSharedRef::FromStringImpl(TString str, TRefCountedTypeCookie tagCookie)
246+
{
247+
auto holder = New<TStringHolder<TString>>(std::move(str), tagCookie);
236248
auto ref = TRef::FromString(holder->String());
237249
return TSharedRef(ref, std::move(holder));
238250
}
239251

252+
TSharedRef TSharedRef::FromString(const char* str)
253+
{
254+
return FromString(std::string(str));
255+
}
256+
240257
TSharedRef TSharedRef::FromBlob(TBlob&& blob)
241258
{
242259
auto ref = TRef::FromBlob(blob);

library/cpp/yt/memory/ref.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,34 @@ class TSharedRef
134134
operator TRef() const;
135135

136136

137-
//! Creates a TSharedRef from a string.
138-
//! Since strings are ref-counted, no data is copied.
137+
//! Creates a TSharedRef from TString.
138+
//! Since strings are ref-counted, no data is being copied.
139139
//! The memory is marked with a given tag.
140140
template <class TTag>
141141
static TSharedRef FromString(TString str);
142142

143-
//! Creates a TSharedRef from a string.
144-
//! Since strings are ref-counted, no data is copied.
145-
//! The memory is marked with TDefaultSharedBlobTag.
143+
//! Same as above but the memory is marked with TDefaultSharedBlobTag.
146144
static TSharedRef FromString(TString str);
147145

148-
//! Creates a TSharedRef reference from a string.
149-
//! Since strings are ref-counted, no data is copied.
150-
//! The memory is marked with a given tag.
146+
//! Same as above but the memory tag is specified in #tagCookie.
151147
static TSharedRef FromString(TString str, TRefCountedTypeCookie tagCookie);
152148

149+
//! Creates a TSharedRef from std::string.
150+
//! No data is being copied in #FromString itself but since #str is passed by value
151+
//! a copy may occur at caller's side.
152+
//! The memory is marked with a given tag.
153+
template <class TTag>
154+
static TSharedRef FromString(std::string str);
155+
156+
//! Same as above but the memory is marked with TDefaultSharedBlobTag.
157+
static TSharedRef FromString(std::string str);
158+
159+
//! Same as above but the memory tag is specified in #tagCookie.
160+
static TSharedRef FromString(std::string str, TRefCountedTypeCookie tagCookie);
161+
162+
//! Creates a TSharedRef from a zero-terminated C string.
163+
static TSharedRef FromString(const char* str);
164+
153165
//! Creates a TSharedRef for a given blob taking ownership of its content.
154166
static TSharedRef FromBlob(TBlob&& blob);
155167

@@ -176,6 +188,9 @@ class TSharedRef
176188

177189
private:
178190
friend class TSharedRefArrayImpl;
191+
192+
template <class TString>
193+
static TSharedRef FromStringImpl(TString str, TRefCountedTypeCookie tagCookie);
179194
};
180195

181196
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)