/usr/include/kglobal.h is in kdelibs5-dev 4:4.8.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | /* This file is part of the KDE libraries
Copyright (C) 1999 Sirtaj Singh Kanq <taj@kde.org>
Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef _KGLOBAL_H
#define _KGLOBAL_H
#include <kdecore_export.h>
#include <QtCore/QAtomicPointer>
#include <sys/types.h>
#include <QtCore/QObject>
//
// WARNING!!
// This code uses undocumented Qt API
// Do not copy it to your application! Use only the functions that are here!
// Otherwise, it could break when a new version of Qt ships.
//
class KComponentData;
class KCharsets;
class KConfig;
class KLocale;
class KStandardDirs;
class KSharedConfig;
template <typename T>
class KSharedPtr;
typedef KSharedPtr<KSharedConfig> KSharedConfigPtr;
/// @cond InternalDocs
/**
* @internal
*/
typedef void (*KdeCleanUpFunction)();
/**
* @internal
*
* Helper class for K_GLOBAL_STATIC to clean up the object on library unload or application
* shutdown.
*/
class KCleanUpGlobalStatic
{
public:
KdeCleanUpFunction func;
inline ~KCleanUpGlobalStatic() { func(); }
};
#ifdef Q_CC_MSVC
/**
* @internal
*
* MSVC seems to give anonymous structs the same name which fails at link time. So instead we name
* the struct and hope that by adding the line number to the name it's unique enough to never clash.
*/
# define K_GLOBAL_STATIC_STRUCT_NAME(NAME) _k_##NAME##__LINE__
#else
/**
* @internal
*
* Make the struct of the K_GLOBAL_STATIC anonymous.
*/
# define K_GLOBAL_STATIC_STRUCT_NAME(NAME)
#endif
/// @endcond
/**
* 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.
*
* @param TYPE The type of the global static object. Do not add a *.
* @param NAME The 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:
* @code
* 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);
* }
* @endcode
*
* A common case for the need of deletion on lib unload/app shutdown are Singleton classes. Here's
* an example how to do it:
* @code
* class MySingletonPrivate;
* class EXPORT_MACRO MySingleton
* {
* friend class MySingletonPrivate;
* public:
* static MySingleton *self();
* QString someFunction();
*
* private:
* MySingleton();
* ~MySingleton();
* };
* @endcode
* in the .cpp file:
* @code
* // 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;
* }
* @endcode
*
* Instead of the above you can use also the following pattern (ignore the name of the namespace):
* @code
* namespace MySingleton
* {
* EXPORT_MACRO QString someFunction();
* }
* @endcode
* in the .cpp file:
* @code
* class MySingletonPrivate
* {
* public:
* QString foo;
* };
*
* K_GLOBAL_STATIC(MySingletonPrivate, mySingletonPrivate)
*
* QString MySingleton::someFunction()
* {
* return mySingletonPrivate->foo;
* }
* @endcode
*
* Now code that wants to call someFunction() doesn't have to do
* @code
* MySingleton::self()->someFunction();
* @endcode
* anymore but instead:
* @code
* MySingleton::someFunction();
* @endcode
*
* @ingroup KDEMacros
*/
#define K_GLOBAL_STATIC(TYPE, NAME) K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
/**
* @overload
* This is the same as K_GLOBAL_STATIC, but can take arguments that are passed
* to the object's constructor
*
* @param TYPE The type of the global static object. Do not add a *.
* @param NAME The name of the function to get a pointer to the global static object.
* @param ARGS the list of arguments, between brackets
*
* Example:
* @code
* 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;
* ...
* }
* @endcode
*
* @ingroup KDEMacros
*/
#define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
static QBasicAtomicPointer<TYPE > _k_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0); \
static bool _k_static_##NAME##_destroyed; \
static struct K_GLOBAL_STATIC_STRUCT_NAME(NAME) \
{ \
inline bool isDestroyed() const \
{ \
return _k_static_##NAME##_destroyed; \
} \
inline bool exists() const \
{ \
return _k_static_##NAME != 0; \
} \
inline operator TYPE*() \
{ \
return operator->(); \
} \
inline TYPE *operator->() \
{ \
if (!_k_static_##NAME) { \
if (isDestroyed()) { \
qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \
"Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__); \
} \
TYPE *x = new TYPE ARGS; \
if (!_k_static_##NAME.testAndSetOrdered(0, x) \
&& _k_static_##NAME != x ) { \
delete x; \
} else { \
static KCleanUpGlobalStatic cleanUpObject = { destroy }; \
} \
} \
return _k_static_##NAME; \
} \
inline TYPE &operator*() \
{ \
return *operator->(); \
} \
static void destroy() \
{ \
_k_static_##NAME##_destroyed = true; \
TYPE *x = _k_static_##NAME; \
_k_static_##NAME = 0; \
delete x; \
} \
} NAME;
/**
* This class is useful in libraries where you want to make sure that
* anyone that uses your library will get the correct catalog loaded.
* Just declare a static KCatalogLoader in the global namespace of one of
* your cpp files and that will load your catalog once
* the global klocale is created
*
* @param catalogName The name of your catalog
*
* @since 4.6.2
*
* Example:
* @code
* static const KCatalogLoader loader("libkdepim");
* @endcode
*/
class KDECORE_EXPORT KCatalogLoader
{
public:
KCatalogLoader(const QString &catalogName);
};
/**
* Access to the KDE global objects.
* KGlobal provides you with pointers of many central
* objects that exist only once in the process. It is also
* responsible for managing instances of KStaticDeleterBase.
*
* @see KStaticDeleterBase
* @author Sirtaj Singh Kang (taj@kde.org)
*/
namespace KGlobal
{
/**
* Returns the global component data. There is always at least
* one instance of a component in one application (in most
* cases the application itself).
* @return the global component data
*/
KDECORE_EXPORT const KComponentData &mainComponent(); //krazy:exclude=constref (don't mess up ref-counting)
/**
* @internal
* Returns whether a main KComponentData is available.
*/
KDECORE_EXPORT bool hasMainComponent();
/**
* Returns the application standard dirs object.
* @return the global standard dir object
*/
KDECORE_EXPORT KStandardDirs *dirs();
/**
* Returns the general config object.
* @return the global configuration object.
*/
KDECORE_EXPORT KSharedConfigPtr config();
/**
* Inserts the catalog in the main locale object if it exists.
* Otherwise the catalog name is stored and added once the main locale gets created
*
* @since 4.6
*/
KDECORE_EXPORT void insertCatalog(const QString& catalog);
/**
* Returns the global locale object.
* @return the global locale object
*
* Note: in multi-threaded programs, you should call KGlobal::locale()
* in the main thread (e.g. in main(), after creating the QCoreApplication
* and setting the main component), to ensure that the initialization is
* done in the main thread. However KApplication takes care of this, so this
* is only needed when not using KApplication.
*/
KDECORE_EXPORT KLocale *locale();
/**
* @internal
* Returns whether KGlobal has a valid KLocale object
*/
KDECORE_EXPORT bool hasLocale();
/**
* The global charset manager.
* @return the global charset manager
*/
KDECORE_EXPORT KCharsets *charsets();
/**
* Returns the umask of the process.
* @return the umask of the process
*/
KDECORE_EXPORT mode_t umask();
/**
* Creates a static QString.
*
* To be used inside functions(!) like:
* @code
* static const QString &myString = KGlobal::staticQString("myText");
* @endcode
*
* @attention Do @b NOT use code such as:
* @code
* static QString myString = KGlobal::staticQString("myText");
* @endcode
* This creates a static object (instead of a static reference)
* and as you know static objects are EVIL.
* @param str the string to create
* @return the static string
*/
KDECORE_EXPORT const QString& staticQString(const char *str); //krazy:exclude=constref (doesn't make sense otherwise)
/**
* Creates a static QString.
*
* To be used inside functions(!) like:
* @code
* static const QString &myString = KGlobal::staticQString(i18n("My Text"));
* @endcode
*
* @attention Do @b NOT use code such as:
* @code
* static QString myString = KGlobal::staticQString(i18n("myText"));
* @endcode
* This creates a static object (instead of a static reference)
* and as you know static objects are EVIL.
* @param str the string to create
* @return the static string
*/
KDECORE_EXPORT const QString& staticQString(const QString &str); //krazy:exclude=constref (doesn't make sense otherwise)
/**
* Tells KGlobal about one more operations that should be finished
* before the application exits. The standard behavior is to exit on the
* "last window closed" event, but some events should outlive the last window closed
* (e.g. a file copy for a file manager, or 'compacting folders on exit' for a mail client),
* or simply any application with a system tray icon.
*
* We have some use cases that we want to take care of (the format is "action refcount"):
* - open window -> setAllowQuit(true) 1 ; close window 0 => EXIT
* - job start 1; job end 0 [don't exit yet]; open window -> setAllowQuit(true) 1 ; close window 0 => EXIT
* - job start 1; open window -> setAllowQuit(true) 2; close window 1; job end 0 => EXIT
* - job start 1; open window -> setAllowQuit(true) 2; job end 1; close window 0 => EXIT
* - open dialog 0; close dialog 0; => DO NOT EXIT
* - job start 1; job end 0; create two main objects 2; delete both main objects 0 => EXIT
* - open window -> setAllowQuit(true) 1; add systray icon 2; close window 1 => DO NOT EXIT
* - open window -> setAllowQuit(true) 1; add systray icon 2; remove systray icon 1; close window 0 => EXIT
* - unit test which opens and closes many windows: should call ref() to avoid subevent-loops quitting too early.
*
* Note that for this to happen you must call qApp->setQuitOnLastWindowClosed(false),
* in main() for instance.
*/
KDECORE_EXPORT void ref();
/**
* Tells KGlobal that one operation such as those described in ref() just finished.
* This call makes the QApplication quit if the counter is back to 0.
*/
KDECORE_EXPORT void deref();
/**
* If refcounting reaches 0 (or less), and @p allowQuit is true, the instance of the application
* will automatically be exited. Otherwise, the application will not exit automatically.
*
* This is used by KMainWindow to allow quitting after the first mainwindow is created,
* and is used by special applications like kfmclient, to allow quitting even though
* no mainwindow was created.
*
* However, don't try to call setAllowQuit(false) in applications, it doesn't make sense.
* If you find that the application quits too early when closing a window, then consider
* _what_ is making your application still alive to the user (like a systray icon or a D-Bus object)
* and use KGlobal::ref() + KGlobal::deref() in that object.
*
* @since 4.1.1
*/
KDECORE_EXPORT void setAllowQuit(bool allowQuit);
/**
* The component currently active (useful in a multi-component
* application, such as a KParts application).
* Don't use this - it's mainly for KAboutDialog and KBugReport.
* @internal
*/
KDECORE_EXPORT KComponentData activeComponent();
/**
* Set the active component for use by KAboutDialog and KBugReport.
* To be used only by a multi-component (KParts) application.
*
* @see activeComponent()
*/
KDECORE_EXPORT void setActiveComponent(const KComponentData &d);
/**
* Returns a text for the window caption.
*
* This may be set by
* "-caption", otherwise it will be equivalent to the name of the
* executable.
* @return the text for the window caption
*/
KDECORE_EXPORT QString caption();
/// @internal
KDECORE_EXPORT QObject* findDirectChild_helper(const QObject* parent, const QMetaObject& mo);
/**
* Returns the child of the given object that can be cast into type T, or 0 if there is no such object.
* Unlike QObject::findChild, the search is NOT performed recursively.
* @since 4.4
*/
template<typename T>
inline T findDirectChild(const QObject* object) {
return static_cast<T>(findDirectChild_helper(object, (static_cast<T>(0))->staticMetaObject));
}
/**
* For setLocale
*/
enum CopyCatalogs { DoCopyCatalogs, DontCopyCatalogs};
///@internal
KDECORE_EXPORT void setLocale(KLocale *, CopyCatalogs copy = DoCopyCatalogs);
}
#ifdef KDE_SUPPORT
/**
* @relates KGlobal
* A typesafe function to find the smaller of the two arguments.
* @deprecated, used qMin instead
*/
#define KMIN(a,b) qMin(a,b)
/**
* @relates KGlobal
* A typesafe function to find the larger of the two arguments.
* @deprecated, used qMax instead
*/
#define KMAX(a,b) qMax(a,b)
/**
* \relates KGlobal
* A typesafe function to determine the absolute value of the argument.
* @deprecated, used qAbs instead
*/
#define KABS(a) qAbs(a)
/**
* @relates KGlobal
* A typesafe function that returns x if it's between low and high values.
* low if x is smaller than low and high if x is bigger than high.
* @deprecated, used qBound instead. Warning, the argument order differs.
*/
#define KCLAMP(x,low,high) qBound(low,x,high)
#define kMin qMin
#define kMax qMax
#define kAbs qAbs
/**
* @relates KGlobal
* A typesafe function that returns x if it's between low and high values.
* low if x is smaller than low and high if x is bigger than high.
* @deprecated, used qBound instead. Warning, the argument order differs.
*/
template<class T>
inline KDE_DEPRECATED T kClamp( const T& x, const T& low, const T& high )
{
if ( x < low ) return low;
else if ( high < x ) return high;
return x;
}
#endif
#endif // _KGLOBAL_H
|