From 5eacb67368b6f5475455a1667c0ddfcbf9118859 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Fri, 27 May 2022 17:55:27 +0200 Subject: [PATCH] Provide deferred execution also for the non-Qt case --- src/tl/tl/tlDeferredExecution.cc | 54 ++++++++++++++++++- src/tl/tl/tlDeferredExecution.h | 22 ++------ src/tl/tl/tlWebDAV.h | 4 ++ src/tl/unit_tests/tlDeferredExecutionTests.cc | 41 +++++++++----- src/tl/unit_tests/tlWebDAVTests.cc | 4 ++ 5 files changed, 93 insertions(+), 32 deletions(-) diff --git a/src/tl/tl/tlDeferredExecution.cc b/src/tl/tl/tlDeferredExecution.cc index 4577784de..baf9fa4b4 100644 --- a/src/tl/tl/tlDeferredExecution.cc +++ b/src/tl/tl/tlDeferredExecution.cc @@ -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; } } diff --git a/src/tl/tl/tlDeferredExecution.h b/src/tl/tl/tlDeferredExecution.h index 380859e92..ef8f53d2d 100644 --- a/src/tl/tl/tlDeferredExecution.h +++ b/src/tl/tl/tlDeferredExecution.h @@ -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 diff --git a/src/tl/tl/tlWebDAV.h b/src/tl/tl/tlWebDAV.h index 380a6e325..2e98332af 100644 --- a/src/tl/tl/tlWebDAV.h +++ b/src/tl/tl/tlWebDAV.h @@ -30,6 +30,10 @@ #include #include +#if !defined(HAVE_CURL) && !defined(HAVE_QT) +# error "tlWebDAV.h can only be used with either curl or Qt enabled" +#endif + namespace tl { diff --git a/src/tl/unit_tests/tlDeferredExecutionTests.cc b/src/tl/unit_tests/tlDeferredExecutionTests.cc index d71505159..f87aa7a02 100644 --- a/src/tl/unit_tests/tlDeferredExecutionTests.cc +++ b/src/tl/unit_tests/tlDeferredExecutionTests.cc @@ -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); } diff --git a/src/tl/unit_tests/tlWebDAVTests.cc b/src/tl/unit_tests/tlWebDAVTests.cc index 6e7346030..b72ee0941 100644 --- a/src/tl/unit_tests/tlWebDAVTests.cc +++ b/src/tl/unit_tests/tlWebDAVTests.cc @@ -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 +