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
icc-effect-5.14.5
Martin Gräßlin 2013-04-05 09:41:25 +02:00
parent 76c6f75d80
commit 0fb27fd12e
18 changed files with 61 additions and 216 deletions

View File

@ -34,14 +34,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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)

View File

@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_ACTIVITIES_H
#define KWIN_ACTIVITIES_H
#include <kwinglobals.h>
#include <QObject>
#include <QStringList>
@ -54,8 +56,6 @@ public:
const QString &current() 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
{

View File

@ -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)

View File

@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_APPLICATIONMENU_H
#define KWIN_APPLICATIONMENU_H
// KWin
#include <kwinglobals.h>
// Qt
#include <QObject>
// 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<xcb_window_t> m_windowsMenu;
static ApplicationMenu *s_self;
KWIN_SINGLETON(ApplicationMenu)
};
inline
ApplicationMenu *ApplicationMenu::self()
{
return s_self;
}
}
#endif // KWIN_APPLICATIONMENU_H

View File

@ -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)

View File

@ -21,7 +21,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_COMPOSITE_H
#define KWIN_COMPOSITE_H
// KWin
#include <kwinglobals.h>
// Qt
#include <QObject>
#include <QElapsedTimer>
#include <QTimer>
@ -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)
};
}

View File

@ -31,7 +31,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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();

View File

@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_CURSOR_H
#define KWIN_CURSOR_H
// kwin
#include <kwinglobals.h>
// Qt
#include <QHash>
#include <QObject>
@ -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;

View File

@ -23,7 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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;

View File

@ -19,6 +19,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_FOCUS_CHAIN_H
#define KWIN_FOCUS_CHAIN_H
// KWin
#include <kwinglobals.h>
// Qt
#include <QObject>
#include <QHash>
@ -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
{

View File

@ -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

View File

@ -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);
}

View File

@ -22,11 +22,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_PLACEMENT_H
#define KWIN_PLACEMENT_H
// KWin
#include <kwinglobals.h>
// Qt
#include <QPoint>
#include <QRect>
#include <QList>
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<DesktopCascadingInfo> cci;
Workspace* m_WorkspacePtr;
static Placement *s_self;
KWIN_SINGLETON(Placement)
};
inline
Placement *Placement::self()
{
return s_self;
}
} // namespace
#endif

View File

@ -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)

View File

@ -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

View File

@ -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) {

View File

@ -19,7 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef KWIN_VIRTUAL_DESKTOPS_H
#define KWIN_VIRTUAL_DESKTOPS_H
// KWin
#include <kwinglobals.h>
// Qt includes
#include <QObject>
#include <QPoint>
@ -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()
{

View File

@ -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()));