• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kglobal.h

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1999 Sirtaj Singh Kanq <taj@kde.org>
00003    Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 #ifndef _KGLOBAL_H
00020 #define _KGLOBAL_H
00021 
00022 #include <kdecore_export.h>
00023 #include <QtCore/QAtomicPointer>
00024 #include <sys/types.h>
00025 
00026 
00027 //
00028 // WARNING!!
00029 // This code uses undocumented Qt API
00030 // Do not copy it to your application! Use only the functions that are here!
00031 // Otherwise, it could break when a new version of Qt ships.
00032 //
00033 
00034 #if QT_VERSION < 0x040400
00035 # define Q_BASIC_ATOMIC_INITIALIZER     Q_ATOMIC_INIT
00036 # define testAndSetOrdered              testAndSet
00037 #endif
00038 
00039 class KComponentData;
00040 class KCharsets;
00041 class KConfig;
00042 class KLocale;
00043 class KStandardDirs;
00044 class KSharedConfig;
00045 template <typename T>
00046 class KSharedPtr;
00047 typedef KSharedPtr<KSharedConfig> KSharedConfigPtr;
00048 
00050 
00054 typedef void (*KdeCleanUpFunction)();
00055 
00062 class KCleanUpGlobalStatic
00063 {
00064     public:
00065         KdeCleanUpFunction func;
00066 
00067         inline ~KCleanUpGlobalStatic() { func(); }
00068 };
00069 
00070 #ifdef Q_CC_MSVC
00071 
00077 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME) _k_##NAME##__LINE__
00078 #else
00079 
00084 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME)
00085 #endif
00086 
00088 
00226 #define K_GLOBAL_STATIC(TYPE, NAME) K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
00227 
00260 #define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)                            \
00261 static QBasicAtomicPointer<TYPE > _k_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0); \
00262 static bool _k_static_##NAME##_destroyed;                                      \
00263 static struct K_GLOBAL_STATIC_STRUCT_NAME(NAME)                                \
00264 {                                                                              \
00265     inline bool isDestroyed() const                                            \
00266     {                                                                          \
00267         return _k_static_##NAME##_destroyed;                                   \
00268     }                                                                          \
00269     inline bool exists() const                                                 \
00270     {                                                                          \
00271         return _k_static_##NAME != 0;                                          \
00272     }                                                                          \
00273     inline operator TYPE*()                                                    \
00274     {                                                                          \
00275         return operator->();                                                   \
00276     }                                                                          \
00277     inline TYPE *operator->()                                                  \
00278     {                                                                          \
00279         if (!_k_static_##NAME) {                                               \
00280             if (isDestroyed()) {                                               \
00281                 qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \
00282                        "Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__);  \
00283             }                                                                  \
00284             TYPE *x = new TYPE ARGS;                                           \
00285             if (!_k_static_##NAME.testAndSetOrdered(0, x)                      \
00286                 && _k_static_##NAME != x ) {                                   \
00287                 delete x;                                                      \
00288             } else {                                                           \
00289                 static KCleanUpGlobalStatic cleanUpObject = { destroy };       \
00290             }                                                                  \
00291         }                                                                      \
00292         return _k_static_##NAME;                                               \
00293     }                                                                          \
00294     inline TYPE &operator*()                                                   \
00295     {                                                                          \
00296         return *operator->();                                                  \
00297     }                                                                          \
00298     static void destroy()                                                      \
00299     {                                                                          \
00300         _k_static_##NAME##_destroyed = true;                                   \
00301         TYPE *x = _k_static_##NAME;                                            \
00302         _k_static_##NAME = 0;                                                  \
00303         delete x;                                                              \
00304     }                                                                          \
00305 } NAME;
00306 
00316 namespace KGlobal
00317 {
00318 
00325     KDECORE_EXPORT const KComponentData &mainComponent(); //krazy:exclude=constref (don't mess up ref-counting)
00326 
00331     KDECORE_EXPORT bool hasMainComponent();
00332 
00337     KDECORE_EXPORT KStandardDirs *dirs();
00338 
00343     KDECORE_EXPORT KSharedConfigPtr config();
00344 
00349     KDECORE_EXPORT KLocale *locale();
00354     KDECORE_EXPORT bool hasLocale();
00355 
00360     KDECORE_EXPORT KCharsets *charsets();
00361 
00366     KDECORE_EXPORT mode_t umask();
00367 
00385     KDECORE_EXPORT const QString& staticQString(const char *str); //krazy:exclude=constref (doesn't make sense otherwise)
00386 
00404     KDECORE_EXPORT const QString& staticQString(const QString &str); //krazy:exclude=constref (doesn't make sense otherwise)
00405 
00425     KDECORE_EXPORT void ref();
00426 
00431     KDECORE_EXPORT void deref();
00432 
00438     KDECORE_EXPORT void setAllowQuit(bool allowQuit);
00439 
00446     KDECORE_EXPORT KComponentData activeComponent();
00447 
00454     KDECORE_EXPORT void setActiveComponent(const KComponentData &d);
00455 
00464     KDECORE_EXPORT QString caption();
00465 
00469     enum CopyCatalogs { DoCopyCatalogs, DontCopyCatalogs};
00470 
00472     KDECORE_EXPORT void setLocale(KLocale *, CopyCatalogs copy = DoCopyCatalogs);
00473 }
00474 
00475 #ifdef KDE_SUPPORT
00476 
00481 #define KMIN(a,b)   qMin(a,b)
00482 
00487 #define KMAX(a,b)   qMax(a,b)
00488 
00493 #define KABS(a) qAbs(a)
00494 
00500 #define KCLAMP(x,low,high) qBound(low,x,high)
00501 
00502 #define kMin qMin
00503 #define kMax qMax
00504 #define kAbs qAbs
00505 
00513 template<class T>
00514 inline KDE_DEPRECATED T kClamp( const T& x, const T& low, const T& high )
00515 {
00516     if ( x < low )       return low;
00517     else if ( high < x ) return high;
00518                          return x;
00519 }
00520 
00521 #endif
00522 
00523 #endif // _KGLOBAL_H
00524 

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal