ViewGuard: Fix ctor and assign operator having different behaviours
The difference was very small: The ctor honoured View::inDtor() while the assign op didn't. It's easy to make them behave the same by creating a setView() method As a side-effect the duplicate connect() is also gone now
This commit is contained in:
@@ -15,13 +15,8 @@
|
||||
using namespace KDDockWidgets;
|
||||
|
||||
ViewGuard::ViewGuard(View *view)
|
||||
: v(view && view->inDtor() ? nullptr : view)
|
||||
{
|
||||
if (v) {
|
||||
m_onDestroy = v->beingDestroyed.connect([this] {
|
||||
v = nullptr;
|
||||
});
|
||||
}
|
||||
setView(view);
|
||||
}
|
||||
|
||||
ViewGuard::operator bool() const
|
||||
@@ -53,15 +48,27 @@ View *ViewGuard::view() const
|
||||
|
||||
ViewGuard &ViewGuard::operator=(View *view)
|
||||
{
|
||||
if (view == v)
|
||||
return *this;
|
||||
setView(view);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ViewGuard::setView(View *view)
|
||||
{
|
||||
if (view == v)
|
||||
return;
|
||||
|
||||
if (view && view->inDtor()) {
|
||||
// We don't care about views that are already being in DTOR. They count as already deleted for what's ViewGuard concerned.
|
||||
// This is rare anway, would need to require some reentrancy.
|
||||
view = nullptr;
|
||||
}
|
||||
|
||||
// Remove the previous connection
|
||||
clear();
|
||||
v = view;
|
||||
|
||||
m_onDestroy = v->beingDestroyed.connect([this] {
|
||||
v = nullptr;
|
||||
});
|
||||
return *this;
|
||||
if (v) {
|
||||
m_onDestroy = v->beingDestroyed.connect([this] {
|
||||
v = nullptr;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ public:
|
||||
ViewGuard &operator=(View *);
|
||||
|
||||
private:
|
||||
void setView(View *);
|
||||
View *v = nullptr;
|
||||
KDBindings::ConnectionHandle m_onDestroy;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user