Add a non-implicit copy constructor to QEvent
authorThiago Macieira <thiago.macieira@intel.com>
Wed, 4 Apr 2012 17:25:12 +0000 (14:25 -0300)
committerQt by Nokia <qt-info@nokia.com>
Wed, 16 May 2012 05:00:31 +0000 (07:00 +0200)
Copying events is a bad idea but it is permitted, used at least in the
state machine framework and QApplication, which somehow found it
amusing to clone events. We can't forbid it because it would be
source-incompatible with Qt 4, and other ill-advised developer may be
doing this.

In the new copy functions and in the destructor, ensure that the d
pointer is null. We can't copy it if it isn't. The exception is for
DeferredDelete events, which use the d pointer to store the loop level
count. Such value must not be deleted.

In the future, if QEvent::d is used at the QEvent level, make sure to
adapt QCoreApplication::postEvent to store the counter somewhere else.

Task-number: QTBUG-25070
Change-Id: I1f2d3f3cfc891ec216df2e8b7dbe531524d21b26
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
src/corelib/kernel/qcoreevent.cpp
src/corelib/kernel/qcoreevent.h

index 45b2293..ab015a4 100644 (file)
@@ -284,6 +284,45 @@ QEvent::QEvent(Type type)
 {}
 
 /*!
+    \internal
+    Attempts to copy the \a other event.
+
+    Copying events is a bad idea, yet some Qt 4 code does it (notably,
+    QApplication and the state machine).
+ */
+QEvent::QEvent(const QEvent &other)
+    : d(other.d), t(other.t), posted(other.posted), spont(other.spont),
+      m_accept(other.m_accept)
+{
+    if (t != QEvent::DeferredDelete) {
+        // if QEventPrivate becomes available, make sure to implement a
+        // virtual QEventPrivate *clone() const; function so we can copy here
+        Q_ASSERT_X(!d, "QEvent", "Impossible, this can't happen: QEventPrivate isn't defined anywhere");
+    }
+}
+
+/*!
+    \internal
+    Attempts to copy the \a other event.
+
+    Copying events is a bad idea, yet some Qt 4 code does it (notably,
+    QApplication and the state machine).
+ */
+QEvent &QEvent::operator=(const QEvent &other)
+{
+    if (t != QEvent::DeferredDelete) {
+        // if QEventPrivate becomes available, make sure to implement a
+        // virtual QEventPrivate *clone() const; function so we can copy here
+        Q_ASSERT_X(!other.d, "QEvent", "Impossible, this can't happen: QEventPrivate isn't defined anywhere");
+    }
+    t = other.t;
+    posted = other.posted;
+    spont = other.spont;
+    m_accept = other.m_accept;
+    return *this;
+}
+
+/*!
     Destroys the event. If it was \link
     QCoreApplication::postEvent() posted \endlink,
     it will be removed from the list of events to be posted.
@@ -293,6 +332,8 @@ QEvent::~QEvent()
 {
     if (posted && QCoreApplication::instance())
         QCoreApplicationPrivate::removePostedEvent(this);
+    if (t != QEvent::DeferredDelete)
+        Q_ASSERT_X(!d, "QEvent", "Impossible, this can't happen: QEventPrivate isn't defined anywhere");
 }
 
 
index a833394..177fc26 100644 (file)
@@ -283,7 +283,9 @@ public:
     };
 
     explicit QEvent(Type type);
+    QEvent(const QEvent &other);
     virtual ~QEvent();
+    QEvent &operator=(const QEvent &other);
     inline Type type() const { return static_cast<Type>(t); }
     inline bool spontaneous() const { return spont; }