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 class KComponentData;
00035 class KCharsets;
00036 class KConfig;
00037 class KLocale;
00038 class KStandardDirs;
00039 class KSharedConfig;
00040 template <typename T>
00041 class KSharedPtr;
00042 typedef KSharedPtr<KSharedConfig> KSharedConfigPtr;
00043
00045
00049 typedef void (*KdeCleanUpFunction)();
00050
00057 class KCleanUpGlobalStatic
00058 {
00059 public:
00060 KdeCleanUpFunction func;
00061
00062 inline ~KCleanUpGlobalStatic() { func(); }
00063 };
00064
00065 #ifdef Q_CC_MSVC
00066
00072 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME) _k_##NAME##__LINE__
00073 #else
00074
00079 # define K_GLOBAL_STATIC_STRUCT_NAME(NAME)
00080 #endif
00081
00083
00221 #define K_GLOBAL_STATIC(TYPE, NAME) K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
00222
00255 #define K_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
00256 static QBasicAtomicPointer<TYPE > _k_static_##NAME = Q_BASIC_ATOMIC_INITIALIZER(0); \
00257 static bool _k_static_##NAME##_destroyed; \
00258 static struct K_GLOBAL_STATIC_STRUCT_NAME(NAME) \
00259 { \
00260 inline bool isDestroyed() const \
00261 { \
00262 return _k_static_##NAME##_destroyed; \
00263 } \
00264 inline bool exists() const \
00265 { \
00266 return _k_static_##NAME != 0; \
00267 } \
00268 inline operator TYPE*() \
00269 { \
00270 return operator->(); \
00271 } \
00272 inline TYPE *operator->() \
00273 { \
00274 if (!_k_static_##NAME) { \
00275 if (isDestroyed()) { \
00276 qFatal("Fatal Error: Accessed global static '%s *%s()' after destruction. " \
00277 "Defined at %s:%d", #TYPE, #NAME, __FILE__, __LINE__); \
00278 } \
00279 TYPE *x = new TYPE ARGS; \
00280 if (!_k_static_##NAME.testAndSetOrdered(0, x) \
00281 && _k_static_##NAME != x ) { \
00282 delete x; \
00283 } else { \
00284 static KCleanUpGlobalStatic cleanUpObject = { destroy }; \
00285 } \
00286 } \
00287 return _k_static_##NAME; \
00288 } \
00289 inline TYPE &operator*() \
00290 { \
00291 return *operator->(); \
00292 } \
00293 static void destroy() \
00294 { \
00295 _k_static_##NAME##_destroyed = true; \
00296 TYPE *x = _k_static_##NAME; \
00297 _k_static_##NAME = 0; \
00298 delete x; \
00299 } \
00300 } NAME;
00301
00311 namespace KGlobal
00312 {
00313
00320 KDECORE_EXPORT const KComponentData &mainComponent();
00321
00326 KDECORE_EXPORT bool hasMainComponent();
00327
00332 KDECORE_EXPORT KStandardDirs *dirs();
00333
00338 KDECORE_EXPORT KSharedConfigPtr config();
00339
00344 KDECORE_EXPORT KLocale *locale();
00349 KDECORE_EXPORT bool hasLocale();
00350
00355 KDECORE_EXPORT KCharsets *charsets();
00356
00361 KDECORE_EXPORT mode_t umask();
00362
00380 KDECORE_EXPORT const QString& staticQString(const char *str);
00381
00399 KDECORE_EXPORT const QString& staticQString(const QString &str);
00400
00422 KDECORE_EXPORT void ref();
00423
00428 KDECORE_EXPORT void deref();
00429
00445 KDECORE_EXPORT void setAllowQuit(bool allowQuit);
00446
00453 KDECORE_EXPORT KComponentData activeComponent();
00454
00461 KDECORE_EXPORT void setActiveComponent(const KComponentData &d);
00462
00471 KDECORE_EXPORT QString caption();
00472
00476 enum CopyCatalogs { DoCopyCatalogs, DontCopyCatalogs};
00477
00479 KDECORE_EXPORT void setLocale(KLocale *, CopyCatalogs copy = DoCopyCatalogs);
00480 }
00481
00482 #ifdef KDE_SUPPORT
00483
00488 #define KMIN(a,b) qMin(a,b)
00489
00494 #define KMAX(a,b) qMax(a,b)
00495
00500 #define KABS(a) qAbs(a)
00501
00507 #define KCLAMP(x,low,high) qBound(low,x,high)
00508
00509 #define kMin qMin
00510 #define kMax qMax
00511 #define kAbs qAbs
00512
00520 template<class T>
00521 inline KDE_DEPRECATED T kClamp( const T& x, const T& low, const T& high )
00522 {
00523 if ( x < low ) return low;
00524 else if ( high < x ) return high;
00525 return x;
00526 }
00527
00528 #endif
00529
00530 #endif // _KGLOBAL_H
00531