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
00025
00026
00027
00028
00029
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();
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);
00374
00392 KDECORE_EXPORT const QString& staticQString(const QString &str);
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