From 00b62d2b652a2c6ef389a0c0243a41ad9ad8ba2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Sun, 11 Nov 2018 20:32:01 +0100 Subject: [PATCH] Add @thirtythreeforty's `std::function<>` variant (#4892) Also convert `batchqueue.cc` where applicable. --- rtgui/batchqueue.cc | 69 +++++++++++++++++++-------------------------- rtgui/guiutils.cc | 13 +++++++++ rtgui/guiutils.h | 2 ++ 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/rtgui/batchqueue.cc b/rtgui/batchqueue.cc index bd7c4b015..2841c6811 100644 --- a/rtgui/batchqueue.cc +++ b/rtgui/batchqueue.cc @@ -39,25 +39,6 @@ using namespace std; using namespace rtengine; -namespace -{ - -struct NLParams { - BatchQueueListener* listener; - int qsize; - bool queueEmptied; - bool queueError; - Glib::ustring queueErrorMessage; -}; - -bool bqnotifylistenerUI(NLParams* params) -{ - params->listener->queueSizeChanged (params->qsize, params->queueEmptied, params->queueError, params->queueErrorMessage); - return false; -} - -} - BatchQueue::BatchQueue (FileCatalog* aFileCatalog) : processing(nullptr), fileCatalog(aFileCatalog), sequence(0), listener(nullptr) { @@ -604,14 +585,13 @@ void BatchQueue::setProgress(double p) } // No need to acquire the GUI, setProgressUI will do it - const auto func = - [](BatchQueue* bq) -> bool + idle_register.add( + [this]() -> bool { - bq->redraw(); + redraw(); return false; - }; - - idle_register.add(func, this, false); + } + ); } void BatchQueue::setProgressStr(const Glib::ustring& str) @@ -636,12 +616,15 @@ void BatchQueue::error(const Glib::ustring& descr) } if (listener) { - NLParams* params = new NLParams; - params->listener = listener; - params->queueEmptied = false; - params->queueError = true; - params->queueErrorMessage = descr; - idle_register.add(bqnotifylistenerUI, params, true); + BatchQueueListener* const listener_copy = listener; + + idle_register.add( + [listener_copy, descr]() -> bool + { + listener_copy->queueSizeChanged(0, false, true, descr); + return false; + } + ); } } @@ -975,15 +958,21 @@ void BatchQueue::notifyListener (bool queueEmptied) { if (listener) { - NLParams* params = new NLParams; - params->listener = listener; - { - MYREADERLOCK(l, entryRW); - params->qsize = fd.size(); - } - params->queueEmptied = queueEmptied; - params->queueError = false; - idle_register.add(bqnotifylistenerUI, params, true); + BatchQueueListener* const listener_copy = listener; + const std::size_t queue_size = + [](MyRWMutex& mutex, const std::vector& fd) -> std::size_t + { + MYREADERLOCK(l, mutex); + return fd.size(); + }(entryRW, fd); + + idle_register.add( + [listener_copy, queue_size, queueEmptied]() -> bool + { + listener_copy->queueSizeChanged(queue_size, queueEmptied, false, {}); + return false; + } + ); } } diff --git a/rtgui/guiutils.cc b/rtgui/guiutils.cc index 257927b0c..bd9828933 100644 --- a/rtgui/guiutils.cc +++ b/rtgui/guiutils.cc @@ -43,6 +43,19 @@ IdleRegister::~IdleRegister() destroy(); } +void IdleRegister::add(std::function function, gint priority) +{ + using Function = std::function; + + const auto func = + [](Function* function) + { + return (*function)(); + }; + + add(func, new Function(std::move(function)), true, priority); +} + void IdleRegister::destroy() { mutex.lock(); diff --git a/rtgui/guiutils.h b/rtgui/guiutils.h index 2cb7ca77e..43f94e4db 100644 --- a/rtgui/guiutils.h +++ b/rtgui/guiutils.h @@ -19,6 +19,7 @@ #ifndef __GUI_UTILS_ #define __GUI_UTILS_ +#include #include #include @@ -98,6 +99,7 @@ public: mutex.unlock(); } + void add(std::function function, gint priority = G_PRIORITY_DEFAULT_IDLE); void destroy(); private: