Improve Q_CONSTRUCTOR_FUNCTION and Q_DESTRUCTOR_FUNCTION macros.
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>
Mon, 28 Nov 2011 14:40:35 +0000 (15:40 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 1 Dec 2011 13:35:42 +0000 (14:35 +0100)
By adding anonymous namespace and static linkage we are reducing
visibility of implementation of these macros.

This patch also fixes warning about a declared but unused variable which
was issued by gcc 4.6 for Q_CONSTRUCTOR_FUNCTION.

Change-Id: I2cb70ad4c93f6f77e5518420abcce6fd4cadccfa
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
src/corelib/global/qglobal.h
src/corelib/kernel/qvariant.h
src/gui/kernel/qguiapplication.cpp
src/gui/kernel/qguivariant.cpp
src/gui/painting/qpaintbuffer.cpp
src/widgets/animation/qguivariantanimation.cpp
src/widgets/kernel/qapplication.cpp
src/widgets/kernel/qwidgetsvariant.cpp
src/widgets/statemachine/qguistatemachine.cpp
tests/auto/corelib/global/qglobal/tst_qglobal.cpp

index 20f3ca9..aa6a1e8 100644 (file)
@@ -373,23 +373,6 @@ namespace QT_NAMESPACE {}
    Should be sorted most to least authoritative.
 */
 
-#if defined(__ghs)
-# define Q_OUTOFLINE_TEMPLATE inline
-
-/* the following are necessary because the GHS C++ name mangling relies on __*/
-# define Q_CONSTRUCTOR_FUNCTION0(AFUNC) \
-   static const int AFUNC ## _init_variable_ = AFUNC();
-# define Q_CONSTRUCTOR_FUNCTION(AFUNC) Q_CONSTRUCTOR_FUNCTION0(AFUNC)
-# define Q_DESTRUCTOR_FUNCTION0(AFUNC) \
-    class AFUNC ## _dest_class_ { \
-    public: \
-       inline AFUNC ## _dest_class_() { } \
-       inline ~ AFUNC ## _dest_class_() { AFUNC(); } \
-    } AFUNC ## _dest_instance_;
-# define Q_DESTRUCTOR_FUNCTION(AFUNC) Q_DESTRUCTOR_FUNCTION0(AFUNC)
-
-#endif
-
 /* Symantec C++ is now Digital Mars */
 #if defined(__DMC__) || defined(__SC__)
 #  define Q_CC_SYM
@@ -827,17 +810,23 @@ namespace QT_NAMESPACE {}
 
 #ifndef Q_CONSTRUCTOR_FUNCTION
 # define Q_CONSTRUCTOR_FUNCTION0(AFUNC) \
-   static const int AFUNC ## __init_variable__ = AFUNC();
+    namespace { \
+    static const struct AFUNC ## _ctor_class_ { \
+        inline AFUNC ## _ctor_class_() { AFUNC(); } \
+    } AFUNC ## _ctor_instance_; \
+    }
+
 # define Q_CONSTRUCTOR_FUNCTION(AFUNC) Q_CONSTRUCTOR_FUNCTION0(AFUNC)
 #endif
 
 #ifndef Q_DESTRUCTOR_FUNCTION
 # define Q_DESTRUCTOR_FUNCTION0(AFUNC) \
-    class AFUNC ## __dest_class__ { \
-    public: \
-       inline AFUNC ## __dest_class__() { } \
-       inline ~ AFUNC ## __dest_class__() { AFUNC(); } \
-    } AFUNC ## __dest_instance__;
+    namespace { \
+    static const struct AFUNC ## _dtor_class_ { \
+        inline AFUNC ## _dtor_class_() { } \
+        inline ~ AFUNC ## _dtor_class_() { AFUNC(); } \
+    } AFUNC ## _dtor_instance_; \
+    }
 # define Q_DESTRUCTOR_FUNCTION(AFUNC) Q_DESTRUCTOR_FUNCTION0(AFUNC)
 #endif
 
index e082659..02458a0 100644 (file)
@@ -371,8 +371,8 @@ class Q_CORE_EXPORT QVariant
 
 protected:
     friend inline bool qvariant_cast_helper(const QVariant &, QVariant::Type, void *);
-    friend int qRegisterGuiVariant();
-    friend int qUnregisterGuiVariant();
+    friend void qRegisterGuiVariant();
+    friend void qUnregisterGuiVariant();
     friend inline bool operator==(const QVariant &, const QVariantComparisonHelper &);
 #ifndef QT_NO_DEBUG_STREAM
     friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant &);
index 859295d..da981b5 100644 (file)
@@ -126,8 +126,8 @@ QWindow *QGuiApplicationPrivate::focus_window = 0;
 Q_GLOBAL_STATIC(QMutex, applicationFontMutex)
 QFont *QGuiApplicationPrivate::app_font = 0;
 
-extern int qRegisterGuiVariant();
-extern int qUnregisterGuiVariant();
+extern void qRegisterGuiVariant();
+extern void qUnregisterGuiVariant();
 extern void qInitDrawhelperAsm();
 extern void qInitImageConversions();
 
index 9198e7f..3b60f29 100644 (file)
@@ -486,20 +486,18 @@ static const QMetaTypeInterface qVariantGuiHelper[] = {
 #undef QT_IMPL_METATYPEINTERFACE_GUI_TYPES
 
 static const QVariant::Handler *qt_guivariant_last_handler = 0;
-int qRegisterGuiVariant()
+void qRegisterGuiVariant()
 {
     qt_guivariant_last_handler = QVariant::handler;
     QVariant::handler = &qt_gui_variant_handler;
     qMetaTypeGuiHelper = qVariantGuiHelper;
-    return 1;
 }
 Q_CONSTRUCTOR_FUNCTION(qRegisterGuiVariant)
 
-int qUnregisterGuiVariant()
+void qUnregisterGuiVariant()
 {
     QVariant::handler = qt_guivariant_last_handler;
     qMetaTypeGuiHelper = 0;
-    return 1;
 }
 Q_DESTRUCTOR_FUNCTION(qUnregisterGuiVariant)
 
index ecc0280..f9e8432 100644 (file)
@@ -2166,14 +2166,12 @@ QDataStream &operator>>(QDataStream &stream, QPaintBufferCacheEntryV2 &entry)
     return stream >> entry.bits;
 }
 
-static int qRegisterPaintBufferMetaTypes()
+static void qRegisterPaintBufferMetaTypes()
 {
     qRegisterMetaType<QPaintBufferCacheEntry>();
     qRegisterMetaTypeStreamOperators<QPaintBufferCacheEntry>("QPaintBufferCacheEntry");
     qRegisterMetaType<QPaintBufferCacheEntryV2>();
     qRegisterMetaTypeStreamOperators<QPaintBufferCacheEntryV2>("QPaintBufferCacheEntryV2");
-
-    return 0; // something
 }
 
 Q_CONSTRUCTOR_FUNCTION(qRegisterPaintBufferMetaTypes)
index b0b18ed..f99b74e 100644 (file)
@@ -64,18 +64,17 @@ template<> Q_INLINE_TEMPLATE QQuaternion _q_interpolate(const QQuaternion &f,con
     return QQuaternion::slerp(f, t, progress);
 }
 
-static int qRegisterGuiGetInterpolator()
+static void qRegisterGuiGetInterpolator()
 {
     qRegisterAnimationInterpolator<QColor>(_q_interpolateVariant<QColor>);
     qRegisterAnimationInterpolator<QVector2D>(_q_interpolateVariant<QVector2D>);
     qRegisterAnimationInterpolator<QVector3D>(_q_interpolateVariant<QVector3D>);
     qRegisterAnimationInterpolator<QVector4D>(_q_interpolateVariant<QVector4D>);
     qRegisterAnimationInterpolator<QQuaternion>(_q_interpolateVariant<QQuaternion>);
-    return 1;
 }
 Q_CONSTRUCTOR_FUNCTION(qRegisterGuiGetInterpolator)
 
-static int qUnregisterGuiGetInterpolator()
+static void qUnregisterGuiGetInterpolator()
 {
     // casts required by Sun CC 5.5
     qRegisterAnimationInterpolator<QColor>(
@@ -88,8 +87,6 @@ static int qUnregisterGuiGetInterpolator()
         (QVariant (*)(const QVector4D &, const QVector4D &, qreal))0);
     qRegisterAnimationInterpolator<QQuaternion>(
         (QVariant (*)(const QQuaternion &, const QQuaternion &, qreal))0);
-
-    return 1;
 }
 Q_DESTRUCTOR_FUNCTION(qUnregisterGuiGetInterpolator)
 
index beeb3da..472f19a 100644 (file)
@@ -859,8 +859,8 @@ QApplication::QApplication(Display *dpy, int &argc, char **argv,
 #endif // Q_WS_X11
 
 #ifndef QT_NO_STATEMACHINE
-extern int qRegisterGuiStateMachine();
-extern int qUnregisterGuiStateMachine();
+void qRegisterGuiStateMachine();
+void qUnregisterGuiStateMachine();
 #endif
 
 /*!
index 2e945d2..18fec50 100644 (file)
@@ -151,19 +151,17 @@ static const QMetaTypeInterface qVariantWidgetsHelper[] = {
 
 extern Q_GUI_EXPORT const QVariant::Handler *qt_widgets_variant_handler;
 
-int qRegisterWidgetsVariant()
+void qRegisterWidgetsVariant()
 {
     qt_widgets_variant_handler = &widgets_handler;
     qMetaTypeWidgetsHelper = qVariantWidgetsHelper;
-    return 1;
 }
 Q_CONSTRUCTOR_FUNCTION(qRegisterWidgetsVariant)
 
-int qUnregisterWidgetsVariant()
+void qUnregisterWidgetsVariant()
 {
     qt_widgets_variant_handler = 0;
     qMetaTypeWidgetsHelper = 0;
-    return 1;
 }
 Q_DESTRUCTOR_FUNCTION(qUnregisterWidgetsVariant)
 
index 7a498eb..f717662 100644 (file)
@@ -474,18 +474,16 @@ const QStateMachinePrivate::Handler qt_gui_statemachine_handler = {
 };
 
 static const QStateMachinePrivate::Handler *qt_guistatemachine_last_handler = 0;
-int qRegisterGuiStateMachine()
+void qRegisterGuiStateMachine()
 {
     qt_guistatemachine_last_handler = QStateMachinePrivate::handler;
     QStateMachinePrivate::handler = &qt_gui_statemachine_handler;
-    return 1;
 }
 Q_CONSTRUCTOR_FUNCTION(qRegisterGuiStateMachine)
 
-int qUnregisterGuiStateMachine()
+void qUnregisterGuiStateMachine()
 {
     QStateMachinePrivate::handler = qt_guistatemachine_last_handler;
-    return 1;
 }
 Q_DESTRUCTOR_FUNCTION(qUnregisterGuiStateMachine)
 
index b3115e0..23ec228 100644 (file)
@@ -53,6 +53,7 @@ private slots:
     void qtry();
     void checkptr();
     void qstaticassert();
+    void qConstructorFunction();
 };
 
 void tst_QGlobal::qIsNull()
@@ -353,5 +354,17 @@ void tst_QGlobal::qstaticassert()
     QVERIFY(true); // if the test compiles it has passed.
 }
 
+static int qConstructorFunctionValue;
+static void qConstructorFunctionCtor()
+{
+    qConstructorFunctionValue = 123;
+}
+Q_CONSTRUCTOR_FUNCTION(qConstructorFunctionCtor);
+
+void tst_QGlobal::qConstructorFunction()
+{
+    QCOMPARE(qConstructorFunctionValue, 123);
+}
+
 QTEST_MAIN(tst_QGlobal)
 #include "tst_qglobal.moc"