From 3f503fd32e456926f271b7d4bc485c91adc7b716 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 5 May 2024 01:38:07 +0200 Subject: aa --- compat/borrowed-ptr.inl | 3 ++- test/bptr.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index 05724fcd..a29e6b1c 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -215,6 +215,7 @@ template T* bptr::get() const noexcept else return nullptr; } + template T* bptr::operator->() const noexcept { auto* ret = get(); @@ -236,7 +237,7 @@ void bptr::swap(bptr& other) noexcept template uint32_t bptr::use_count() const noexcept { - if (blk) [[likely]] + if (blk && blk->_ptr) [[likely]] return blk->_count; else return 0; diff --git a/test/bptr.cpp b/test/bptr.cpp index 55b10fe0..6050c4f5 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -1,6 +1,7 @@ #include "app.hpp" -#include "compat/assert.hpp" #include "compat/borrowed-ptr.inl" +#include "compat/assert.hpp" +#include "compat/defs.hpp" #include namespace floormat { @@ -47,6 +48,8 @@ struct A int val, serial; explicit A(int val) : val{val}, serial{++A_total} { ++A_alive; } ~A() noexcept { --A_alive; fm_assert(A_alive >= 0); } + + fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(A); }; void test1() @@ -277,10 +280,62 @@ void test7() check_empty(p1); check_empty(p2); fm_assert(A_total == 1 && A_alive == 0); +} +void test8() +{ A_total = 0; A_alive = 0; + + auto p1 = bptr{InPlace, 81}; + auto p2 = bptr{InPlace, 82}; + fm_assert(A_total == 2 && A_alive == 2); + + p1 = p2; + fm_assert(A_total == 2 && A_alive == 1); + + p2 = move(p1); + fm_assert(A_total == 2 && A_alive == 1); + + p1.reset(); + p2 = nullptr; + (void)p2; + fm_assert(A_total == 2 && A_alive == 0); } +void test9() +{ + A_total = 0; A_alive = 0; + + auto p1 = bptr{InPlace, 9}; + auto p2 = p1; + p1 = p2; + fm_assert(p1.use_count() == 2); + fm_assert(A_total == 1); + fm_assert(A_alive == 1); + + p1.destroy(); + fm_assert(!p1); + fm_assert(!p2); + fm_assert(p1.use_count() == 0); + fm_assert(p2.use_count() == 0); + fm_assert(A_total == 1); + fm_assert(A_alive == 0); + + p1.swap(p2); + fm_assert(!p1 && !p2); + fm_assert(p1.use_count() == 0 && p2.use_count() == 0); + fm_assert(A_total == 1); + fm_assert(A_alive == 0); + + p1.reset(); + fm_assert(p1.use_count() == 0); + fm_assert(p2.use_count() == 0); + p2 = bptr{(A*)nullptr}; + fm_assert(p1.use_count() == 0); + fm_assert(p2.use_count() == 0); + fm_assert(A_total == 1 && A_alive == 0); +}; + } // namespace void test_app::test_bptr() @@ -292,6 +347,8 @@ void test_app::test_bptr() test5(); test6(); test7(); + test8(); + test9(); } } // namespace floormat -- cgit v1.2.3