WLink: implement copy operations

swap-based implementation
not sure if attachTo() should be noexcept
This commit is contained in:
Fischlurch 2018-08-16 04:57:55 +02:00
parent baa3d2bff0
commit ae26012bf5
3 changed files with 70 additions and 50 deletions

View file

@ -52,19 +52,14 @@
#include "lib/error.hpp"
//#include "lib/symbol.hpp"
#include <type_traits>
#include <sigc++/trackable.h>
//#include <utility>
//#include <string>
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<TAR**>(p);
ASSERT (widgetPtr);
widgetPtr = nullptr;
return p;
});
return &target;
try {
target.add_destroy_notify_callback (&widget_
,[](void* p)
{
TAR* & widgetPtr = *static_cast<TAR**>(p);
ASSERT (widgetPtr);
widgetPtr = nullptr;
return p;
});
return &target;
}
catch(...)
{
return nullptr;
}
}
};

View file

@ -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 <utility>
#include <memory>
//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<Wuint>();
WLink<Wuint> lu{*uu};
///////////////does not compile...
///////////////does not compile...
// l1 = uu;
// l1.connect(*uu);
// but it is a compile time check...
l1 = reinterpret_cast<WLink<Wint>&&> (uu);
l1 = reinterpret_cast<WLink<Wint>&&> (lu);
CHECK ((int)uu->val == l1->val);
CHECK (not lu);
}

View file

@ -3188,20 +3188,20 @@
<node COLOR="#338800" CREATED="1534334964700" ID="ID_1509791330" MODIFIED="1534343003956" TEXT="dereferenzierbar">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534335220066" ID="ID_132223755" MODIFIED="1534335235369" TEXT="target anbinden">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1534335220066" ID="ID_132223755" MODIFIED="1534388600416" TEXT="target anbinden">
<icon BUILTIN="button_ok"/>
<node CREATED="1534335240246" ID="ID_1480460700" MODIFIED="1534336108969" TEXT="sigc::trackable">
<icon BUILTIN="info"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534336110858" ID="ID_280796006" MODIFIED="1534336115810" TEXT="connect()">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1534336110858" ID="ID_280796006" MODIFIED="1534388602201" TEXT="connect()">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534336116617" ID="ID_1274372728" MODIFIED="1534336119507" TEXT="clear()">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1534336116617" ID="ID_1274372728" MODIFIED="1534388603779" TEXT="clear()">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534335253941" ID="ID_269300171" MODIFIED="1534335534298" TEXT="magisch kopierbar">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1534335253941" ID="ID_269300171" MODIFIED="1534388596484" TEXT="magisch kopierbar">
<icon BUILTIN="button_ok"/>
</node>
</node>
<node CREATED="1534336069415" ID="ID_1775411301" MODIFIED="1534337958030">
@ -3223,14 +3223,14 @@
<node COLOR="#338800" CREATED="1534340031277" ID="ID_506504727" MODIFIED="1534341378412" TEXT="test-dummy-trackable">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534340016027" ID="ID_109515645" MODIFIED="1534342990871" TEXT="verify_standardUsage">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1534340016027" ID="ID_109515645" MODIFIED="1534388606258" TEXT="verify_standardUsage">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534340023288" ID="ID_1266509201" MODIFIED="1534340043884" TEXT="verify_reconnect">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1534340023288" ID="ID_1266509201" MODIFIED="1534388607832" TEXT="verify_reconnect">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1534340028094" ID="ID_1406183687" MODIFIED="1534340044144" TEXT="verify_copy">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1534340028094" ID="ID_1406183687" MODIFIED="1534388609833" TEXT="verify_copy">
<icon BUILTIN="button_ok"/>
</node>
</node>
</node>