From e9204d4b3300bc387164de5cc0e62f06c45255f7 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 31 Jan 2023 19:50:22 +0700 Subject: [PATCH] cleanup intrusive_ptr --- develop/singles/headers/meta.hpp/meta_all.hpp | 59 +++++++++++-------- .../untests/meta_base/intrusive_ptr_tests.cpp | 7 +++ headers/meta.hpp/meta_base/intrusive_ptr.hpp | 57 ++++++++++-------- headers/meta.hpp/meta_uvalue/uvalue.hpp | 6 +- 4 files changed, 77 insertions(+), 52 deletions(-) diff --git a/develop/singles/headers/meta.hpp/meta_all.hpp b/develop/singles/headers/meta.hpp/meta_all.hpp index f2e5f94..cc9a492 100644 --- a/develop/singles/headers/meta.hpp/meta_all.hpp +++ b/develop/singles/headers/meta.hpp/meta_all.hpp @@ -729,7 +729,8 @@ namespace meta_hpp::detail template < typename Derived > void intrusive_ptr_release(const intrusive_ref_counter* ptr) { if ( ptr->counter_.fetch_sub(1, std::memory_order_acq_rel) == 1 ) { - std::unique_ptr(static_cast(ptr)).reset(); + // NOLINTNEXTLINE(*-owning-memory) + delete static_cast(ptr); } } } @@ -820,43 +821,51 @@ namespace meta_hpp::detail T* ptr_{}; }; + template < typename T > + void swap(intrusive_ptr& l, intrusive_ptr& r) noexcept { + return l.swap(r); + } + template < typename T, typename... Args > intrusive_ptr make_intrusive(Args&&... args) { - return std::make_unique(std::forward(args)...).release(); + // NOLINTNEXTLINE(*-owning-memory) + return new T(std::forward(args)...); } template < typename T > - void swap(intrusive_ptr& l, intrusive_ptr& r) noexcept { return l.swap(r); } + [[nodiscard]] bool operator==(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() == r.get(); } + template < typename T > + [[nodiscard]] bool operator!=(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() != r.get(); } template < typename T > - bool operator==(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() == r.get(); } + [[nodiscard]] bool operator==(const intrusive_ptr& l, const T* r) noexcept { return l.get() == r; } + template < typename T > + [[nodiscard]] bool operator==(const T* l, const intrusive_ptr& r) noexcept { return l == r.get(); } template < typename T > - bool operator!=(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() != r.get(); } + [[nodiscard]] bool operator!=(const intrusive_ptr& l, const T* r) noexcept { return l.get() != r; } + template < typename T > + [[nodiscard]] bool operator!=(const T* l, const intrusive_ptr& r) noexcept { return l != r.get(); } template < typename T > - bool operator==(const intrusive_ptr& l, const T* r) noexcept { return l.get() == r; } + [[nodiscard]] bool operator==(const intrusive_ptr& l, std::nullptr_t) noexcept { return !l; } + template < typename T > + [[nodiscard]] bool operator==(std::nullptr_t, const intrusive_ptr& r) noexcept { return !r; } template < typename T > - bool operator==(const T* l, const intrusive_ptr& r) noexcept { return l == r.get(); } - + [[nodiscard]] bool operator!=(const intrusive_ptr& l, std::nullptr_t) noexcept { return !!l; } template < typename T > - bool operator!=(const intrusive_ptr& l, const T* r) noexcept { return l.get() != r; } + [[nodiscard]] bool operator!=(std::nullptr_t, const intrusive_ptr& r) noexcept { return !!r; } +} +namespace std +{ template < typename T > - bool operator!=(const T* l, const intrusive_ptr& r) noexcept { return l != r.get(); } - - template < typename T > - bool operator==(const intrusive_ptr& l, std::nullptr_t) noexcept { return !l; } - - template < typename T > - bool operator==(std::nullptr_t, const intrusive_ptr& r) noexcept { return !r; } - - template < typename T > - bool operator!=(const intrusive_ptr& l, std::nullptr_t) noexcept { return !!l; } - - template < typename T > - bool operator!=(std::nullptr_t, const intrusive_ptr& r) noexcept { return !!r; } + struct hash> { + size_t operator()(const meta_hpp::detail::intrusive_ptr& ptr) const noexcept { + return hash{}(ptr.get()); + } + }; } namespace meta_hpp::detail @@ -8659,7 +8668,7 @@ namespace meta_hpp : detail::to_underlying(storage_e::internal); } else { // NOLINTNEXTLINE(*-union-access, *-owning-memory) - dst.storage_.external.ptr = ::new Tp(std::forward(args)...); + dst.storage_.external.ptr = new Tp(std::forward(args)...); dst.storage_.vtag = detail::to_underlying(storage_e::external); } @@ -8773,7 +8782,7 @@ namespace meta_hpp do_ctor(to, *src); } else { // NOLINTNEXTLINE(*-union-access, *-owning-memory) - to.storage_.external.ptr = ::new Tp(*src); + to.storage_.external.ptr = new Tp(*src); to.storage_.vtag = self.storage_.vtag; } }, @@ -8787,7 +8796,7 @@ namespace meta_hpp std::destroy_at(src); } else { // NOLINTNEXTLINE(*-owning-memory) - ::delete src; + delete src; } self.storage_.vtag = 0; diff --git a/develop/untests/meta_base/intrusive_ptr_tests.cpp b/develop/untests/meta_base/intrusive_ptr_tests.cpp index 2418f72..0cef996 100644 --- a/develop/untests/meta_base/intrusive_ptr_tests.cpp +++ b/develop/untests/meta_base/intrusive_ptr_tests.cpp @@ -212,4 +212,11 @@ TEST_CASE("meta/meta_base/intrusive_ptr") { CHECK_FALSE(ptr3 != nullptr); CHECK_FALSE(nullptr != ptr3); } + + SUBCASE("hash") { + intrusive_ptr ptr1{}; + intrusive_ptr ptr2{meta::detail::make_intrusive(42)}; + CHECK(std::hash>{}(ptr1) == std::hash{}(ptr1.get())); + CHECK(std::hash>{}(ptr2) == std::hash{}(ptr2.get())); + } } diff --git a/headers/meta.hpp/meta_base/intrusive_ptr.hpp b/headers/meta.hpp/meta_base/intrusive_ptr.hpp index 34c1312..d34282c 100644 --- a/headers/meta.hpp/meta_base/intrusive_ptr.hpp +++ b/headers/meta.hpp/meta_base/intrusive_ptr.hpp @@ -49,7 +49,8 @@ namespace meta_hpp::detail template < typename Derived > void intrusive_ptr_release(const intrusive_ref_counter* ptr) { if ( ptr->counter_.fetch_sub(1, std::memory_order_acq_rel) == 1 ) { - std::unique_ptr(static_cast(ptr)).reset(); + // NOLINTNEXTLINE(*-owning-memory) + delete static_cast(ptr); } } } @@ -140,41 +141,49 @@ namespace meta_hpp::detail T* ptr_{}; }; + template < typename T > + void swap(intrusive_ptr& l, intrusive_ptr& r) noexcept { + return l.swap(r); + } + template < typename T, typename... Args > intrusive_ptr make_intrusive(Args&&... args) { - return std::make_unique(std::forward(args)...).release(); + // NOLINTNEXTLINE(*-owning-memory) + return new T(std::forward(args)...); } template < typename T > - void swap(intrusive_ptr& l, intrusive_ptr& r) noexcept { return l.swap(r); } + [[nodiscard]] bool operator==(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() == r.get(); } + template < typename T > + [[nodiscard]] bool operator!=(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() != r.get(); } template < typename T > - bool operator==(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() == r.get(); } + [[nodiscard]] bool operator==(const intrusive_ptr& l, const T* r) noexcept { return l.get() == r; } + template < typename T > + [[nodiscard]] bool operator==(const T* l, const intrusive_ptr& r) noexcept { return l == r.get(); } template < typename T > - bool operator!=(const intrusive_ptr& l, const intrusive_ptr& r) noexcept { return l.get() != r.get(); } + [[nodiscard]] bool operator!=(const intrusive_ptr& l, const T* r) noexcept { return l.get() != r; } + template < typename T > + [[nodiscard]] bool operator!=(const T* l, const intrusive_ptr& r) noexcept { return l != r.get(); } template < typename T > - bool operator==(const intrusive_ptr& l, const T* r) noexcept { return l.get() == r; } + [[nodiscard]] bool operator==(const intrusive_ptr& l, std::nullptr_t) noexcept { return !l; } + template < typename T > + [[nodiscard]] bool operator==(std::nullptr_t, const intrusive_ptr& r) noexcept { return !r; } template < typename T > - bool operator==(const T* l, const intrusive_ptr& r) noexcept { return l == r.get(); } - + [[nodiscard]] bool operator!=(const intrusive_ptr& l, std::nullptr_t) noexcept { return !!l; } template < typename T > - bool operator!=(const intrusive_ptr& l, const T* r) noexcept { return l.get() != r; } - - template < typename T > - bool operator!=(const T* l, const intrusive_ptr& r) noexcept { return l != r.get(); } - - template < typename T > - bool operator==(const intrusive_ptr& l, std::nullptr_t) noexcept { return !l; } - - template < typename T > - bool operator==(std::nullptr_t, const intrusive_ptr& r) noexcept { return !r; } - - template < typename T > - bool operator!=(const intrusive_ptr& l, std::nullptr_t) noexcept { return !!l; } - - template < typename T > - bool operator!=(std::nullptr_t, const intrusive_ptr& r) noexcept { return !!r; } + [[nodiscard]] bool operator!=(std::nullptr_t, const intrusive_ptr& r) noexcept { return !!r; } +} + +namespace std +{ + template < typename T > + struct hash> { + size_t operator()(const meta_hpp::detail::intrusive_ptr& ptr) const noexcept { + return hash{}(ptr.get()); + } + }; } diff --git a/headers/meta.hpp/meta_uvalue/uvalue.hpp b/headers/meta.hpp/meta_uvalue/uvalue.hpp index c63bea6..e8d5df5 100644 --- a/headers/meta.hpp/meta_uvalue/uvalue.hpp +++ b/headers/meta.hpp/meta_uvalue/uvalue.hpp @@ -85,7 +85,7 @@ namespace meta_hpp : detail::to_underlying(storage_e::internal); } else { // NOLINTNEXTLINE(*-union-access, *-owning-memory) - dst.storage_.external.ptr = ::new Tp(std::forward(args)...); + dst.storage_.external.ptr = new Tp(std::forward(args)...); dst.storage_.vtag = detail::to_underlying(storage_e::external); } @@ -199,7 +199,7 @@ namespace meta_hpp do_ctor(to, *src); } else { // NOLINTNEXTLINE(*-union-access, *-owning-memory) - to.storage_.external.ptr = ::new Tp(*src); + to.storage_.external.ptr = new Tp(*src); to.storage_.vtag = self.storage_.vtag; } }, @@ -213,7 +213,7 @@ namespace meta_hpp std::destroy_at(src); } else { // NOLINTNEXTLINE(*-owning-memory) - ::delete src; + delete src; } self.storage_.vtag = 0;