aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorponasenko-rs <ponasenko-rs@yandex-team.com>2023-11-14 11:24:26 +0300
committerponasenko-rs <ponasenko-rs@yandex-team.com>2023-11-14 18:38:56 +0300
commit59c91d9ecf73b46bd8e22a7f78531b93502c0cae (patch)
tree3e0dc509900bafbf38870486a075eeadcfd6f71d
parent9de04ef9e1acde332d456b088729bf328fcc9962 (diff)
downloadydb-59c91d9ecf73b46bd8e22a7f78531b93502c0cae.tar.gz
YT-20424: Fix move constructor and assignment for TSharedRange.
-rw-r--r--library/cpp/yt/memory/shared_range.h40
-rw-r--r--library/cpp/yt/memory/unittests/shared_range_ut.cpp44
-rw-r--r--library/cpp/yt/memory/unittests/ya.make1
3 files changed, 85 insertions, 0 deletions
diff --git a/library/cpp/yt/memory/shared_range.h b/library/cpp/yt/memory/shared_range.h
index af6856d633..e1c262f418 100644
--- a/library/cpp/yt/memory/shared_range.h
+++ b/library/cpp/yt/memory/shared_range.h
@@ -98,6 +98,26 @@ public:
, Holder_(std::move(holder))
{ }
+ TSharedRange(const TSharedRange& other) = default;
+
+ TSharedRange(TSharedRange&& other) noexcept
+ : TSharedRange()
+ {
+ other.Swap(*this);
+ }
+
+ TSharedRange& operator=(TSharedRange other) noexcept
+ {
+ other.Swap(*this);
+ return *this;
+ }
+
+ void Swap(TSharedRange& other) noexcept
+ {
+ DoSwap(TRange<T>::Data_, other.Data_);
+ DoSwap(TRange<T>::Length_, other.Length_);
+ Holder_.Swap(other.Holder_);
+ }
void Reset()
{
@@ -265,6 +285,26 @@ public:
, Holder_(std::move(holder))
{ }
+ TSharedMutableRange(const TSharedMutableRange& other) = default;
+
+ TSharedMutableRange(TSharedMutableRange&& other) noexcept
+ : TSharedMutableRange()
+ {
+ other.Swap(*this);
+ }
+
+ TSharedMutableRange& operator=(TSharedMutableRange other) noexcept
+ {
+ other.Swap(*this);
+ return *this;
+ }
+
+ void Swap(TSharedMutableRange& other) noexcept
+ {
+ DoSwap(TRange<T>::Data_, other.Data_);
+ DoSwap(TRange<T>::Length_, other.Length_);
+ Holder_.Swap(other.Holder_);
+ }
void Reset()
{
diff --git a/library/cpp/yt/memory/unittests/shared_range_ut.cpp b/library/cpp/yt/memory/unittests/shared_range_ut.cpp
new file mode 100644
index 0000000000..7b86a1eb15
--- /dev/null
+++ b/library/cpp/yt/memory/unittests/shared_range_ut.cpp
@@ -0,0 +1,44 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/memory/new.h>
+#include <library/cpp/yt/memory/shared_range.h>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+DECLARE_REFCOUNTED_STRUCT(THolder);
+
+struct THolder
+ : public TRefCounted
+{
+ int Value;
+
+ THolder(int value)
+ : Value(value)
+ { }
+};
+
+DEFINE_REFCOUNTED_TYPE(THolder);
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TSharedRange, Move)
+{
+ auto holder = New<THolder>(0);
+ int* raw = &holder->Value;
+
+ auto sharedRange = MakeSharedRange<int>({raw, 1}, std::move(holder));
+
+ {
+ auto sharedRangeMovedTo = std::move(sharedRange);
+ }
+
+ EXPECT_TRUE(sharedRange.ToVector().empty());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT
diff --git a/library/cpp/yt/memory/unittests/ya.make b/library/cpp/yt/memory/unittests/ya.make
index 0f475fcf38..514caa93a3 100644
--- a/library/cpp/yt/memory/unittests/ya.make
+++ b/library/cpp/yt/memory/unittests/ya.make
@@ -13,6 +13,7 @@ SRCS(
chunked_memory_pool_output_ut.cpp
free_list_ut.cpp
intrusive_ptr_ut.cpp
+ shared_range_ut.cpp
weak_ptr_ut.cpp
)