KDECore
kglobal.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00029
00030
00031
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();
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);
00386
00404 KDECORE_EXPORT const QString& staticQString(const QString &str);
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