From 05b3e8cdc7f6cf7056c96d9cd150a0e2a8fb85a4 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 28 Oct 2016 17:55:06 +0200 Subject: Rewrite event handler to use shared_ptr Events instead of raw pointers. --- plugingui/eventhandler.cc | 27 ++++++++------------------- plugingui/eventhandler.h | 5 +++-- plugingui/nativewindow.h | 5 +++-- plugingui/nativewindow_win32.cc | 22 +++++++++++----------- plugingui/nativewindow_win32.h | 6 +++--- plugingui/nativewindow_x11.cc | 23 ++++++++++++----------- plugingui/nativewindow_x11.h | 6 +++--- 7 files changed, 43 insertions(+), 51 deletions(-) (limited to 'plugingui') diff --git a/plugingui/eventhandler.cc b/plugingui/eventhandler.cc index 62ffb2b..083efae 100644 --- a/plugingui/eventhandler.cc +++ b/plugingui/eventhandler.cc @@ -43,12 +43,12 @@ bool EventHandler::hasEvent() return nativeWindow.hasEvent(); } -Event *EventHandler::getNextEvent() +std::shared_ptr EventHandler::getNextEvent() { return nativeWindow.getNextEvent(); } -Event *EventHandler::peekNextEvent() +std::shared_ptr EventHandler::peekNextEvent() { return nativeWindow.peekNextEvent(); } @@ -87,16 +87,13 @@ void EventHandler::processEvents() } if(peekEvent->type() != EventType::resize) { - //delete peekEvent; break; } - //delete peekEvent; - //delete event; event = getNextEvent(); } - auto resizeEvent = static_cast(event); + auto resizeEvent = static_cast(event.get()); if((resizeEvent->width != window.width()) || (resizeEvent->height != window.height())) { @@ -121,16 +118,13 @@ void EventHandler::processEvents() } if(peekEvent->type() != EventType::mouseMove) { - //delete peekEvent; break; } - //delete peekEvent; - //delete event; event = getNextEvent(); } - auto moveEvent = static_cast(event); + auto moveEvent = static_cast(event.get()); auto widget = window.find(moveEvent->x, moveEvent->y); auto oldwidget = window.mouseFocus(); @@ -178,7 +172,7 @@ void EventHandler::processEvents() continue; } - auto buttonEvent = static_cast(event); + auto buttonEvent = static_cast(event.get()); lastWasDoubleClick = buttonEvent->doubleClick; @@ -236,19 +230,16 @@ void EventHandler::processEvents() } if(peekEvent->type() != EventType::scroll) { - //delete peekEvent; break; } - //delete peekEvent; - auto scrollEvent = static_cast(event); + auto scrollEvent = static_cast(event.get()); delta += scrollEvent->delta; - //delete event; event = getNextEvent(); } - auto scrollEvent = static_cast(event); + auto scrollEvent = static_cast(event.get()); scrollEvent->delta += delta; auto widget = window.find(scrollEvent->x, scrollEvent->y); @@ -267,7 +258,7 @@ void EventHandler::processEvents() // TODO: Filter out multiple arrow events. - auto keyEvent = static_cast(event); + auto keyEvent = static_cast(event.get()); if(window.keyboardFocus()) { window.keyboardFocus()->keyEvent(keyEvent); @@ -279,8 +270,6 @@ void EventHandler::processEvents() closeNotifier(); break; } - - delete event; } } diff --git a/plugingui/eventhandler.h b/plugingui/eventhandler.h index 490c515..6269baf 100644 --- a/plugingui/eventhandler.h +++ b/plugingui/eventhandler.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include "guievent.h" #include "nativewindow.h" @@ -47,11 +48,11 @@ public: //! \brief Get a single event from the event queue. //! \return A pointer to the event or nullptr if there are none. - Event *getNextEvent(); + std::shared_ptr getNextEvent(); //! \brief Get a single event from the event queue without popping it. //! \return A pointer to the event or nullptr if there are none. - Event *peekNextEvent(); + std::shared_ptr peekNextEvent(); Notifier<> closeNotifier; diff --git a/plugingui/nativewindow.h b/plugingui/nativewindow.h index a7d101e..ff24fed 100644 --- a/plugingui/nativewindow.h +++ b/plugingui/nativewindow.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include "guievent.h" @@ -74,11 +75,11 @@ public: //! \brief Read a single event from the event queue. //! \return A pointer to the event or nullptr is none exists. - virtual Event *getNextEvent() = 0; + virtual std::shared_ptr getNextEvent() = 0; //! \brief Read next event without popping it from the event queue. //! \return A pointer to the event or nullptr is none exists. - virtual Event* peekNextEvent() = 0; + virtual std::shared_ptr peekNextEvent() = 0; }; } // GUI:: diff --git a/plugingui/nativewindow_win32.cc b/plugingui/nativewindow_win32.cc index f49f3c6..b318f59 100644 --- a/plugingui/nativewindow_win32.cc +++ b/plugingui/nativewindow_win32.cc @@ -52,7 +52,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, static bool first = true; if(!first) { - ResizeEvent* resizeEvent = new ResizeEvent(); + auto resizeEvent = std::make_shared(); resizeEvent->width = LOWORD(lp); resizeEvent->height = HIWORD(lp); native->event_queue.push(resizeEvent); @@ -63,7 +63,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, case WM_MOVE: { -// MoveEvent* moveEvent = new MoveEvent(); +// auto moveEvent = std::make_shared(); // moveEvent->x = (short)LOWORD(lp); // moveEvent->y = (short)HIWORD(lp); // native->event_queue.push(moveEvent); @@ -72,7 +72,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, case WM_CLOSE: { - CloseEvent* closeEvent = new CloseEvent(); + auto closeEvent = std::make_shared(); native->event_queue.push(closeEvent); } break; @@ -91,7 +91,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, // return 0; case WM_MOUSEMOVE: { - MouseMoveEvent* mouseMoveEvent = new MouseMoveEvent(); + auto mouseMoveEvent = std::make_shared(); mouseMoveEvent->x = (short)LOWORD(lp); mouseMoveEvent->y = (short)HIWORD(lp); native->event_queue.push(mouseMoveEvent); @@ -100,7 +100,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, case WM_MOUSEWHEEL: { - ScrollEvent* scrollEvent = new ScrollEvent(); + auto scrollEvent = std::make_shared(); // NOTE: lp is coordinates in screen space, not client space. POINT p; @@ -125,7 +125,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, case WM_MBUTTONDBLCLK: case WM_MBUTTONDOWN: { - ButtonEvent* buttonEvent = new ButtonEvent(); + auto buttonEvent = std::make_shared(); buttonEvent->x = (short)LOWORD(lp); buttonEvent->y = (short)HIWORD(lp); @@ -178,7 +178,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, case WM_KEYDOWN: case WM_KEYUP: { - KeyEvent* keyEvent = new KeyEvent(); + auto keyEvent = std::make_shared(); switch(wp) { case VK_LEFT: keyEvent->keycode = Key::left; break; @@ -207,7 +207,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, { if(wp >= ' ') // Filter control chars. { - KeyEvent* keyEvent = new KeyEvent(); + auto keyEvent = std::make_shared(); keyEvent->keycode = Key::character; keyEvent->text += (char)wp; keyEvent->direction = Direction::up; @@ -218,7 +218,7 @@ LRESULT CALLBACK NativeWindowWin32::dialogProc(HWND hwnd, UINT msg, case WM_PAINT: { - RepaintEvent* repaintEvent = new RepaintEvent(); + auto repaintEvent = std::make_shared(); repaintEvent->x = 0; repaintEvent->y = 0; repaintEvent->width = 100; @@ -417,7 +417,7 @@ bool NativeWindowWin32::hasEvent() return false; } -Event* NativeWindowWin32::getNextEvent() +std::shared_ptr NativeWindowWin32::getNextEvent() { if(!event_queue.empty()) { @@ -447,7 +447,7 @@ Event* NativeWindowWin32::getNextEvent() return event; } -Event* NativeWindowWin32::peekNextEvent() +std::shared_ptr NativeWindowWin32::peekNextEvent() { if(!event_queue.empty()) { diff --git a/plugingui/nativewindow_win32.h b/plugingui/nativewindow_win32.h index 3d708b8..110085c 100644 --- a/plugingui/nativewindow_win32.h +++ b/plugingui/nativewindow_win32.h @@ -55,8 +55,8 @@ public: void grabMouse(bool grab) override; bool hasEvent() override; - Event* getNextEvent() override; - Event* peekNextEvent() override; + std::shared_ptr getNextEvent() override; + std::shared_ptr peekNextEvent() override; private: static LRESULT CALLBACK dialogProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); @@ -65,7 +65,7 @@ private: Window& window; WNDID m_hwnd = 0; char* m_className = nullptr; - std::queue event_queue; + std::queue> event_queue; }; } // GUI:: diff --git a/plugingui/nativewindow_x11.cc b/plugingui/nativewindow_x11.cc index b0188e1..f50551e 100644 --- a/plugingui/nativewindow_x11.cc +++ b/plugingui/nativewindow_x11.cc @@ -354,7 +354,7 @@ bool NativeWindowX11::hasEvent() return XPending(display); } -Event* NativeWindowX11::getNextEvent() +std::shared_ptr NativeWindowX11::getNextEvent() { if(display == nullptr) { @@ -366,7 +366,7 @@ Event* NativeWindowX11::getNextEvent() return translateXMessage(xEvent); } -Event* NativeWindowX11::peekNextEvent() +std::shared_ptr NativeWindowX11::peekNextEvent() { if(display == nullptr) { @@ -378,14 +378,15 @@ Event* NativeWindowX11::peekNextEvent() return translateXMessage(peekXEvent, true); } -Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) +std::shared_ptr NativeWindowX11::translateXMessage(XEvent& xevent, + bool peek) { - Event* event = nullptr; + std::shared_ptr event = nullptr; switch(xevent.type) { case MotionNotify: { - auto mouseMoveEvent = new MouseMoveEvent(); + auto mouseMoveEvent = std::make_shared(); mouseMoveEvent->window_id = xevent.xmotion.window; mouseMoveEvent->x = xevent.xmotion.x; mouseMoveEvent->y = xevent.xmotion.y; @@ -396,7 +397,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) case Expose: if(xevent.xexpose.count == 0) { - auto repaintEvent = new RepaintEvent(); + auto repaintEvent = std::make_shared(); repaintEvent->window_id = xevent.xexpose.window; repaintEvent->x = xevent.xexpose.x; repaintEvent->y = xevent.xexpose.y; @@ -408,7 +409,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) case ConfigureNotify: { - auto resizeEvent = new ResizeEvent(); + auto resizeEvent = std::make_shared(); resizeEvent->window_id = xevent.xconfigure.window; //resizeEvent->x = xevent.xconfigure.x; //resizeEvent->y = xevent.xconfigure.y; @@ -424,7 +425,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) if((xevent.xbutton.button == 4) || (xevent.xbutton.button == 5)) { int scroll = 1; - auto scrollEvent = new ScrollEvent(); + auto scrollEvent = std::make_shared(); scrollEvent->window_id = xevent.xbutton.window; scrollEvent->x = xevent.xbutton.x; scrollEvent->y = xevent.xbutton.y; @@ -433,7 +434,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) } else { - auto buttonEvent = new ButtonEvent(); + auto buttonEvent = std::make_shared(); buttonEvent->window_id = xevent.xbutton.window; buttonEvent->x = xevent.xbutton.x; buttonEvent->y = xevent.xbutton.y; @@ -474,7 +475,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) case KeyPress: case KeyRelease: { - auto keyEvent = new KeyEvent(); + auto keyEvent = std::make_shared(); keyEvent->window_id = xevent.xkey.window; switch(xevent.xkey.keycode) { @@ -512,7 +513,7 @@ Event* NativeWindowX11::translateXMessage(XEvent& xevent, bool peek) case ClientMessage: if(((unsigned int)xevent.xclient.data.l[0] == wmDeleteMessage)) { - auto closeEvent = new CloseEvent(); + auto closeEvent = std::make_shared(); event = closeEvent; } break; diff --git a/plugingui/nativewindow_x11.h b/plugingui/nativewindow_x11.h index 2d99556..50cd904 100644 --- a/plugingui/nativewindow_x11.h +++ b/plugingui/nativewindow_x11.h @@ -49,11 +49,11 @@ public: void redraw() override; void grabMouse(bool grab) override; bool hasEvent() override; - Event* getNextEvent() override; - Event* peekNextEvent() override; + std::shared_ptr getNextEvent() override; + std::shared_ptr peekNextEvent() override; private: - Event* translateXMessage(XEvent& xevent, bool peek = false); + std::shared_ptr translateXMessage(XEvent& xevent, bool peek = false); XImage* createImageFromBuffer(unsigned char* buf, int width, int height); ::Window xwindow{0}; -- cgit v1.2.3