Skip to content

Commit

Permalink
libappfw: Added a smart pointer for owning a GuiWidget
Browse files Browse the repository at this point in the history
Automatically calls the correct destructor on the widget.
  • Loading branch information
skyjake committed Nov 1, 2014
1 parent c91f70d commit 2af910d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 15 additions & 2 deletions doomsday/libappfw/include/de/framework/guiwidget.h
Expand Up @@ -189,7 +189,6 @@ class LIBAPPFW_PUBLIC GuiWidget : public QObject, public Widget
*/
static void destroy(GuiWidget *widget);

GuiRootWidget &root();
GuiRootWidget &root() const;
Widget::Children childWidgets() const;
Widget *parentWidget() const;
Expand Down Expand Up @@ -297,7 +296,7 @@ class LIBAPPFW_PUBLIC GuiWidget : public QObject, public Widget
void deinitialize();
void viewResized();
void update();
void draw() /*final*/;
void draw() final;
bool handleEvent(Event const &event);

/**
Expand Down Expand Up @@ -460,6 +459,20 @@ public slots:

Q_DECLARE_OPERATORS_FOR_FLAGS(GuiWidget::Attributes)

template <typename WidgetType>
struct GuiWidgetDeleter {
void operator () (WidgetType *w) {
GuiWidget::destroy(w);
}
};

template <typename WidgetType>
class UniqueWidgetPtr : public std::unique_ptr<WidgetType, GuiWidgetDeleter<WidgetType>> {
public:
UniqueWidgetPtr(WidgetType *w = nullptr)
: std::unique_ptr<WidgetType, GuiWidgetDeleter<WidgetType>>(w) {}
};

} // namespace de

#endif // LIBAPPFW_GUIWIDGET_H
7 changes: 1 addition & 6 deletions doomsday/libappfw/src/guiwidget.cpp
Expand Up @@ -374,16 +374,11 @@ void GuiWidget::destroy(GuiWidget *widget)
delete widget;
}

GuiRootWidget &GuiWidget::root()
{
return static_cast<GuiRootWidget &>(Widget::root());
}

GuiRootWidget &GuiWidget::root() const
{
return static_cast<GuiRootWidget &>(Widget::root());
}

Widget::Children GuiWidget::childWidgets() const
{
return Widget::children();
Expand Down

0 comments on commit 2af910d

Please sign in to comment.