From 0fb27fd12e3d476350fd65876db0621ead4400d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Fri, 5 Apr 2013 09:41:25 +0200 Subject: [PATCH] Defines to create the boilerplate code for KWin's singleton classes The define KWIN_SINGLETON adds to a class definition: public: static Foo *create(QObject *parent = 0); static Foo *self() { return s_self; } protected: explicit Foo(QObject *parent = 0); private: static Foo *s_self; There is an additional define KWIN_SINGLETON_VARIABLE to set a different name than s_self. The define KWIN_SINGLETON_FACTORY can be used to generate the create method. It expands to: Foo *Foo::s_self = 0; Foo *Foo::create(QObject *parent) { Q_ASSERT(!s_self); s_self = new Foo(parent); return s_self; } In addition there are defines to again set a different variable name and to create an object of another inheriting class. All the classes currently using this pattern are adjusted to use these new defines. In a few places the name was adjusted. E.g. in Compositor the factory method was called createCompositor instead of create. REVIEW: 109865 --- activities.cpp | 8 +------- activities.h | 13 +++---------- appmenu.cpp | 9 +-------- appmenu.h | 14 +++----------- composite.cpp | 8 +------- composite.h | 33 ++++----------------------------- cursor.cpp | 9 +-------- cursor.h | 16 +++------------- focuschain.cpp | 9 +-------- focuschain.h | 28 ++++------------------------ libkwineffects/kwinglobals.h | 23 +++++++++++++++++++++++ placement.cpp | 13 +++---------- placement.h | 26 ++++++-------------------- screenedge.cpp | 9 +-------- screenedge.h | 22 +--------------------- virtualdesktops.cpp | 9 +-------- virtualdesktops.h | 26 +++----------------------- workspace.cpp | 2 +- 18 files changed, 61 insertions(+), 216 deletions(-) diff --git a/activities.cpp b/activities.cpp index e48dc33dd3..e6f83533be 100644 --- a/activities.cpp +++ b/activities.cpp @@ -34,14 +34,8 @@ along with this program. If not, see . namespace KWin { -Activities *Activities::s_self = NULL; -Activities *Activities::create(QObject *parent) -{ - Q_ASSERT(!s_self); - s_self = new Activities(parent); - return s_self; -} +KWIN_SINGLETON_FACTORY(Activities) Activities::Activities(QObject *parent) : QObject(parent) diff --git a/activities.h b/activities.h index 12e97f2b85..8717611656 100644 --- a/activities.h +++ b/activities.h @@ -20,6 +20,8 @@ along with this program. If not, see . #ifndef KWIN_ACTIVITIES_H #define KWIN_ACTIVITIES_H +#include + #include #include @@ -54,8 +56,6 @@ public: const QString ¤t() const; const QString &previous() const; - static Activities *self(); - static Activities *create(QObject *parent); static QString nullUuid(); Q_SIGNALS: @@ -85,22 +85,15 @@ private Q_SLOTS: void handleReply(); private: - explicit Activities(QObject *parent); QStringList m_running; QStringList m_all; QString m_current; QString m_previous; KActivities::Controller *m_controller; - static Activities *s_self; + KWIN_SINGLETON(Activities) }; -inline -Activities *Activities::self() -{ - return s_self; -} - inline const QStringList &Activities::all() const { diff --git a/appmenu.cpp b/appmenu.cpp index ffdffea87e..6696c9483e 100644 --- a/appmenu.cpp +++ b/appmenu.cpp @@ -32,14 +32,7 @@ static const char *KDED_SERVICE = "org.kde.kded"; static const char *KDED_APPMENU_PATH = "/modules/appmenu"; static const char *KDED_INTERFACE = "org.kde.kded"; -ApplicationMenu *ApplicationMenu::s_self = NULL; - -ApplicationMenu *ApplicationMenu::create(QObject *parent) -{ - Q_ASSERT(!s_self); - s_self = new ApplicationMenu(parent); - return s_self; -} +KWIN_SINGLETON_FACTORY(ApplicationMenu) ApplicationMenu::ApplicationMenu(QObject *parent) : QObject(parent) diff --git a/appmenu.h b/appmenu.h index 868dfc35e3..43b5a2b476 100644 --- a/appmenu.h +++ b/appmenu.h @@ -21,6 +21,8 @@ along with this program. If not, see . *********************************************************************/ #ifndef KWIN_APPLICATIONMENU_H #define KWIN_APPLICATIONMENU_H +// KWin +#include // Qt #include // xcb @@ -41,9 +43,6 @@ public: bool hasMenu(xcb_window_t window); void showApplicationMenu(const QPoint &pos, const xcb_window_t window); - static ApplicationMenu *self(); - static ApplicationMenu *create(QObject *parent); - private Q_SLOTS: void slotShowRequest(qulonglong wid); void slotMenuAvailable(qulonglong wid); @@ -51,18 +50,11 @@ private Q_SLOTS: void slotClearMenus(); private: - ApplicationMenu(QObject *parent); QList m_windowsMenu; - static ApplicationMenu *s_self; + KWIN_SINGLETON(ApplicationMenu) }; -inline -ApplicationMenu *ApplicationMenu::self() -{ - return s_self; -} - } #endif // KWIN_APPLICATIONMENU_H diff --git a/composite.cpp b/composite.cpp index 94c7eda32e..078cf6215c 100644 --- a/composite.cpp +++ b/composite.cpp @@ -60,15 +60,9 @@ Q_DECLARE_METATYPE(KWin::Compositor::SuspendReason) namespace KWin { -Compositor *Compositor::s_compositor = NULL; extern int currentRefreshRate(); -Compositor *Compositor::createCompositor(QObject *parent) -{ - Q_ASSERT(!s_compositor); - s_compositor = new Compositor(parent); - return s_compositor; -} +KWIN_SINGLETON_FACTORY_VARIABLE(Compositor, s_compositor) Compositor::Compositor(QObject* workspace) : QObject(workspace) diff --git a/composite.h b/composite.h index ebaf7e7eb9..2479eaa493 100644 --- a/composite.h +++ b/composite.h @@ -21,7 +21,9 @@ along with this program. If not, see . #ifndef KWIN_COMPOSITE_H #define KWIN_COMPOSITE_H - +// KWin +#include +// Qt #include #include #include @@ -114,32 +116,6 @@ public: return m_scene; } - /** - * @brief Factory Method to create the Compositor singleton. - * - * This method is mainly used by Workspace to create the Compositor Singleton as a child - * of the Workspace. - * - * To actually access the Compositor instance use @link self. - * - * @param parent The parent object - * @return :Compositor* Created Compositor if not already created - * @warning This method is not Thread safe. - * @see self - **/ - static Compositor *createCompositor(QObject *parent); - /** - * @brief Singleton getter for the Compositor object. - * - * Ensure that the Compositor has been created through createCompositor prior to access - * this method. - * - * @return :Compositor* The Compositor instance - * @see createCompositor - **/ - static Compositor *self() { - return s_compositor; - } /** * @brief Checks whether the Compositor has already been created by the Workspace. * @@ -283,7 +259,6 @@ private Q_SLOTS: void releaseCompositorSelection(); private: - Compositor(QObject *workspace); void setCompositeTimer(); bool windowRepaintsPending() const; @@ -315,7 +290,7 @@ private: int m_timeSinceLastVBlank, m_nextFrameDelay; Scene *m_scene; - static Compositor *s_compositor; + KWIN_SINGLETON_VARIABLE(Compositor, s_compositor) }; } diff --git a/cursor.cpp b/cursor.cpp index 43217dfabb..35a0365c81 100644 --- a/cursor.cpp +++ b/cursor.cpp @@ -31,7 +31,7 @@ along with this program. If not, see . namespace KWin { -Cursor *Cursor::s_self = NULL; +KWIN_SINGLETON_FACTORY_FACTORED(Cursor, X11Cursor) Cursor::Cursor(QObject *parent) : QObject(parent) @@ -44,13 +44,6 @@ Cursor::~Cursor() s_self = NULL; } -Cursor *Cursor::create(QObject *parent) -{ - Q_ASSERT(!s_self); - s_self = new X11Cursor(parent); - return s_self; -} - QPoint Cursor::pos() { s_self->doGetPos(); diff --git a/cursor.h b/cursor.h index 9acf0b2fd1..6fb0ad9698 100644 --- a/cursor.h +++ b/cursor.h @@ -19,6 +19,8 @@ along with this program. If not, see . *********************************************************************/ #ifndef KWIN_CURSOR_H #define KWIN_CURSOR_H +// kwin +#include // Qt #include #include @@ -70,14 +72,8 @@ public: **/ static void setPos(const QPoint &pos); static void setPos(int x, int y); - static Cursor *self(); static xcb_cursor_t x11Cursor(Qt::CursorShape shape); - /** - * @internal - * Factory method - **/ - static Cursor *create(QObject *parent); Q_SIGNALS: void posChanged(QPoint pos); void mouseChanged(const QPoint& pos, const QPoint& oldpos, @@ -85,7 +81,6 @@ Q_SIGNALS: Qt::KeyboardModifiers modifiers, Qt::KeyboardModifiers oldmodifiers); protected: - Cursor(QObject *parent); /** * Called from @link x11Cursor to actually retrieve the X11 cursor. Base implementation returns * a null cursor, an implementing subclass should implement this method if it can provide X11 @@ -128,7 +123,7 @@ private: QPoint m_pos; int m_mousePollingCounter; - static Cursor *s_self; + KWIN_SINGLETON(Cursor) }; class X11Cursor : public Cursor @@ -163,11 +158,6 @@ private: friend class Cursor; }; -inline Cursor *Cursor::self() -{ - return s_self; -} - inline const QPoint &Cursor::currentPos() const { return m_pos; diff --git a/focuschain.cpp b/focuschain.cpp index 1687ec8af1..b14e57139d 100644 --- a/focuschain.cpp +++ b/focuschain.cpp @@ -23,7 +23,7 @@ along with this program. If not, see . namespace KWin { -FocusChain *FocusChain::s_manager = NULL; +KWIN_SINGLETON_FACTORY_VARIABLE(FocusChain, s_manager) FocusChain::FocusChain(QObject *parent) : QObject(parent) @@ -33,13 +33,6 @@ FocusChain::FocusChain(QObject *parent) { } -FocusChain *FocusChain::create(QObject *parent) -{ - Q_ASSERT(!s_manager); - s_manager = new FocusChain(parent); - return s_manager; -} - FocusChain::~FocusChain() { s_manager = NULL; diff --git a/focuschain.h b/focuschain.h index 881dafaddb..2e64b2f0b8 100644 --- a/focuschain.h +++ b/focuschain.h @@ -19,6 +19,9 @@ along with this program. If not, see . *********************************************************************/ #ifndef KWIN_FOCUS_CHAIN_H #define KWIN_FOCUS_CHAIN_H +// KWin +#include +// Qt #include #include @@ -161,22 +164,6 @@ public: **/ Client *firstMostRecentlyUsed() const; - /** - * @brief Singleton getter for this FocusChain. - * If called before the Singleton is created the method returns a @c null pointer. Be sure - * to call @link create first. - * - * @return :FocusChain* Singleton pointer - * @see create - **/ - static FocusChain *self(); - /** - * @brief Creates this FocusChain and sets the singleton pointer. - * - * @param parent The parent for this FocusChain. - * @return :FocusChain* The created FocusChain - **/ - static FocusChain *create(QObject *parent); public slots: /** * @brief Resizes the per virtual desktop focus chains from @p previousSize to @p newSize. @@ -201,7 +188,6 @@ public slots: bool isUsableFocusCandidate(Client *c, Client *prev) const; private: - explicit FocusChain(QObject *parent = 0); /** * @brief Makes @p client the first Client in the given focus @p chain. * @@ -234,15 +220,9 @@ private: Client *m_activeClient; uint m_currentDesktop; - static FocusChain *s_manager; + KWIN_SINGLETON_VARIABLE(FocusChain, s_manager) }; -inline -FocusChain *FocusChain::self() -{ - return s_manager; -} - inline bool FocusChain::contains(Client *client) const { diff --git a/libkwineffects/kwinglobals.h b/libkwineffects/kwinglobals.h index 505ad5c549..67922804ac 100644 --- a/libkwineffects/kwinglobals.h +++ b/libkwineffects/kwinglobals.h @@ -198,4 +198,27 @@ private: } // namespace +#define KWIN_SINGLETON_VARIABLE(ClassName, variableName) \ +public: \ + static ClassName *create(QObject *parent = 0);\ + static ClassName *self() { return variableName; }\ +protected: \ + explicit ClassName(QObject *parent = 0); \ +private: \ + static ClassName *variableName; + +#define KWIN_SINGLETON(ClassName) KWIN_SINGLETON_VARIABLE(ClassName, s_self) + +#define KWIN_SINGLETON_FACTORY_VARIABLE_FACTORED(ClassName, FactoredClassName, variableName) \ +ClassName *ClassName::variableName = 0; \ +ClassName *ClassName::create(QObject *parent) \ +{ \ + Q_ASSERT(!variableName); \ + variableName = new FactoredClassName(parent); \ + return variableName; \ +} +#define KWIN_SINGLETON_FACTORY_VARIABLE(ClassName, variableName) KWIN_SINGLETON_FACTORY_VARIABLE_FACTORED(ClassName, ClassName, variableName) +#define KWIN_SINGLETON_FACTORY_FACTORED(ClassName, FactoredClassName) KWIN_SINGLETON_FACTORY_VARIABLE_FACTORED(ClassName, FactoredClassName, s_self) +#define KWIN_SINGLETON_FACTORY(ClassName) KWIN_SINGLETON_FACTORY_VARIABLE(ClassName, s_self) + #endif diff --git a/placement.cpp b/placement.cpp index a97d9c6e82..2e70f9245b 100644 --- a/placement.cpp +++ b/placement.cpp @@ -39,18 +39,11 @@ namespace KWin #ifndef KCMRULES -Placement *Placement::s_self = NULL; +KWIN_SINGLETON_FACTORY(Placement) -Placement *Placement::create(Workspace *ws) +Placement::Placement(QObject*) { - Q_ASSERT(!s_self); - s_self = new Placement(ws); - return s_self; -} - -Placement::Placement(Workspace* w) -{ - m_WorkspacePtr = w; + m_WorkspacePtr = Workspace::self(); reinitCascading(0); } diff --git a/placement.h b/placement.h index cc9c33e9e9..10eb652602 100644 --- a/placement.h +++ b/placement.h @@ -22,11 +22,15 @@ along with this program. If not, see . #ifndef KWIN_PLACEMENT_H #define KWIN_PLACEMENT_H - +// KWin +#include +// Qt #include #include #include +class QObject; + namespace KWin { @@ -83,19 +87,7 @@ public: static Policy policyFromString(const QString& policy, bool no_special); static const char* policyToString(Policy policy); - /** - * Singleton getter for this Placement object once the Placement has been created. - * @see create - **/ - static Placement *self(); - /** - * Creates the Placement singleton. - **/ - static Placement *create(Workspace *ws); - private: - explicit Placement(Workspace* w); - void place(Client* c, QRect& area, Policy policy, Policy nextPlacement = Unknown); void placeUnderMouse(Client* c, QRect& area, Policy next = Unknown); void placeOnMainWindow(Client* c, QRect& area, Policy next = Unknown); @@ -111,15 +103,9 @@ private: QList cci; Workspace* m_WorkspacePtr; - static Placement *s_self; + KWIN_SINGLETON(Placement) }; -inline -Placement *Placement::self() -{ - return s_self; -} - } // namespace #endif diff --git a/screenedge.cpp b/screenedge.cpp index d0071a434d..9ac2af6069 100644 --- a/screenedge.cpp +++ b/screenedge.cpp @@ -534,14 +534,7 @@ void WindowBasedEdge::doUpdateBlocking() /********************************************************** * ScreenEdges *********************************************************/ -ScreenEdges *ScreenEdges::s_self = NULL; - -ScreenEdges *ScreenEdges::create(QObject *parent) -{ - Q_ASSERT(!s_self); - s_self = new ScreenEdges(parent); - return s_self; -} +KWIN_SINGLETON_FACTORY(ScreenEdges) ScreenEdges::ScreenEdges(QObject *parent) : QObject(parent) diff --git a/screenedge.h b/screenedge.h index 38e716389e..a49ee13fc4 100644 --- a/screenedge.h +++ b/screenedge.h @@ -198,7 +198,6 @@ class ScreenEdges : public QObject Q_PROPERTY(int actionBottomLeft READ actionBottomLeft) Q_PROPERTY(int actionLeft READ actionLeft) public: - explicit ScreenEdges(QObject *parent = 0); virtual ~ScreenEdges(); /** * @internal @@ -283,19 +282,6 @@ public: ElectricBorderAction actionBottomLeft() const; ElectricBorderAction actionLeft() const; - /** - * Singleton getter for this manager. - * - * Does not create a new instance. If the manager has not been created yet a @c null pointer - * is returned. - * @see create - **/ - static ScreenEdges *self(); - /** - * Factory method to create the ScreenEdges. - * @see self - **/ - static ScreenEdges *create(QObject *parent = NULL); public Q_SLOTS: void reconfigure(); /** @@ -348,7 +334,7 @@ private: ElectricBorderAction m_actionBottomLeft; ElectricBorderAction m_actionLeft; - static ScreenEdges *s_self; + KWIN_SINGLETON(ScreenEdges) }; /********************************************************** @@ -534,11 +520,5 @@ ACTION(actionLeft) #undef ACTION -inline ScreenEdges *ScreenEdges::self() -{ - Q_ASSERT(s_self); - return s_self; -} - } #endif // KWIN_SCREENEDGE_H diff --git a/virtualdesktops.cpp b/virtualdesktops.cpp index e53e298387..038e77824b 100644 --- a/virtualdesktops.cpp +++ b/virtualdesktops.cpp @@ -86,7 +86,7 @@ QPoint VirtualDesktopGrid::gridCoords(uint id) const return QPoint(-1, -1); } -VirtualDesktopManager *VirtualDesktopManager::s_manager = NULL; +KWIN_SINGLETON_FACTORY_VARIABLE(VirtualDesktopManager, s_manager) VirtualDesktopManager::VirtualDesktopManager(QObject *parent) : QObject(parent) @@ -102,13 +102,6 @@ VirtualDesktopManager::~VirtualDesktopManager() s_manager = NULL; } -VirtualDesktopManager *VirtualDesktopManager::create(QObject *parent) -{ - Q_ASSERT(!s_manager); - s_manager = new VirtualDesktopManager(parent); - return s_manager; -} - QString VirtualDesktopManager::name(uint desktop) const { if (!m_rootInfo) { diff --git a/virtualdesktops.h b/virtualdesktops.h index f80ddebf72..14f13e2543 100644 --- a/virtualdesktops.h +++ b/virtualdesktops.h @@ -19,7 +19,8 @@ along with this program. If not, see . *********************************************************************/ #ifndef KWIN_VIRTUAL_DESKTOPS_H #define KWIN_VIRTUAL_DESKTOPS_H - +// KWin +#include // Qt includes #include #include @@ -179,19 +180,6 @@ public: void initShortcuts(KActionCollection *keys); - /** - * Singleton getter for this manager. - * - * Does not create a new instance. If the manager has not been created yet a @c null pointer - * is returned. - * @see create - **/ - static VirtualDesktopManager *self(); - /** - * Factory method to create the VirtualDesktopManager. - * @see self - **/ - static VirtualDesktopManager *create(QObject *parent = NULL); /** * @returns The maximum number of desktops that KWin supports. */ @@ -312,7 +300,6 @@ private slots: void slotDown(); private: - explicit VirtualDesktopManager(QObject *parent = 0); /** * This method is called when the number of desktops is updated in a way that desktops * are removed. At the time when this method is invoked the count property is already @@ -374,7 +361,7 @@ private: NETRootInfo *m_rootInfo; KSharedConfig::Ptr m_config; - static VirtualDesktopManager *s_manager; + KWIN_SINGLETON_VARIABLE(VirtualDesktopManager, s_manager) }; /** @@ -530,13 +517,6 @@ uint VirtualDesktopGrid::at(QPoint coords) const return m_grid[index]; } -inline -VirtualDesktopManager *VirtualDesktopManager::self() -{ - Q_ASSERT(s_manager); - return s_manager; -} - inline uint VirtualDesktopManager::maximum() { diff --git a/workspace.cpp b/workspace.cpp index a8c9951273..d4fbef8fb8 100644 --- a/workspace.cpp +++ b/workspace.cpp @@ -204,7 +204,7 @@ Workspace::Workspace(bool restore) TabBox::TabBox::create(this); #endif - m_compositor = Compositor::createCompositor(this); + m_compositor = Compositor::create(this); connect(this, SIGNAL(currentDesktopChanged(int,KWin::Client*)), m_compositor, SLOT(addRepaintFull())); connect(m_compositor, SIGNAL(compositingToggled(bool)), SLOT(slotCompositingToggled()));