Introduce ViewGuard: A weak ptr to View

This commit is contained in:
Waqar Ahmed
2022-04-05 17:05:17 +05:00
parent 84497f422d
commit b573d3f0f7
5 changed files with 124 additions and 0 deletions

57
src/ViewGuard.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "ViewGuard.h"
#include "View.h"
using namespace KDDockWidgets;
ViewGuard::ViewGuard(View *view)
: v(view && view->inDtor() ? nullptr : view)
{
if (v) {
m_onDestroy = v->beingDestroyed.connect([this] {
v = nullptr;
});
}
}
ViewGuard::operator bool() const
{
return !isNull();
}
bool ViewGuard::isNull() const
{
return v == nullptr;
}
View *ViewGuard::operator->()
{
return v;
}
void ViewGuard::clear()
{
v = nullptr;
}
View *ViewGuard::view() const
{
return v;
}
ViewGuard& ViewGuard::operator=(View *view)
{
if (view == v) {
return *this;
}
// Remove the pervious connection
if (m_onDestroy.isActive())
m_onDestroy.disconnect();
v = view;
m_onDestroy = v->beingDestroyed.connect([this] {
v = nullptr;
});
return *this;
}