summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2018-10-13 17:00:45 +0200
committerAndreas Kling <awesomekling@gmail.com>2018-10-13 17:00:45 +0200
commit560405667e92014282c1442a2df80092af6fec61 (patch)
treea1715e487362842b2bdc29fefb48103e3e5aa684
parent16fff6dff7a59918b0e914c2bca1ab8203191799 (diff)
downloadserenity-560405667e92014282c1442a2df80092af6fec61.zip
Start using WeakPtr for some of the WindowManager window pointers.
-rw-r--r--AK/WeakPtr.h19
-rw-r--r--AK/Weakable.h5
-rw-r--r--Widgets/Object.h3
-rw-r--r--Widgets/WindowManager.cpp6
-rw-r--r--Widgets/WindowManager.h4
5 files changed, 29 insertions, 8 deletions
diff --git a/AK/WeakPtr.h b/AK/WeakPtr.h
index eab6c09d31..137bb06632 100644
--- a/AK/WeakPtr.h
+++ b/AK/WeakPtr.h
@@ -11,11 +11,28 @@ public:
WeakPtr() { }
WeakPtr(std::nullptr_t) { }
+ template<typename U>
+ WeakPtr& operator=(WeakPtr<U>&& other)
+ {
+ m_link = reinterpret_cast<WeakLink<T>*>(other.leakLink());
+ return *this;
+ }
+
operator bool() const { return ptr(); }
T* ptr() { return m_link ? m_link->ptr() : nullptr; }
const T* ptr() const { return m_link ? m_link->ptr() : nullptr; }
+
+ T* operator->() { return ptr(); }
+ const T* operator->() const { return ptr(); }
+
+ T& operator*() { return *ptr(); }
+ const T& operator*() const { return *ptr(); }
+
bool isNull() const { return !m_link || !m_link->ptr(); }
+ void clear() { m_link = nullptr; }
+
+ WeakLink<T>* leakLink() { return m_link.leakRef(); }
private:
WeakPtr(RetainPtr<WeakLink<T>>&& link) : m_link(std::move(link)) { }
@@ -27,7 +44,7 @@ template<typename T>
inline WeakPtr<T> Weakable<T>::makeWeakPtr()
{
if (!m_link)
- m_link = adopt(*new WeakLink<T>(*this));
+ m_link = adopt(*new WeakLink<T>(static_cast<T&>(*this)));
return WeakPtr<T>(m_link.copyRef());
}
diff --git a/AK/Weakable.h b/AK/Weakable.h
index bd24704961..8f47b6ac2f 100644
--- a/AK/Weakable.h
+++ b/AK/Weakable.h
@@ -2,6 +2,7 @@
#include "Assertions.h"
#include "Retainable.h"
+#include "RetainPtr.h"
namespace AK {
@@ -16,8 +17,8 @@ public:
const T* ptr() const { return static_cast<const T*>(m_ptr); }
private:
- explicit WeakLink(Weakable<T>& weakable) : m_ptr(&weakable) { }
- Weakable<T>* m_ptr;
+ explicit WeakLink(T& weakable) : m_ptr(&weakable) { }
+ T* m_ptr;
};
template<typename T>
diff --git a/Widgets/Object.h b/Widgets/Object.h
index 028515ce1a..4bf672d8ae 100644
--- a/Widgets/Object.h
+++ b/Widgets/Object.h
@@ -1,11 +1,12 @@
#pragma once
#include <AK/Vector.h>
+#include <AK/Weakable.h>
class Event;
class TimerEvent;
-class Object {
+class Object : public Weakable<Object> {
public:
Object(Object* parent = nullptr);
virtual ~Object();
diff --git a/Widgets/WindowManager.cpp b/Widgets/WindowManager.cpp
index d2cb5e5470..1cb8557e77 100644
--- a/Widgets/WindowManager.cpp
+++ b/Widgets/WindowManager.cpp
@@ -96,7 +96,7 @@ void WindowManager::paintWindowFrame(Window& window)
m_lastDragRect = Rect();
}
- if (m_dragWindow == &window) {
+ if (m_dragWindow.ptr() == &window) {
p.xorRect(outerRect, Color::Red);
m_lastDragRect = outerRect;
return;
@@ -138,7 +138,7 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
{
if (event.type() == Event::MouseDown) {
printf("[WM] Begin dragging Window{%p}\n", &window);
- m_dragWindow = &window;
+ m_dragWindow = window.makeWeakPtr();;
m_dragOrigin = event.position();
m_dragWindowOrigin = window.position();
m_dragStartRect = outerRectForWindow(window);
@@ -180,7 +180,7 @@ void WindowManager::processMouseEvent(MouseEvent& event)
{
if (event.type() == Event::MouseUp) {
if (m_dragWindow) {
- printf("[WM] Finish dragging Window{%p}\n", m_dragWindow);
+ printf("[WM] Finish dragging Window{%p}\n", m_dragWindow.ptr());
m_dragWindow->setIsBeingDragged(false);
m_dragEndRect = outerRectForWindow(*m_dragWindow);
m_dragWindow = nullptr;
diff --git a/Widgets/WindowManager.h b/Widgets/WindowManager.h
index cb43e393f5..f73a3325b0 100644
--- a/Widgets/WindowManager.h
+++ b/Widgets/WindowManager.h
@@ -4,6 +4,7 @@
#include "Rect.h"
#include "Color.h"
#include <AK/HashTable.h>
+#include <AK/WeakPtr.h>
class MouseEvent;
class PaintEvent;
@@ -41,7 +42,8 @@ private:
HashTable<Window*> m_windows;
Widget* m_rootWidget { nullptr };
- Window* m_dragWindow { nullptr };
+ WeakPtr<Window> m_dragWindow;
+
Point m_dragOrigin;
Point m_dragWindowOrigin;
Rect m_lastDragRect;