aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpavook <pavook@yandex-team.com>2024-11-04 18:22:23 +0300
committerpavook <pavook@yandex-team.com>2024-11-04 18:37:03 +0300
commita6181c673b1896aa2e0377c725ccc0bd23d0ebe9 (patch)
tree272fb016a928af28b416923ad75ae305e93a57ec
parent277ba0ca11143f8dde61dbf1778360eea6222ca1 (diff)
downloadydb-a6181c673b1896aa2e0377c725ccc0bd23d0ebe9.tar.gz
add ::element_type, .get() to smart pointers for better compatibility with std
For example, this makes it possible to use gtest pointer matchers on smart pointers commit_hash:2650074ae18ee35696b297d3d1f0393e7350789f
-rw-r--r--library/cpp/yt/memory/atomic_intrusive_ptr-inl.h6
-rw-r--r--library/cpp/yt/memory/atomic_intrusive_ptr.h6
-rw-r--r--library/cpp/yt/memory/intrusive_ptr.h9
-rw-r--r--library/cpp/yt/memory/weak_ptr.h1
-rw-r--r--util/generic/ptr.h47
-rw-r--r--util/generic/ptr_ut.cpp28
-rw-r--r--yt/yt/core/misc/mpsc_fair_share_queue-inl.h8
7 files changed, 97 insertions, 8 deletions
diff --git a/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h b/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h
index e337a78bcf..ffb2aaff5f 100644
--- a/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h
+++ b/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h
@@ -237,6 +237,12 @@ typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::Get() const
}
template <class T>
+typename TAtomicIntrusivePtr<T>::TRawPtr TAtomicIntrusivePtr<T>::get() const
+{
+ return Get();
+}
+
+template <class T>
TPackedPtr TAtomicIntrusivePtr<T>::AcquireObject(T* obj, bool consumeRef)
{
if (obj) {
diff --git a/library/cpp/yt/memory/atomic_intrusive_ptr.h b/library/cpp/yt/memory/atomic_intrusive_ptr.h
index 4bab78ef58..243920bdab 100644
--- a/library/cpp/yt/memory/atomic_intrusive_ptr.h
+++ b/library/cpp/yt/memory/atomic_intrusive_ptr.h
@@ -20,6 +20,9 @@ template <class T>
class TAtomicIntrusivePtr
{
public:
+ using TUnderlying = T;
+ using element_type = T;
+
TAtomicIntrusivePtr() = default;
TAtomicIntrusivePtr(std::nullptr_t);
@@ -45,6 +48,9 @@ public:
//! Result is only suitable for comparison, not dereference.
TRawPtr Get() const;
+ //! Result is only suitable for comparison, not dereference.
+ TRawPtr get() const;
+
explicit operator bool() const;
private:
diff --git a/library/cpp/yt/memory/intrusive_ptr.h b/library/cpp/yt/memory/intrusive_ptr.h
index 91b15279ae..1a168cec0e 100644
--- a/library/cpp/yt/memory/intrusive_ptr.h
+++ b/library/cpp/yt/memory/intrusive_ptr.h
@@ -18,6 +18,9 @@ class TIntrusivePtr
public:
using TUnderlying = T;
+ //! For compatibility with std:: smart pointers.
+ using element_type = T;
+
constexpr TIntrusivePtr() noexcept
{ }
@@ -148,6 +151,12 @@ public:
return T_;
}
+ //! Returns the pointer, for compatibility with std:: smart pointers.
+ T* get() const noexcept
+ {
+ return T_;
+ }
+
//! Returns the pointer and releases the ownership.
T* Release() noexcept
{
diff --git a/library/cpp/yt/memory/weak_ptr.h b/library/cpp/yt/memory/weak_ptr.h
index c647198aaf..e7b847653d 100644
--- a/library/cpp/yt/memory/weak_ptr.h
+++ b/library/cpp/yt/memory/weak_ptr.h
@@ -13,6 +13,7 @@ class TWeakPtr
{
public:
using TUnderlying = T;
+ using element_type = T;
//! Empty constructor.
TWeakPtr() = default;
diff --git a/util/generic/ptr.h b/util/generic/ptr.h
index cc2e3c0f51..703e61e68e 100644
--- a/util/generic/ptr.h
+++ b/util/generic/ptr.h
@@ -105,6 +105,11 @@ class TPointerCommon {
public:
using TValueType = T;
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ using element_type = T;
+
inline T* operator->() const noexcept {
T* ptr = AsT();
Y_ASSERT(ptr);
@@ -214,6 +219,13 @@ public:
return T_;
}
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ inline T* get() const noexcept {
+ return T_;
+ }
+
#ifdef __cpp_impl_three_way_comparison
template <class Other>
inline bool operator==(const Other& p) const noexcept {
@@ -309,6 +321,13 @@ public:
return T_;
}
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ inline T* get() const noexcept {
+ return T_;
+ }
+
inline operator TAutoPtr<T, D>() noexcept {
return Release();
}
@@ -560,6 +579,13 @@ public:
return T_;
}
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ inline T* get() const noexcept {
+ return T_;
+ }
+
inline void Swap(TIntrusivePtr& r) noexcept {
DoSwap(T_, r.T_);
}
@@ -680,6 +706,13 @@ public:
return T_;
}
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ inline const T* get() const noexcept {
+ return T_;
+ }
+
inline void Swap(TIntrusiveConstPtr& r) noexcept {
DoSwap(T_, r.T_);
}
@@ -883,6 +916,13 @@ public:
return T_;
}
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ inline T* get() const noexcept {
+ return T_;
+ }
+
inline C* ReferenceCounter() const noexcept {
return C_;
}
@@ -1105,6 +1145,13 @@ public:
return Const();
}
+ /*
+ * for compatibility with std:: smart pointers.
+ */
+ inline const T* get() const noexcept {
+ return Const();
+ }
+
inline const T* Const() const noexcept {
return T_.Get();
}
diff --git a/util/generic/ptr_ut.cpp b/util/generic/ptr_ut.cpp
index 84e11886c9..8f72cee521 100644
--- a/util/generic/ptr_ut.cpp
+++ b/util/generic/ptr_ut.cpp
@@ -35,6 +35,7 @@ class TPointerTest: public TTestBase {
UNIT_TEST(TestSimpleIntrusivePtrCtorTsan);
UNIT_TEST(TestRefCountedPtrsInHashSet);
UNIT_TEST(TestSharedPtrDowncast);
+ UNIT_TEST(TestStdCompatibility);
UNIT_TEST_SUITE_END();
private:
@@ -89,6 +90,7 @@ private:
void TestRefCountedPtrsInHashSetImpl();
void TestRefCountedPtrsInHashSet();
void TestSharedPtrDowncast();
+ void TestStdCompatibility();
};
UNIT_TEST_SUITE_REGISTRATION(TPointerTest);
@@ -934,3 +936,29 @@ void TPointerTest::TestSharedPtrDowncast() {
UNIT_ASSERT_VALUES_EQUAL(probeState.Destructors, 1);
}
}
+
+void TPointerTest::TestStdCompatibility() {
+ {
+ TSimpleSharedPtr<int> ptr = MakeSimpleShared<int>(5);
+ UNIT_ASSERT_TYPES_EQUAL(decltype(ptr)::element_type, int);
+ UNIT_ASSERT_VALUES_EQUAL(ptr.get(), ptr.Get());
+ }
+
+ {
+ TAtomicSharedPtr<int> ptr = MakeAtomicShared<int>(5);
+ UNIT_ASSERT_TYPES_EQUAL(decltype(ptr)::element_type, int);
+ UNIT_ASSERT_VALUES_EQUAL(ptr.get(), ptr.Get());
+ }
+
+ {
+ TAutoPtr<int> ptr = MakeHolder<int>(5);
+ UNIT_ASSERT_TYPES_EQUAL(decltype(ptr)::element_type, int);
+ UNIT_ASSERT_VALUES_EQUAL(ptr.get(), ptr.Get());
+ }
+
+ {
+ TIntrusivePtr<TOp> ptr;
+ UNIT_ASSERT_TYPES_EQUAL(decltype(ptr)::element_type, TOp);
+ UNIT_ASSERT_VALUES_EQUAL(ptr.get(), ptr.Get());
+ }
+}
diff --git a/yt/yt/core/misc/mpsc_fair_share_queue-inl.h b/yt/yt/core/misc/mpsc_fair_share_queue-inl.h
index 523128cfcf..4aaa28f19e 100644
--- a/yt/yt/core/misc/mpsc_fair_share_queue-inl.h
+++ b/yt/yt/core/misc/mpsc_fair_share_queue-inl.h
@@ -16,14 +16,6 @@ namespace NMpscFSQueue {
using TCookie = uintptr_t;
-// For yandex smart pointer types
-template <typename T>
- requires requires(T t) { t.Get(); }
-TCookie ToCookie(const T& value)
-{
- return reinterpret_cast<TCookie>(value.Get());
-}
-
// For std smart pointers
template <typename T>
requires requires(T t) { t.get(); }