From f12b52f465b111a40ecb37574ea9cd38c6ef2527 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 29 Aug 2016 08:16:33 +0200 Subject: [PATCH] Use lambdas in the ConfigItem class refs #12509 --- lib/base/workqueue.cpp | 4 +-- lib/base/workqueue.hpp | 2 +- lib/config/configitem.cpp | 66 +++++++++++++++++++-------------------- lib/config/configitem.hpp | 3 -- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/lib/base/workqueue.cpp b/lib/base/workqueue.cpp index 3b84e8976..1ded9c150 100644 --- a/lib/base/workqueue.cpp +++ b/lib/base/workqueue.cpp @@ -64,7 +64,7 @@ String WorkQueue::GetName(void) const * allowInterleaved is true in which case the new task might be run * immediately if it's being enqueued from within the WorkQueue thread. */ -void WorkQueue::Enqueue(const boost::function& function, WorkQueuePriority priority, +void WorkQueue::Enqueue(boost::function&& function, WorkQueuePriority priority, bool allowInterleaved) { bool wq_thread = IsWorkerThread(); @@ -93,7 +93,7 @@ void WorkQueue::Enqueue(const boost::function& function, WorkQueueP m_CVFull.wait(lock); } - m_Tasks.push(Task(function, priority, ++m_NextTaskID)); + m_Tasks.emplace(std::move(function), priority, ++m_NextTaskID); m_CVEmpty.notify_one(); } diff --git a/lib/base/workqueue.hpp b/lib/base/workqueue.hpp index 8b466cc70..22e9bc8be 100644 --- a/lib/base/workqueue.hpp +++ b/lib/base/workqueue.hpp @@ -86,7 +86,7 @@ public: void SetName(const String& name); String GetName(void) const; - void Enqueue(const boost::function& function, WorkQueuePriority priority = PriorityNormal, + void Enqueue(boost::function&& function, WorkQueuePriority priority = PriorityNormal, bool allowInterleaved = false); void Join(bool stop = false); diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index 197738438..71aadc3c2 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -384,35 +384,6 @@ ConfigItem::Ptr ConfigItem::GetByTypeAndName(const String& type, const String& n return it2->second; } -void ConfigItem::OnAllConfigLoadedHelper(void) -{ - try { - m_Object->OnAllConfigLoaded(); - } catch (const std::exception& ex) { - if (m_IgnoreOnError) { - Log(LogNotice, "ConfigObject") - << "Ignoring config object '" << m_Name << "' of type '" << m_Type << "' due to errors: " << DiagnosticInformation(ex); - - Unregister(); - - { - boost::mutex::scoped_lock lock(m_Mutex); - m_IgnoredItems.push_back(m_DebugInfo.Path); - } - - return; - } - - throw; - } -} - -void ConfigItem::CreateChildObjectsHelper(const Type::Ptr& type) -{ - ActivationScope ascope(m_ActivationContext); - m_Object->CreateChildObjects(type); -} - bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector& newItems) { typedef std::pair ItemPair; @@ -455,7 +426,9 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue for (const ItemPair& ip : items) { newItems.push_back(ip.first); - upq.Enqueue(boost::bind(&ConfigItem::Commit, ip.first, ip.second)); + upq.Enqueue([&]() { + ip.first->Commit(ip.second); + }); } upq.Join(); @@ -497,8 +470,29 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue if (!item->m_Object) continue; - if (item->m_Type == type) - upq.Enqueue(boost::bind(&ConfigItem::OnAllConfigLoadedHelper, item)); + if (item->m_Type == type) { + upq.Enqueue([&]() { + try { + item->m_Object->OnAllConfigLoaded(); + } catch (const std::exception& ex) { + if (item->m_IgnoreOnError) { + Log(LogNotice, "ConfigObject") + << "Ignoring config object '" << item->m_Name << "' of type '" << item->m_Type << "' due to errors: " << DiagnosticInformation(ex); + + item->Unregister(); + + { + boost::mutex::scoped_lock lock(item->m_Mutex); + item->m_IgnoredItems.push_back(item->m_DebugInfo.Path); + } + + return; + } + + throw; + } + }); + } } completed_types.insert(type); @@ -515,8 +509,12 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue if (!item->m_Object) continue; - if (item->m_Type == loadDep) - upq.Enqueue(boost::bind(&ConfigItem::CreateChildObjectsHelper, item, ptype)); + if (item->m_Type == loadDep) { + upq.Enqueue([&]() { + ActivationScope ascope(item->m_ActivationContext); + item->m_Object->CreateChildObjects(ptype); + }); + } } } diff --git a/lib/config/configitem.hpp b/lib/config/configitem.hpp index 6cd80aa64..bfae063d1 100644 --- a/lib/config/configitem.hpp +++ b/lib/config/configitem.hpp @@ -114,9 +114,6 @@ private: ConfigObject::Ptr Commit(bool discard = true); static bool CommitNewItems(const ActivationContext::Ptr& context, WorkQueue& upq, std::vector& newItems); - - void OnAllConfigLoadedHelper(void); - void CreateChildObjectsHelper(const Type::Ptr& type); }; } -- 2.40.0