diff options
author | Murray Cumming <murrayc@murrayc.com> | 2016-12-11 20:57:37 +0100 |
---|---|---|
committer | Murray Cumming <murrayc@murrayc.com> | 2016-12-11 21:43:41 +0100 |
commit | 313e376d1712ac78b005ac3ee61c2a46b50952b5 (patch) | |
tree | c84d201385b9d75f3bda9389f8de4d36e04ac0cd /tests | |
parent | cb35307fa9e8ccdd4d18a0f508ca8887d740c940 (diff) | |
download | glibmm-refptr_as_sharedptr_v3.tar.gz |
Remove Glib::WeakRefrefptr_as_sharedptr_v3
Now that RefPtr is really a std::shared_ptr<>, we should use
std::weak_ref<> instead.
Note that a std::weak_ptr<> tells you nothing about whether
the underlying GObject is still alive, which Glib::RefPtr did.
It just tells you whether our std::shared_ptr<> still holds
a reference to it. That's why I removed one of the checks in
tests/giomm_listmodel/main.cc.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/giomm_listmodel/main.cc | 30 | ||||
-rw-r--r-- | tests/glibmm_weakref/main.cc | 165 |
3 files changed, 10 insertions, 186 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index bbe85ae4..aba00e97 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -47,7 +47,6 @@ check_PROGRAMS = \ glibmm_null_containerhandle/test \ glibmm_refptr/test \ glibmm_refptr_sigc_bind/test \ - glibmm_weakref/test \ glibmm_bytearray/test TESTS = $(check_PROGRAMS) diff --git a/tests/giomm_listmodel/main.cc b/tests/giomm_listmodel/main.cc index 5fb8598c..6d5ebe84 100644 --- a/tests/giomm_listmodel/main.cc +++ b/tests/giomm_listmodel/main.cc @@ -39,7 +39,7 @@ void test_store_boundaries() { auto store = Gio::ListStore<Gio::MenuItem>::create(); auto item = Gio::MenuItem::create("", ""); - auto weakref_item = Glib::WeakRef<Gio::MenuItem>(item); + std::weak_ptr<Gio::MenuItem> weakref_item = item; // Remove an item from an empty list. store->remove(0); @@ -84,7 +84,7 @@ void test_store_boundaries() store.reset(); item.reset(); - if (weakref_item) + if (weakref_item.lock()) { result = EXIT_FAILURE; std::cerr << "test_store_boundaries(), 10: weakref_item is not null" << std::endl; @@ -116,16 +116,16 @@ void test_store_refcounts() const std::size_t n_items = 10; std::vector<Glib::RefPtr<Gio::MenuItem>> items; - std::vector<Glib::WeakRef<Gio::MenuItem>> weakref_items; + std::vector<std::weak_ptr<Gio::MenuItem>> weakref_items; for (std::size_t i = 0; i < n_items; ++i) { items.push_back(Gio::MenuItem::create("", "")); - weakref_items.push_back(Glib::WeakRef<Gio::MenuItem>(items[i])); + weakref_items.emplace_back(items[i]); store->append(items[i]); } check_store_refcounts_n_items(2, store, n_items); - if (store->get_item(3).operator->() != items[3].operator->()) + if (store->get_item(3).get() != items[3].get()) { result = EXIT_FAILURE; std::cerr << "test_store_refcounts(), 3: get_item(3) != items[3]" << std::endl; @@ -134,25 +134,15 @@ void test_store_refcounts() for (std::size_t i = 0; i < n_items; ++i) { items[i].reset(); - if (!weakref_items[i]) - { - result = EXIT_FAILURE; - std::cerr << "test_store_refcounts(), 4: weakref_items[" << i << "] is null" << std::endl; - } } store->remove(4); - if (weakref_items[4]) - { - result = EXIT_FAILURE; - std::cerr << "test_store_refcounts(), 5: weakref_items[4] is not null" << std::endl; - } check_store_refcounts_n_items(6, store, n_items-1); store.reset(); for (std::size_t i = 0; i < n_items; ++i) { - if (weakref_items[i]) + if (weakref_items[i].lock()) { result = EXIT_FAILURE; std::cerr << "test_store_refcounts(), 7: weakref_items[" << i << "] is not null" << std::endl; @@ -228,7 +218,7 @@ void test_store_sorted1() result = EXIT_FAILURE; std::cerr << "test_store_sorted1(), 2: i=" << i << ", items are not equal" << std::endl; } - if (a.operator->() == b.operator->()) + if (a.get() == b.get()) { result = EXIT_FAILURE; std::cerr << "test_store_sorted1(), 3: i=" << i << ", items are the same" << std::endl; @@ -237,7 +227,7 @@ void test_store_sorted1() if (i > 0) { auto c = store->get_item(i * 2 - 1); - if (c.operator->() == a.operator->() || c.operator->() == b.operator->()) + if (c.get() == a.get() || c.get() == b.get()) { result = EXIT_FAILURE; std::cerr << "test_store_sorted1(), 4: i=" << i << ", items are the same" << std::endl; @@ -311,7 +301,7 @@ void test_store_sorted2() result = EXIT_FAILURE; std::cerr << "test_store_sorted2(), 2: i=" << i << ", items are not equal" << std::endl; } - if (a.operator->() == b.operator->()) + if (a.get() == b.get()) { result = EXIT_FAILURE; std::cerr << "test_store_sorted2(), 3: i=" << i << ", items are the same" << std::endl; @@ -320,7 +310,7 @@ void test_store_sorted2() if (i > 0) { auto c = store->get_item(i * 2 - 1); - if (c.operator->() == a.operator->() || c.operator->() == b.operator->()) + if (c.get() == a.get() || c.get() == b.get()) { result = EXIT_FAILURE; std::cerr << "test_store_sorted2(), 4: i=" << i << ", items are the same" << std::endl; diff --git a/tests/glibmm_weakref/main.cc b/tests/glibmm_weakref/main.cc deleted file mode 100644 index ae8edf14..00000000 --- a/tests/glibmm_weakref/main.cc +++ /dev/null @@ -1,165 +0,0 @@ -/* Copyright (C) 2015 The glibmm Development Team - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <cstring> -#include <giomm.h> //There is no class derived from Glib::Object in glibmm -#include <glibmm.h> -#include <iostream> -#include <utility> // std::move - -int -main(int, char**) -{ - Glib::init(); - bool success = true; - - // A Glib::WeakRef cannot be created from a Glib::RefPtr<Glib::Bytes>, - // because Glib::Bytes is not derived from Glib::ObjectBase. - // const int bdata = 1234; - // Glib::RefPtr<Glib::Bytes> bytes = Glib::Bytes::create(&bdata, sizeof bdata); - // Glib::WeakRef<Glib::Bytes> weakbytes = bytes; // does not compile - - // Gio::MemoryInputStream - Glib::RefPtr<Gio::MemoryInputStream> memstream1 = Gio::MemoryInputStream::create(); - const char data[] = "Some arbitrary data"; - memstream1->add_data(data, sizeof data, Gio::MemoryInputStream::SlotDestroyData()); - - // Downcast copy, followed by upcast. - Glib::WeakRef<Gio::MemoryInputStream> weakmemstream1 = memstream1; - Glib::WeakRef<Gio::InputStream> weakstream1 = weakmemstream1; - Glib::WeakRef<Gio::MemoryInputStream> weakmemstream2 = - Glib::WeakRef<Gio::MemoryInputStream>::cast_dynamic(weakstream1); - Glib::RefPtr<Gio::MemoryInputStream> memstream2 = weakmemstream2.get(); - if (memstream2) - { - char buffer[200]; - gsize bytes_read = 0; - try - { - memstream2->read_all(buffer, sizeof buffer, bytes_read); - std::cout << buffer << std::endl; - success &= std::strcmp(buffer, data) == 0; - } - catch (const Glib::Error& ex) - { - std::cout << "Error reading from memory stream: " << ex.what() << std::endl; - success = false; - } - } - else - { - std::cout << "!memstream2" << std::endl; - success = false; - } - - // Move construction. - Glib::WeakRef<Gio::MemoryInputStream> weakmemstream3(std::move(weakmemstream1)); - if (!weakmemstream3.get()) - { - success = false; - std::cout << "weakmemstream1 || !weakmemstream3: !weakmemstream3" << std::endl; - } - else - { - // Move assignment. - weakmemstream2 = std::move(weakmemstream3); - if (!weakmemstream2) - { - success = false; - std::cout << "!weakmemstream2 || weakmemstream3: !weakmemstream2" << std::endl; - } - else - { - // Downcast move, followed by upcast. - weakstream1 = std::move(weakmemstream2); - weakmemstream1 = Glib::WeakRef<Gio::MemoryInputStream>::cast_dynamic(weakstream1); - if (!weakmemstream1) - { - success = false; - std::cout << "weakmemstream2 || !weakmemstream1: !weakmemstream1" << std::endl; - } - } - } - - // Gio::SimpleAction - Glib::RefPtr<Gio::SimpleAction> action1 = Gio::SimpleAction::create("Action1"); - - Glib::ustring name = action1->get_name(); - std::cout << "The name is '" << name << "'." << std::endl; - success &= name == "Action1"; - - Glib::WeakRef<Gio::SimpleAction> weakaction1 = action1; - Glib::WeakRef<Gio::SimpleAction> weakaction2 = weakaction1; - - // A second RefPtr - Glib::RefPtr<Gio::SimpleAction> action2 = weakaction1.get(); - if (action2) - { - name = action2->get_name(); - std::cout << "The name is '" << name << "'." << std::endl; - success &= name == "Action1"; - } - else - { - std::cout << "!action2" << std::endl; - success = false; - } - - weakaction1.reset(); - if (weakaction1.get()) - { - std::cout << "weakaction1" << std::endl; - success = false; - } - - action2 = weakaction2.get(); - if (action2) - { - name = action2->get_name(); - std::cout << "The name is '" << name << "'." << std::endl; - success &= name == "Action1"; - } - else - { - std::cout << "!action2" << std::endl; - success = false; - } - - // Reset one of the RefPtrs. One remains. - action1.reset(); - action2 = weakaction2.get(); - if (action2) - { - name = action2->get_name(); - std::cout << "The name is '" << name << "'." << std::endl; - success &= name == "Action1"; - } - else - { - std::cout << "!action2" << std::endl; - success = false; - } - - // Reset the other RefPtr as well. - action2.reset(); - if (weakaction2.get()) - { - std::cout << "weakaction2" << std::endl; - success = false; - } - - return success ? EXIT_SUCCESS : EXIT_FAILURE; -} |