• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

Macros
KDE Macros

Macros

#define K_GLOBAL_STATIC(TYPE, NAME)   K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
 
#define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
 

Detailed Description

Macro Definition Documentation

◆ K_GLOBAL_STATIC

#define K_GLOBAL_STATIC (   TYPE,
  NAME 
)    K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())

This macro makes it easy to use non-POD types as global statics.

The object is created on first use and creation is threadsafe.

The object is destructed on library unload or application exit. Be careful with calling other objects in the destructor of the class as you have to be sure that they (or objects they depend on) are not already destructed.

Parameters
TYPEThe type of the global static object. Do not add a *.
NAMEThe name of the function to get a pointer to the global static object.

If you have code that might be called after the global object has been destroyed you can check for that using the isDestroyed() function.

If needed (If the destructor of the global object calls other functions that depend on other global statics (e.g. KConfig::sync) your destructor has to be called before those global statics are destroyed. A Qt post routine does that.) you can also install a post routine (qAddPostRoutine) to clean up the object using the destroy() method. If you registered a post routine and the object is destroyed because of a lib unload you have to call qRemovePostRoutine!

Example:

class A {
public:
~A();
...
};
K_GLOBAL_STATIC(A, globalA)
// The above creates a new globally static variable named 'globalA' which you
// can use as a pointer to an instance of A.
void doSomething()
{
// The first time you access globalA a new instance of A will be created automatically.
A *a = globalA;
...
}
void doSomethingElse()
{
if (globalA.isDestroyed()) {
return;
}
A *a = globalA;
...
}
void installPostRoutine()
{
// A post routine can be used to delete the object when QCoreApplication destructs,
// not adding such a post routine will delete the object normally at program unload
qAddPostRoutine(globalA.destroy);
}
A::~A()
{
// When you install a post routine you have to remove the post routine from the destructor of
// the class used as global static!
qRemovePostRoutine(globalA.destroy);
}
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:221

A common case for the need of deletion on lib unload/app shutdown are Singleton classes. Here's an example how to do it:

class MySingletonPrivate;
class EXPORT_MACRO MySingleton
{
friend class MySingletonPrivate;
public:
static MySingleton *self();
QString someFunction();
private:
MySingleton();
~MySingleton();
};
QString

in the .cpp file:

// This class will be instantiated and referenced as a singleton in this example
class MySingletonPrivate
{
public:
QString foo;
MySingleton instance;
};
K_GLOBAL_STATIC(MySingletonPrivate, mySingletonPrivate)
MySingleton *MySingleton::self()
{
// returns the singleton; automatically creates a new instance if that has not happened yet.
return &mySingletonPrivate->instance;
}
QString MySingleton::someFunction()
{
// Refencing the singleton directly is possible for your convenience
return mySingletonPrivate->foo;
}

Instead of the above you can use also the following pattern (ignore the name of the namespace):

namespace MySingleton
{
EXPORT_MACRO QString someFunction();
}

in the .cpp file:

class MySingletonPrivate
{
public:
QString foo;
};
K_GLOBAL_STATIC(MySingletonPrivate, mySingletonPrivate)
QString MySingleton::someFunction()
{
return mySingletonPrivate->foo;
}

Now code that wants to call someFunction() doesn't have to do

MySingleton::self()->someFunction();

anymore but instead:

MySingleton::someFunction();

Definition at line 221 of file kglobal.h.

◆ K_GLOBAL_STATIC_WITH_ARGS

#define K_GLOBAL_STATIC_WITH_ARGS (   TYPE,
  NAME,
  ARGS 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. This is the same as K_GLOBAL_STATIC, but can take arguments that are passed to the object's constructor.

Parameters
TYPEThe type of the global static object. Do not add a *.
NAMEThe name of the function to get a pointer to the global static object.
ARGSthe list of arguments, between brackets

Example:

class A
{
public:
A(const char *s, int i);
...
};
K_GLOBAL_STATIC_WITH_ARGS(A, globalA, ("foo", 0))
// The above creates a new globally static variable named 'globalA' which you
// can use as a pointer to an instance of A.
void doSomething()
{
// The first time you access globalA a new instance of A will be created automatically.
A *a = globalA;
...
}
K_GLOBAL_STATIC_WITH_ARGS
#define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: kglobal.h:255

Definition at line 255 of file kglobal.h.

This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal