• Skip to content
  • Skip to link menu
KDE 4.0 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 
00025 //
00026 // WARNING!!
00027 // This code uses undocumented Qt API
00028 // Do not copy it to your application! Use only the functions that are here!
00029 // Otherwise, it could break when a new version of Qt ships.
00030 //
00031 
00032 #if QT_VERSION < 0x040400
00033 # define Q_BASIC_ATOMIC_INITIALIZER     Q_ATOMIC_INIT
00034 # define testAndSetOrdered              testAndSet
00035 #endif
00036 
00037 class KComponentData;
00038 class KCharsets;
00039 class KConfig;
00040 class KLocale;
00041 class KStandardDirs;
00042 class KSharedConfig;
00043 template <typename T>
00044 class KSharedPtr;
00045 typedef KSharedPtr<KSharedConfig> KSharedConfigPtr;
00046 
00048 
00052 typedef void (*KdeCleanUpFunction)();
00053 
00060 class KCleanUpGlobalStatic
00061 {
00062     public:
00063         KdeCleanUpFunction func;
00064 
00065         inline ~KCleanUpGlobalStatic() { func(); }
00066 };
00067 
00068 #ifdef Q_CC_MSVC
00069 
00075 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME) _k_##NAME##__LINE__
00076 #else
00077 
00082 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME)
00083 #endif
00084 
00086 
00224 #define K_GLOBAL_STATIC(TYPE, NAME) K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
00225 
00258 #define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)                            \
00259 static QBasicAtomicPointer<TYPE > _k_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0); \
00260 static bool _k_static_##NAME##_destroyed;                                      \
00261 static struct K_GLOBAL_STATIC_STRUCT_NAME(NAME)                                \
00262 {                                                                              \
00263     bool isDestroyed()                                                         \
00264     {                                                                          \
00265         return _k_static_##NAME##_destroyed;                                   \
00266     }                                                                          \
00267     inline operator TYPE*()                                                    \
00268     {                                                                          \
00269         return operator->();                                                   \
00270     }                                                                          \
00271     inline TYPE *operator->()                                                  \
00272     {                                                                          \
00273         if (!_k_static_##NAME) {                                               \
00274             if (isDestroyed()) {                                               \
00275                 qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \
00276                        "Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__);  \
00277             }                                                                  \
00278             TYPE *x = new TYPE ARGS;                                           \
00279             if (!_k_static_##NAME.testAndSetOrdered(0, x)                      \
00280                 && _k_static_##NAME != x ) {                                   \
00281                 delete x;                                                      \
00282             } else {                                                           \
00283                 static KCleanUpGlobalStatic cleanUpObject = { destroy };       \
00284             }                                                                  \
00285         }                                                                      \
00286         return _k_static_##NAME;                                               \
00287     }                                                                          \
00288     inline TYPE &operator*()                                                   \
00289     {                                                                          \
00290         return *operator->();                                                  \
00291     }                                                                          \
00292     static void destroy()                                                      \
00293     {                                                                          \
00294         _k_static_##NAME##_destroyed = true;                                   \
00295         TYPE *x = _k_static_##NAME;                                            \
00296         _k_static_##NAME = 0;                                                  \
00297         delete x;                                                              \
00298     }                                                                          \
00299 } NAME;
00300 
00310 namespace KGlobal
00311 {
00312 
00319     KDECORE_EXPORT const KComponentData &mainComponent(); //krazy:exclude=constref (don't mess up ref-counting)
00320 
00325     KDECORE_EXPORT bool hasMainComponent();
00326 
00331     KDECORE_EXPORT KStandardDirs *dirs();
00332 
00337     KDECORE_EXPORT KSharedConfigPtr config();
00338 
00343     KDECORE_EXPORT KLocale *locale();
00348     KDECORE_EXPORT bool hasLocale();
00349 
00354     KDECORE_EXPORT KCharsets *charsets();
00355 
00373     KDECORE_EXPORT const QString& staticQString(const char *str); //krazy:exclude=constref (doesn't make sense otherwise)
00374 
00392     KDECORE_EXPORT const QString& staticQString(const QString &str); //krazy:exclude=constref (doesn't make sense otherwise)
00393 
00403     KDECORE_EXPORT void ref();
00404 
00409     KDECORE_EXPORT void deref();
00410 
00417     KDECORE_EXPORT KComponentData activeComponent();
00418 
00425     KDECORE_EXPORT void setActiveComponent(const KComponentData &d);
00426 
00435     KDECORE_EXPORT QString caption();
00436 
00440     enum CopyCatalogs { DoCopyCatalogs, DontCopyCatalogs};
00441 
00443     KDECORE_EXPORT void setLocale(KLocale *, CopyCatalogs copy = DoCopyCatalogs);
00444 }
00445 
00446 #ifdef KDE_SUPPORT
00447 
00452 #define KMIN(a,b)   qMin(a,b)
00453 
00458 #define KMAX(a,b)   qMax(a,b)
00459 
00464 #define KABS(a) qAbs(a)
00465 
00471 #define KCLAMP(x,low,high) qBound(low,x,high)
00472 
00473 #define kMin qMin
00474 #define kMax qMax
00475 #define kAbs qAbs
00476 
00484 template<class T>
00485 inline KDE_DEPRECATED T kClamp( const T& x, const T& low, const T& high )
00486 {
00487     if ( x < low )       return low;
00488     else if ( high < x ) return high;
00489                          return x;
00490 }
00491 
00492 #endif
00493 
00494 #endif // _KGLOBAL_H
00495 

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
  •   WTF
  • KJSEmbed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  •   core
  • Phonon
  •   Backend
  • 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