From ae819b5318bf0a0a21b80d6269ef73ed8123d5d6 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sat, 9 Apr 2022 14:32:23 -0400 Subject: Rewrite PointerHolder as derived from std::shared_ptr --- libtests/pointer_holder.cc | 134 ++++++++++++++++++++++++++++++++++++++++++++- libtests/qtest/ph/ph.out | 48 ++++++++++++++++ 2 files changed, 180 insertions(+), 2 deletions(-) (limited to 'libtests') diff --git a/libtests/pointer_holder.cc b/libtests/pointer_holder.cc index 70c83aeb..f03c4257 100644 --- a/libtests/pointer_holder.cc +++ b/libtests/pointer_holder.cc @@ -65,8 +65,8 @@ callHelloWithGet(ObjectHolder const& oh) (*oh).hello(); } -int -main(int argc, char* argv[]) +void +test_ph() { std::list ol1; @@ -111,5 +111,135 @@ main(int argc, char* argv[]) std::cout << "array" << std::endl; PointerHolder o_arr1_ph(true, new Object[2]); std::cout << "goodbye" << std::endl; +} + +PointerHolder +make_object_ph() +{ + return new Object; +} + +std::shared_ptr +make_object_sp() +{ + return std::make_shared(); +} + +PointerHolder +make_object_const_ph() +{ + return new Object; +} + +std::shared_ptr +make_object_const_sp() +{ + return std::make_shared(); +} + +void +hello_ph(PointerHolder o) +{ + o->hello(); +} + +void +hello_sp(std::shared_ptr o) +{ + o->hello(); +} + +void +hello_ph_const(PointerHolder o) +{ + o->hello(); +} + +void +hello_sp_const(std::shared_ptr o) +{ + o->hello(); +} + +void +ph_sp_compat() +{ + // Ensure bidirectional compatibility between PointerHolder and + // shared_ptr. + std::cout << "compat" << std::endl; + PointerHolder ph_from_ph = make_object_ph(); + std::shared_ptr sp_from_ph = make_object_ph(); + PointerHolder ph_from_sp = make_object_sp(); + std::shared_ptr sp_from_sp = make_object_sp(); + hello_sp(ph_from_ph); + hello_ph(sp_from_ph); + hello_sp(ph_from_sp); + hello_ph(sp_from_sp); + PointerHolder ph_const_from_ph = make_object_const_ph(); + std::shared_ptr sp_const_from_ph = make_object_const_ph(); + PointerHolder ph_const_from_sp = make_object_const_sp(); + std::shared_ptr sp_const_from_sp = make_object_const_sp(); + hello_sp_const(ph_const_from_ph); + hello_ph_const(sp_const_from_ph); + hello_sp_const(ph_const_from_sp); + hello_ph_const(sp_const_from_sp); + PointerHolder arr1_ph; + { + std::cout << "initialize ph array from shared_ptr" << std::endl; + std::shared_ptr arr1( + new Object[2], std::default_delete()); + arr1_ph = arr1; + } + std::cout << "delete ph array" << std::endl; + arr1_ph = nullptr; + std::shared_ptr arr2_sp; + { + std::cout << "initialize sp array from PointerHolder" << std::endl; + PointerHolder arr2(true, new Object[2]); + arr2_sp = arr2; + } + std::cout << "delete sp array" << std::endl; + arr2_sp = nullptr; + std::cout << "end compat" << std::endl; +} + +std::list> +get_ph_list() +{ + std::list> l = { + make_object_sp(), + make_object_ph(), + }; + return l; +} + +std::list> +get_sp_list() +{ + std::list> l = { + make_object_sp(), + make_object_ph(), + }; + return l; +} + +void +ph_sp_containers() +{ + std::cout << "containers" << std::endl; + // Demonstrate that using auto makes it easy to switch interfaces + // from using a container of one shared pointer type to a + // container of the other. + auto phl1 = get_ph_list(); + auto phl2 = get_sp_list(); + std::cout << "end containers" << std::endl; +} + +int +main(int argc, char* argv[]) +{ + test_ph(); + ph_sp_compat(); + ph_sp_containers(); return 0; } diff --git a/libtests/qtest/ph/ph.out b/libtests/qtest/ph/ph.out index a7efe1bb..7b8ba935 100644 --- a/libtests/qtest/ph/ph.out +++ b/libtests/qtest/ph/ph.out @@ -30,3 +30,51 @@ destroyed Object, id 6 destroyed Object, id 5 destroyed Object, id 3 destroyed Object, id 1 +compat +created Object, id 7 +created Object, id 8 +created Object, id 9 +created Object, id 10 +calling Object::hello for 7 +calling Object::hello for 8 +calling Object::hello for 9 +calling Object::hello for 10 +created Object, id 11 +created Object, id 12 +created Object, id 13 +created Object, id 14 +calling Object::hello const for 11 +calling Object::hello const for 12 +calling Object::hello const for 13 +calling Object::hello const for 14 +initialize ph array from shared_ptr +created Object, id 15 +created Object, id 16 +delete ph array +destroyed Object, id 16 +destroyed Object, id 15 +initialize sp array from PointerHolder +created Object, id 17 +created Object, id 18 +delete sp array +destroyed Object, id 18 +destroyed Object, id 17 +end compat +destroyed Object, id 14 +destroyed Object, id 13 +destroyed Object, id 12 +destroyed Object, id 11 +destroyed Object, id 10 +destroyed Object, id 9 +destroyed Object, id 8 +destroyed Object, id 7 +containers +created Object, id 19 +created Object, id 20 +created Object, id 21 +created Object, id 22 +end containers +destroyed Object, id 21 +destroyed Object, id 22 +destroyed Object, id 19 +destroyed Object, id 20 -- cgit v1.2.3-54-g00ecf