From ae26012bf56618f2f9f48314e03d9b30805775d4 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Thu, 16 Aug 2018 04:57:55 +0200 Subject: [PATCH] WLink: implement copy operations swap-based implementation not sure if attachTo() should be noexcept --- src/gui/model/w-link.hpp | 72 ++++++++++++++++++++++++--------- tests/gui/model/w-link-test.cpp | 20 ++------- wiki/thinkPad.ichthyo.mm | 28 ++++++------- 3 files changed, 70 insertions(+), 50 deletions(-) diff --git a/src/gui/model/w-link.hpp b/src/gui/model/w-link.hpp index 5fa4d419d..552f2e9c9 100644 --- a/src/gui/model/w-link.hpp +++ b/src/gui/model/w-link.hpp @@ -52,19 +52,14 @@ #include "lib/error.hpp" -//#include "lib/symbol.hpp" #include #include -//#include -//#include namespace gui { namespace model { -// using std::string; -// using lib::Symbol; using lumiera::error::LUMIERA_ERROR_BOTTOM_VALUE; @@ -93,11 +88,39 @@ namespace model { { } explicit - WLink(TAR& targetWidget) + WLink (TAR& targetWidget) : widget_{attachTo (targetWidget)} { } - ////////////////////////////TODO copy operations + WLink (WLink const& r) + : widget_{r.widget_? attachTo(*r.widget_) : nullptr} + { } + + WLink (WLink && rr) + : widget_{rr.widget_? attachTo(*rr.widget_) : nullptr} + { + rr.clear(); + } + + WLink& + operator= (WLink other) + { + swap (*this, other); + return *this; + } + + friend void + swap (WLink& l, WLink& r) + { + TAR* tl = l.widget_; + TAR* tr = r.widget_; + if (tl == tr) return; + l.clear(); + r.clear(); + if (tr) l.widget_ = l.attachTo (*tr); + if (tl) r.widget_ = r.attachTo (*tl); + } + explicit operator bool() const @@ -122,17 +145,20 @@ namespace model { /** detach and deactivate this link */ void - clear() + clear() noexcept { if (widget_) - widget_->remove_destroy_notify_callback (&widget_); + try { + widget_->remove_destroy_notify_callback (&widget_); + } + catch(...) { } widget_ = nullptr; } /** (re)connect this smart link to the given target. * Any previously existing link is detached beforehand */ void - connect (TAR& otherTarget) + connect (TAR& otherTarget) noexcept { if (widget_ == &otherTarget) return; clear(); @@ -152,17 +178,23 @@ namespace model { * to detach this link in case the target is destroyed */ TAR* - attachTo (TAR& target) + attachTo (TAR& target) noexcept { - target.add_destroy_notify_callback (&widget_ - ,[](void* p) - { - TAR* & widgetPtr = *static_cast(p); - ASSERT (widgetPtr); - widgetPtr = nullptr; - return p; - }); - return ⌖ + try { + target.add_destroy_notify_callback (&widget_ + ,[](void* p) + { + TAR* & widgetPtr = *static_cast(p); + ASSERT (widgetPtr); + widgetPtr = nullptr; + return p; + }); + return ⌖ + } + catch(...) + { + return nullptr; + } } }; diff --git a/tests/gui/model/w-link-test.cpp b/tests/gui/model/w-link-test.cpp index fa157337c..6ae177ed0 100644 --- a/tests/gui/model/w-link-test.cpp +++ b/tests/gui/model/w-link-test.cpp @@ -28,33 +28,21 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "gui/model/w-link.hpp" -//#include "gen-node-location-query.hpp" -#include "lib/format-cout.hpp" -//#include "lib/idi/entry-id.hpp" -//#include "lib/diff/gen-node.hpp" #include "lib/util.hpp" #include #include -//using std::string; -//using lib::diff::MakeRec; -//using lib::diff::Rec; -//using lib::idi::EntryID; -//using lib::diff::GenNode; using util::isSameObject; using std::make_unique; using std::move; -//using util::isnil; namespace gui { namespace model{ namespace test { -// using lumiera::error::LUMIERA_ERROR_WRONG_TYPE; -// using lib::test::showSizeof; namespace { // Test fixture... @@ -70,8 +58,7 @@ namespace test { /******************************************************************************//** - * @test verify the usage pattern of low-level UI element access, based on a - * mock implementation of the accessor directory. + * @test verify proper behaviour of a smart-link to a `sigc::trackable` GTK widget. * @see w-link.hpp */ class WLink_test : public Test @@ -108,6 +95,7 @@ namespace test { } + /** @test registration is automatically maintained when re-assigning targets */ void verify_reconnect() { @@ -202,12 +190,12 @@ namespace test { auto uu = make_unique(); WLink lu{*uu}; -///////////////does not compile... +///////////////does not compile... // l1 = uu; // l1.connect(*uu); // but it is a compile time check... - l1 = reinterpret_cast&&> (uu); + l1 = reinterpret_cast&&> (lu); CHECK ((int)uu->val == l1->val); CHECK (not lu); } diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 621b75948..cc4018dac 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -3188,20 +3188,20 @@ - - + + - - + + - - + + - - + + @@ -3223,14 +3223,14 @@ - - + + - - + + - - + +