From d60c3b214989c486b9af9e9935a52acd183b5370 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 24 Jul 2024 01:36:21 +0200 Subject: [PATCH] enable weak_bptr, add tests --- compat/borrowed-ptr-fwd.hpp | 8 +++++- compat/borrowed-ptr.hpp | 3 --- test/bptr.cpp | 52 +++++++++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/compat/borrowed-ptr-fwd.hpp b/compat/borrowed-ptr-fwd.hpp index 8fcc5dde..48c3e141 100644 --- a/compat/borrowed-ptr-fwd.hpp +++ b/compat/borrowed-ptr-fwd.hpp @@ -2,12 +2,18 @@ namespace floormat { +#define FM_BPTR_DEBUG +//#define FM_NO_WEAK_BPTR + struct bptr_base; template class bptr; template class weak_bptr; template bptr(T* ptr) -> bptr; -template bptr(const T* ptr) -> bptr; + +#ifndef FM_NO_WEAK_BPTR +template weak_bptr(const bptr& ptr) -> weak_bptr; +#endif } // namespace floormat diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 76039926..b818154c 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -1,9 +1,6 @@ #pragma once #include "borrowed-ptr-fwd.hpp" -#define FM_BPTR_DEBUG -#define FM_NO_WEAK_BPTR - #ifdef __CLION_IDE__ #define fm_bptr_assert(...) (void(__VA_ARGS__)) #elif defined FM_BPTR_DEBUG && !defined FM_NO_DEBUG diff --git a/test/bptr.cpp b/test/bptr.cpp index e9d0dff3..260b72b2 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -2,7 +2,11 @@ #include "compat/borrowed-ptr.inl" #include "compat/assert.hpp" #include "compat/defs.hpp" +#ifndef FM_NO_WEAK_BPTR +#include "compat/weak-borrowed-ptr.inl" +#endif #include +#include namespace floormat { @@ -291,7 +295,7 @@ void test8() p1 = p2; fm_assert(A_total == 2 && A_alive == 1); - p2 = move(p1); + p2 = move(p1); (void)p2; fm_assert(A_total == 2 && A_alive == 1); p1.reset(); @@ -332,7 +336,7 @@ void test9() fm_assert(p1.use_count() == 0); fm_assert(p2.use_count() == 0); fm_assert(A_total == 1 && A_alive == 0); -}; +} void test10() { @@ -390,6 +394,48 @@ void test12() fm_assert(p1.use_count() == 1); } +void test13() +{ +#ifndef FM_NO_WEAK_BPTR + auto p1 = bptr{InPlace, 13}; + fm_assert_equal(13, p1->x); + auto w1 = weak_bptr{p1}; + fm_assert(p1); fm_assert(w1.lock()); + fm_assert_equal(1u, p1.use_count()); + auto p2 = p1; + fm_assert_equal(2u, p2.use_count()); + p1 = {}; + fm_assert_equal(1u, p2.use_count()); + fm_assert(!p1); fm_assert(p2); + fm_assert(w1.lock()); + fm_assert_equal(13, w1.lock()->x); + p2 = {}; (void)p2; + fm_assert(!w1.lock()); +#endif +} + +void test14() +{ +#ifndef FM_NO_WEAK_BPTR + auto p1 = bptr{InPlace, 14}; + auto w1 = weak_bptr{p1}; + auto w2 = weak_bptr{p1}; + fm_assert_equal(14, w1.lock()->x); + fm_assert_equal(14, w2.lock()->x); + fm_assert_equal(1u, p1.use_count()); + w1 = {}; + fm_assert(p1); + fm_assert_equal(1u, p1.use_count()); + w2 = {}; (void)w2; + fm_assert(p1); + fm_assert_equal(1u, p1.use_count()); + auto w3 = weak_bptr{p1}; + fm_assert_equal(14, w3.lock()->x); + p1 = {}; (void)p1; + fm_assert(!w1.lock()); fm_assert(!w2.lock()); fm_assert(!w3.lock()); +#endif +} + } // namespace void Test::test_bptr() @@ -406,6 +452,8 @@ void Test::test_bptr() test10(); test11(); test12(); + test13(); + test14(); } } // namespace floormat