Provide deferred execution also for the non-Qt case

This commit is contained in:
Matthias Koefferlein 2022-05-27 17:55:27 +02:00
parent e26afab4e5
commit 5eacb67368
5 changed files with 93 additions and 32 deletions

View File

@ -34,6 +34,21 @@ namespace tl
static DeferredMethodScheduler *s_inst = 0;
// -----------------------------------------------------------------------------------
/**
* @brief A default scheduler which is used for example in non-Qt environments
*/
class DefaultDeferredMethodScheduler
: public DeferredMethodScheduler
{
public:
DefaultDeferredMethodScheduler () : DeferredMethodScheduler () { }
void queue_event() {}
};
// -----------------------------------------------------------------------------------
DeferredMethodScheduler::DeferredMethodScheduler ()
: m_disabled (0), m_scheduled (false)
{
@ -55,9 +70,35 @@ DeferredMethodScheduler::instance ()
new DeferredMethodSchedulerQt ();
}
#endif
if (! s_inst) {
new DefaultDeferredMethodScheduler ();
}
return s_inst;
}
void
DeferredMethodScheduler::enable (bool en)
{
if (instance ()) {
instance ()->do_enable (en);
}
}
void
DeferredMethodScheduler::execute ()
{
if (instance ()) {
while (instance ()->do_execute ())
;
}
}
bool
DeferredMethodScheduler::is_disabled () const
{
return m_disabled;
}
void
DeferredMethodScheduler::schedule (DeferredMethodBase *method)
{
@ -105,10 +146,18 @@ DeferredMethodScheduler::do_enable (bool en)
}
}
void
bool
DeferredMethodScheduler::do_execute ()
{
m_lock.lock ();
if (m_disabled) {
m_lock.unlock ();
return false;
}
bool any_pending = false;
m_executing.clear ();
m_unqueued.clear ();
m_executing.swap (m_methods);
@ -143,7 +192,10 @@ DeferredMethodScheduler::do_execute ()
m_lock.lock ();
m_unqueued.clear ();
m_executing.clear ();
any_pending = !m_methods.empty ();
m_lock.unlock ();
return any_pending;
}
}

View File

@ -90,32 +90,19 @@ public:
* Enabling is cumulative: multiple enable(true) calls must be matched to
* the same number of enable(false) calls.
*/
static void enable (bool en)
{
if (instance ()) {
instance ()->do_enable (en);
}
}
static void enable (bool en);
/**
* @brief Execute all queued methods
*
* This method can be called to force execution of all queued methods.
*/
static void execute ()
{
if (instance ()) {
instance ()->do_execute ();
}
}
static void execute ();
/**
* @brief Gets a value indicating whether the scheduler is disabled
*/
bool is_disabled () const
{
return m_disabled;
}
bool is_disabled () const;
protected:
/**
@ -126,8 +113,9 @@ protected:
/**
* @brief Executes the pending methods
* Returns true if more calls are required because handlers have issued more calls
*/
void do_execute ();
bool do_execute ();
/**
* @brief Constructor

View File

@ -30,6 +30,10 @@
#include <string>
#include <vector>
#if !defined(HAVE_CURL) && !defined(HAVE_QT)
# error "tlWebDAV.h can only be used with either curl or Qt enabled"
#endif
namespace tl
{

View File

@ -40,11 +40,24 @@ public:
int na, nb;
};
void trigger_execution ()
{
#if defined(HAVE_QT)
if (QCoreApplication::instance ()) {
QCoreApplication::instance ()->processEvents ();
return;
}
#endif
// explicit execute if timer does not do it
tl::DeferredMethodScheduler::execute ();
}
TEST(1)
{
g_na = g_nb = 0;
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
X *x = new X ();
@ -53,7 +66,7 @@ TEST(1)
EXPECT_EQ (g_na, 0);
EXPECT_EQ (g_nb, 0);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 0);
EXPECT_EQ (x->nb, 0);
@ -71,7 +84,7 @@ TEST(1)
tl::DeferredMethodScheduler::enable (false);
tl::DeferredMethodScheduler::enable (false);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 0);
EXPECT_EQ (x->nb, 0);
@ -83,7 +96,7 @@ TEST(1)
x->db ();
x->db ();
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 0);
EXPECT_EQ (x->nb, 0);
@ -92,14 +105,14 @@ TEST(1)
tl::DeferredMethodScheduler::enable (true);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 1);
EXPECT_EQ (x->nb, 2);
EXPECT_EQ (g_na, 1);
EXPECT_EQ (g_nb, 2);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 1);
EXPECT_EQ (x->nb, 2);
@ -116,14 +129,14 @@ TEST(1)
EXPECT_EQ (g_na, 1);
EXPECT_EQ (g_nb, 2);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 2);
EXPECT_EQ (x->nb, 4);
EXPECT_EQ (g_na, 2);
EXPECT_EQ (g_nb, 4);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 2);
EXPECT_EQ (x->nb, 4);
@ -132,7 +145,7 @@ TEST(1)
delete x;
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
x = new X ();
@ -141,7 +154,7 @@ TEST(1)
EXPECT_EQ (g_na, 2);
EXPECT_EQ (g_nb, 4);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 0);
EXPECT_EQ (x->nb, 0);
@ -158,14 +171,14 @@ TEST(1)
EXPECT_EQ (g_na, 2);
EXPECT_EQ (g_nb, 4);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 1);
EXPECT_EQ (x->nb, 2);
EXPECT_EQ (g_na, 3);
EXPECT_EQ (g_nb, 6);
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (x->na, 1);
EXPECT_EQ (x->nb, 2);
@ -179,7 +192,7 @@ TEST(1)
delete x;
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (g_na, 3);
EXPECT_EQ (g_nb, 6);
@ -208,7 +221,7 @@ TEST(2)
y->da ();
y->db ();
QCoreApplication::instance ()->processEvents ();
trigger_execution ();
EXPECT_EQ (y_inst, 0);
}

View File

@ -20,6 +20,7 @@
*/
#if defined(HAVE_CURL) || defined(HAVE_QT)
#include "tlWebDAV.h"
#include "tlHttpStream.h"
@ -149,3 +150,6 @@ TEST(5)
EXPECT_EQ (ba21, "A text II.I.\n");
text21.close ();
}
#endif