瀏覽代碼

Merge branch '125-workaround-for-unsupported-qdatetime-msecsto'

* 125-workaround-for-unsupported-qdatetime-msecsto:
  Added workaround function for computing QDateTime differences in msecs.
Sascha Zelzer 13 年之前
父節點
當前提交
834f67b5ab

+ 8 - 0
Libs/Core/ctkUtils.cpp

@@ -314,3 +314,11 @@ QString ctk::qtHandleToString(Qt::HANDLE handle)
   s << handle;
   return str;
 }
+
+//-----------------------------------------------------------------------------
+qint64 ctk::msecsTo(const QDateTime& t1, const QDateTime& t2)
+{
+  qint64 days = t1.daysTo(t2);
+  qint64 msecs = t1.time().msecsTo(t2.time());
+  return days*(1000*3600*24) + msecs;
+}

+ 14 - 0
Libs/Core/ctkUtils.h

@@ -23,6 +23,7 @@
 
 // Qt includes
 #include <QStringList>
+#include <QDateTime>
 
 // STD includes
 #include <vector>
@@ -121,6 +122,19 @@ bool CTK_CORE_EXPORT removeDirRecursively(const QString & dirName);
 /// Convert Qt::HANDLE to string
 /// \sa Qt::HANDLE
 QString CTK_CORE_EXPORT qtHandleToString(Qt::HANDLE handle);
+
+
+///
+/// \ingroup Core
+/// \brief Compute the milli secons from one QDateTime to an other.
+///
+/// This function can be used to correctly compute the amount of milli
+/// seconds from <code>t1</code> to <code>t2</code>. This is for
+/// back-wards compatibility with Qt 4.6. Since Qt 4.7 there exists
+/// a QDateTime::msecsTo() method which should be used instead, after
+/// bumping the minimum required Qt version for CTK.
+qint64 CTK_CORE_EXPORT msecsTo(const QDateTime& t1, const QDateTime& t2);
+
 }
 
 #endif

+ 6 - 1
Libs/PluginFramework/ctkPluginPrivate.cpp

@@ -33,6 +33,9 @@
 #include "ctkServiceReferencePrivate.h"
 #include "ctkServiceRegistration.h"
 
+// for ctk::msecsTo() - remove after switching to Qt 4.7
+#include <ctkUtils.h>
+
 const ctkPlugin::States ctkPluginPrivate::RESOLVED_FLAGS = ctkPlugin::RESOLVED | ctkPlugin::STARTING | ctkPlugin::ACTIVE | ctkPlugin::STOPPING;
 
 //----------------------------------------------------------------------------
@@ -479,7 +482,9 @@ void ctkPluginPrivate::waitOnOperation(LockObject* lock, const QString& src, boo
       {
         return;
       }
-      left = QDateTime::currentDateTime().msecsTo(waitUntil);
+      // TODO use Qt 4.7 QDateTime::msecsTo() API
+      //left = QDateTime::currentDateTime().msecsTo(waitUntil);
+      left = ctk::msecsTo(QDateTime::currentDateTime(), waitUntil);
     } while (left > 0);
 
     QString op;

+ 5 - 2
Plugins/org.commontk.eventadmin/dispatch/ctkEALinkedQueue.cpp

@@ -25,6 +25,9 @@
 #include "ctkEAInterruptibleThread_p.h"
 #include "ctkEAInterruptedException_p.h"
 
+// for ctk::msecsTo() - remove after switching to Qt 4.7
+#include <ctkUtils.h>
+
 #include <QDateTime>
 
 
@@ -121,7 +124,7 @@ ctkEARunnable* ctkEALinkedQueue::poll(long msecs)
   {
     QMutexLocker l(&putLock_);
     try {
-      long waitTime = msecs;
+      qint64 waitTime = static_cast<qint64>(msecs);
       //TODO Use Qt4.7 API
       //long start = (msecs <= 0)? 0 : System.currentTimeMillis();
       QDateTime start = QDateTime::currentDateTime();
@@ -138,7 +141,7 @@ ctkEARunnable* ctkEALinkedQueue::poll(long msecs)
         {
           ctkEAInterruptibleThread::currentThread()->wait(&putLock_, &putLockWait_, waitTime);
           //waitTime = msecs - (System.currentTimeMillis() - start);
-          waitTime = msecs - start.time().msecsTo(QDateTime::currentDateTime().time());
+          waitTime = static_cast<qint64>(msecs) - ctk::msecsTo(start, QDateTime::currentDateTime());
         }
       }
     }

+ 6 - 3
Plugins/org.commontk.eventadmin/dispatch/ctkEAPooledExecutor.cpp

@@ -22,6 +22,9 @@
 
 #include "ctkEAPooledExecutor_p.h"
 
+// for ctk::msecsTo() - remove after switching to Qt 4.7
+#include <ctkUtils.h>
+
 #include "ctkEAChannel_p.h"
 #include <dispatch/ctkEAInterruptibleThread_p.h>
 #include <dispatch/ctkEAInterruptedException_p.h>
@@ -175,7 +178,7 @@ bool ctkEAPooledExecutor::awaitTerminationAfterShutdown(long maxWaitTime) const
     throw std::logic_error("not in shutdown state");
   if (poolSize_ == 0)
     return true;
-  long waitTime = maxWaitTime;
+  qint64 waitTime = static_cast<qint64>(maxWaitTime);
   if (waitTime <= 0)
     return false;
   //TODO Use Qt4.7 API
@@ -185,8 +188,8 @@ bool ctkEAPooledExecutor::awaitTerminationAfterShutdown(long maxWaitTime) const
     waitCond.wait(&shutdownMutex, waitTime);
     if (poolSize_ == 0)
       return true;
-    qint64 currWait = start.time().msecsTo(QDateTime::currentDateTime().time());
-    waitTime = maxWaitTime - currWait;
+    qint64 currWait = ctk::msecsTo(start, QDateTime::currentDateTime());
+    waitTime = static_cast<qint64>(maxWaitTime) - currWait;
     if (waitTime <= 0)
       return false;
   }

+ 7 - 6
Plugins/org.commontk.eventadmin/util/ctkEACyclicBarrier.cpp

@@ -22,6 +22,9 @@
 
 #include "ctkEACyclicBarrier_p.h"
 
+// for ctk::msecsTo() - remove after switching to Qt 4.7
+#include <ctkUtils.h>
+
 #include <QDateTime>
 #include <QRunnable>
 #include <QDebug>
@@ -128,10 +131,8 @@ int ctkEACyclicBarrier::doBarrier(bool timed, long msecs)
   else
   { // wait until next reset
     int r = resets_;
-    QTime startTime = QDateTime::currentDateTime().time();
-    //TODO use Qt 4.7 API
-    //long startTime = (timed)? QDateTime::toMSecsSinceEpoch() : 0;
-    long waitTime = msecs;
+    QDateTime startTime = QDateTime::currentDateTime();
+    qint64 waitTime = static_cast<qint64>(msecs);
     forever
     {
       try
@@ -165,8 +166,8 @@ int ctkEACyclicBarrier::doBarrier(bool timed, long msecs)
       else if (timed)
       {
         //TODO use Qt 4.7 API
-        //waitTime = msecs - (QDateTime::toMSecsSinceEpoch() - startTime);
-        waitTime = msecs - (startTime.msecsTo(QDateTime::currentDateTime().time()));
+        //waitTime = msecs - QDateTime::toMSecs(startTime);
+        waitTime = static_cast<qint64>(msecs) - ctk::msecsTo(startTime, QDateTime::currentDateTime());
         if  (waitTime <= 0)
         {
           broken_ = true;