diff options
author | Jay Berkenbilt <ejb@ql.org> | 2022-04-09 20:32:23 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2022-04-09 23:33:29 +0200 |
commit | ae819b5318bf0a0a21b80d6269ef73ed8123d5d6 (patch) | |
tree | dfebb1199f18c992183576b7a7d9183dc88f8346 /libtests | |
parent | ec219100668357e46cc8640386e2332379956b7c (diff) | |
download | qpdf-ae819b5318bf0a0a21b80d6269ef73ed8123d5d6.tar.zst |
Rewrite PointerHolder as derived from std::shared_ptr
Diffstat (limited to 'libtests')
-rw-r--r-- | libtests/pointer_holder.cc | 134 | ||||
-rw-r--r-- | libtests/qtest/ph/ph.out | 48 |
2 files changed, 180 insertions, 2 deletions
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<ObjectHolder> ol1; @@ -111,5 +111,135 @@ main(int argc, char* argv[]) std::cout << "array" << std::endl; PointerHolder<Object> o_arr1_ph(true, new Object[2]); std::cout << "goodbye" << std::endl; +} + +PointerHolder<Object> +make_object_ph() +{ + return new Object; +} + +std::shared_ptr<Object> +make_object_sp() +{ + return std::make_shared<Object>(); +} + +PointerHolder<Object const> +make_object_const_ph() +{ + return new Object; +} + +std::shared_ptr<Object const> +make_object_const_sp() +{ + return std::make_shared<Object const>(); +} + +void +hello_ph(PointerHolder<Object> o) +{ + o->hello(); +} + +void +hello_sp(std::shared_ptr<Object> o) +{ + o->hello(); +} + +void +hello_ph_const(PointerHolder<Object const> o) +{ + o->hello(); +} + +void +hello_sp_const(std::shared_ptr<Object const> o) +{ + o->hello(); +} + +void +ph_sp_compat() +{ + // Ensure bidirectional compatibility between PointerHolder and + // shared_ptr. + std::cout << "compat" << std::endl; + PointerHolder<Object> ph_from_ph = make_object_ph(); + std::shared_ptr<Object> sp_from_ph = make_object_ph(); + PointerHolder<Object> ph_from_sp = make_object_sp(); + std::shared_ptr<Object> 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<Object const> ph_const_from_ph = make_object_const_ph(); + std::shared_ptr<Object const> sp_const_from_ph = make_object_const_ph(); + PointerHolder<Object const> ph_const_from_sp = make_object_const_sp(); + std::shared_ptr<Object const> 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<Object> arr1_ph; + { + std::cout << "initialize ph array from shared_ptr" << std::endl; + std::shared_ptr<Object> arr1( + new Object[2], std::default_delete<Object[]>()); + arr1_ph = arr1; + } + std::cout << "delete ph array" << std::endl; + arr1_ph = nullptr; + std::shared_ptr<Object> arr2_sp; + { + std::cout << "initialize sp array from PointerHolder" << std::endl; + PointerHolder<Object> 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<PointerHolder<Object>> +get_ph_list() +{ + std::list<PointerHolder<Object>> l = { + make_object_sp(), + make_object_ph(), + }; + return l; +} + +std::list<std::shared_ptr<Object>> +get_sp_list() +{ + std::list<std::shared_ptr<Object>> 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 |