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 #include <QtCore/QObject> 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 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 00318 class KDECORE_EXPORT KCatalogLoader 00319 { 00320 public: 00321 KCatalogLoader(const QString &catalogName); 00322 }; 00323 00333 namespace KGlobal 00334 { 00335 00342 KDECORE_EXPORT const KComponentData &mainComponent(); //krazy:exclude=constref (don't mess up ref-counting) 00343 00348 KDECORE_EXPORT bool hasMainComponent(); 00349 00354 KDECORE_EXPORT KStandardDirs *dirs(); 00355 00360 KDECORE_EXPORT KSharedConfigPtr config(); 00361 00368 KDECORE_EXPORT void insertCatalog(const QString& catalog); 00369 00380 KDECORE_EXPORT KLocale *locale(); 00385 KDECORE_EXPORT bool hasLocale(); 00386 00391 KDECORE_EXPORT KCharsets *charsets(); 00392 00397 KDECORE_EXPORT mode_t umask(); 00398 00416 KDECORE_EXPORT const QString& staticQString(const char *str); //krazy:exclude=constref (doesn't make sense otherwise) 00417 00435 KDECORE_EXPORT const QString& staticQString(const QString &str); //krazy:exclude=constref (doesn't make sense otherwise) 00436 00458 KDECORE_EXPORT void ref(); 00459 00464 KDECORE_EXPORT void deref(); 00465 00481 KDECORE_EXPORT void setAllowQuit(bool allowQuit); 00482 00489 KDECORE_EXPORT KComponentData activeComponent(); 00490 00497 KDECORE_EXPORT void setActiveComponent(const KComponentData &d); 00498 00507 KDECORE_EXPORT QString caption(); 00508 00510 KDECORE_EXPORT QObject* findDirectChild_helper(const QObject* parent, const QMetaObject& mo); 00511 00517 template<typename T> 00518 inline T findDirectChild(const QObject* object) { 00519 return static_cast<T>(findDirectChild_helper(object, (static_cast<T>(0))->staticMetaObject)); 00520 } 00521 00525 enum CopyCatalogs { DoCopyCatalogs, DontCopyCatalogs}; 00526 00528 KDECORE_EXPORT void setLocale(KLocale *, CopyCatalogs copy = DoCopyCatalogs); 00529 } 00530 00531 #ifdef KDE_SUPPORT 00532 00537 #define KMIN(a,b) qMin(a,b) 00538 00543 #define KMAX(a,b) qMax(a,b) 00544 00549 #define KABS(a) qAbs(a) 00550 00556 #define KCLAMP(x,low,high) qBound(low,x,high) 00557 00558 #define kMin qMin 00559 #define kMax qMax 00560 #define kAbs qAbs 00561 00569 template<class T> 00570 inline KDE_DEPRECATED T kClamp( const T& x, const T& low, const T& high ) 00571 { 00572 if ( x < low ) return low; 00573 else if ( high < x ) return high; 00574 return x; 00575 } 00576 00577 #endif 00578 00579 #endif // _KGLOBAL_H 00580
KDE 4.6 API Reference