diff options
author | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2011-03-04 10:29:33 (GMT) |
---|---|---|
committer | Jedrzej Nowacki <jedrzej.nowacki@nokia.com> | 2011-03-07 09:09:49 (GMT) |
commit | abff4d5090c1706c44485bbe3689a0e339c26a9b (patch) | |
tree | 050bbf9d7636bf5a683b8e2bb14f2ce0d3238440 | |
parent | 106b19f108644e28d76e6f2a762fa15bd7f0edf5 (diff) | |
download | Qt-abff4d5090c1706c44485bbe3689a0e339c26a9b.zip Qt-abff4d5090c1706c44485bbe3689a0e339c26a9b.tar.gz Qt-abff4d5090c1706c44485bbe3689a0e339c26a9b.tar.bz2 |
Improve Q_GLOBAL_STATIC macors.
The patch fix small issues inside the macros. New features:
- Class friendly. The macro can be used inside class declaration to
define a static method instead of function.
- Encapsulation. Smaller default namespace pollution by hiding all this_
variables inside a function.
Reviewed-by: Joao
-rw-r--r-- | src/corelib/global/qglobal.h | 124 | ||||
-rw-r--r-- | src/network/bearer/qnetworkconfigmanager.cpp | 3 |
2 files changed, 64 insertions, 63 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 67ccf4d..19ef760 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1815,32 +1815,32 @@ public: inline ~QGlobalStatic() { pointer = 0; } }; -#define Q_GLOBAL_STATIC(TYPE, NAME) \ - static TYPE *NAME() \ - { \ - static TYPE this_##NAME; \ - static QGlobalStatic<TYPE > global_##NAME(&this_##NAME); \ - return global_##NAME.pointer; \ +#define Q_GLOBAL_STATIC(TYPE, NAME) \ + static TYPE *NAME() \ + { \ + static TYPE thisVariable; \ + static QGlobalStatic<TYPE > thisGlobalStatic(&thisVariable); \ + return thisGlobalStatic.pointer; \ } -#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \ - static TYPE *NAME() \ - { \ - static TYPE this_##NAME ARGS; \ - static QGlobalStatic<TYPE > global_##NAME(&this_##NAME); \ - return global_##NAME.pointer; \ +#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \ + static TYPE *NAME() \ + { \ + static TYPE thisVariable ARGS; \ + static QGlobalStatic<TYPE > thisGlobalStatic(&thisVariable); \ + return thisGlobalStatic.pointer; \ } -#define Q_GLOBAL_STATIC_WITH_INITIALIZER(TYPE, NAME, INITIALIZER) \ - static TYPE *NAME() \ - { \ - static TYPE this_##NAME; \ - static QGlobalStatic<TYPE > global_##NAME(0); \ - if (!global_##NAME.pointer) { \ - TYPE *x = global_##NAME.pointer = &this_##NAME; \ - INITIALIZER; \ - } \ - return global_##NAME.pointer; \ +#define Q_GLOBAL_STATIC_WITH_INITIALIZER(TYPE, NAME, INITIALIZER) \ + static TYPE *NAME() \ + { \ + static TYPE thisVariable; \ + static QGlobalStatic<TYPE > thisGlobalStatic(0); \ + if (!thisGlobalStatic.pointer) { \ + TYPE *x = thisGlobalStatic.pointer = &thisVariable; \ + INITIALIZER; \ + } \ + return thisGlobalStatic.pointer; \ } #else @@ -1875,50 +1875,50 @@ public: } }; -#define Q_GLOBAL_STATIC_INIT(TYPE, NAME) \ - static QGlobalStatic<TYPE > this_##NAME = { Q_BASIC_ATOMIC_INITIALIZER(0), false } - -#define Q_GLOBAL_STATIC(TYPE, NAME) \ - Q_GLOBAL_STATIC_INIT(TYPE, NAME); \ - static TYPE *NAME() \ - { \ - if (!this_##NAME.pointer && !this_##NAME.destroyed) { \ - TYPE *x = new TYPE; \ - if (!this_##NAME.pointer.testAndSetOrdered(0, x)) \ - delete x; \ - else \ - static QGlobalStaticDeleter<TYPE > cleanup(this_##NAME); \ - } \ - return this_##NAME.pointer; \ +#define Q_GLOBAL_STATIC(TYPE, NAME) \ + static TYPE *NAME() \ + { \ + static QGlobalStatic<TYPE > thisGlobalStatic \ + = { Q_BASIC_ATOMIC_INITIALIZER(0), false }; \ + if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) { \ + TYPE *x = new TYPE; \ + if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) \ + delete x; \ + else \ + static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \ + } \ + return thisGlobalStatic.pointer; \ } -#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \ - Q_GLOBAL_STATIC_INIT(TYPE, NAME); \ - static TYPE *NAME() \ - { \ - if (!this_##NAME.pointer && !this_##NAME.destroyed) { \ - TYPE *x = new TYPE ARGS; \ - if (!this_##NAME.pointer.testAndSetOrdered(0, x)) \ - delete x; \ - else \ - static QGlobalStaticDeleter<TYPE > cleanup(this_##NAME); \ - } \ - return this_##NAME.pointer; \ +#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \ + static TYPE *NAME() \ + { \ + static QGlobalStatic<TYPE > thisGlobalStatic \ + = { Q_BASIC_ATOMIC_INITIALIZER(0), false }; \ + if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) { \ + TYPE *x = new TYPE ARGS; \ + if (!thisGlobalStatic.pointer.testAndSetOrdered(0, x)) \ + delete x; \ + else \ + static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \ + } \ + return thisGlobalStatic.pointer; \ } -#define Q_GLOBAL_STATIC_WITH_INITIALIZER(TYPE, NAME, INITIALIZER) \ - Q_GLOBAL_STATIC_INIT(TYPE, NAME); \ - static TYPE *NAME() \ - { \ - if (!this_##NAME.pointer && !this_##NAME.destroyed) { \ - QScopedPointer<TYPE > x(new TYPE); \ - INITIALIZER; \ - if (this_##NAME.pointer.testAndSetOrdered(0, x.data())) { \ - static QGlobalStaticDeleter<TYPE > cleanup(this_##NAME); \ - x.take(); \ - } \ - } \ - return this_##NAME.pointer; \ +#define Q_GLOBAL_STATIC_WITH_INITIALIZER(TYPE, NAME, INITIALIZER) \ + static TYPE *NAME() \ + { \ + static QGlobalStatic<TYPE > thisGlobalStatic \ + = { Q_BASIC_ATOMIC_INITIALIZER(0), false }; \ + if (!thisGlobalStatic.pointer && !thisGlobalStatic.destroyed) { \ + QScopedPointer<TYPE > x(new TYPE); \ + INITIALIZER; \ + if (thisGlobalStatic.pointer.testAndSetOrdered(0, x.data())) { \ + static QGlobalStaticDeleter<TYPE > cleanup(thisGlobalStatic); \ + x.take(); \ + } \ + } \ + return thisGlobalStatic.pointer; \ } #endif diff --git a/src/network/bearer/qnetworkconfigmanager.cpp b/src/network/bearer/qnetworkconfigmanager.cpp index 2a46229..9e1eaea 100644 --- a/src/network/bearer/qnetworkconfigmanager.cpp +++ b/src/network/bearer/qnetworkconfigmanager.cpp @@ -52,7 +52,8 @@ QT_BEGIN_NAMESPACE #define Q_GLOBAL_STATIC_QAPP_DESTRUCTION(TYPE, NAME) \ - Q_GLOBAL_STATIC_INIT(TYPE, NAME); \ + static QGlobalStatic<TYPE > this_##NAME \ + = { Q_BASIC_ATOMIC_INITIALIZER(0), false }; \ static void NAME##_cleanup() \ { \ delete this_##NAME.pointer; \ |