From: Gunnar Beutner Date: Sat, 14 Jul 2012 11:57:20 +0000 (+0200) Subject: Cleaned up AsyncTask class. X-Git-Tag: v0.0.1~239 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6cbccdc91cd09e910a958fa3dba7d3d2eebe23ab;p=icinga2 Cleaned up AsyncTask class. --- diff --git a/base/Makefile.am b/base/Makefile.am index 24d60168d..dbf52b8fc 100644 --- a/base/Makefile.am +++ b/base/Makefile.am @@ -7,6 +7,7 @@ pkglib_LTLIBRARIES = \ libbase_la_SOURCES = \ application.cpp \ application.h \ + asynctask.cpp \ asynctask.h \ component.cpp \ component.h \ diff --git a/base/asynctask.cpp b/base/asynctask.cpp new file mode 100644 index 000000000..7b09f83d9 --- /dev/null +++ b/base/asynctask.cpp @@ -0,0 +1,50 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#include "i2-base.h" + +using namespace icinga; + +AsyncTask::AsyncTask(const AsyncTask::CompletionCallback& completionCallback) + : m_Finished(false), m_CompletionCallback(completionCallback) +{ } + +AsyncTask::~AsyncTask(void) +{ + assert(m_Finished); +} + +void AsyncTask::Start(void) +{ + assert(Application::IsMainThread()); + + Run(); +} + +void AsyncTask::Finish(void) +{ + Event::Post(boost::bind(&AsyncTask::ForwardCallback, static_cast(GetSelf()))); +} + +void AsyncTask::ForwardCallback(void) +{ + m_CompletionCallback(GetSelf()); + m_CompletionCallback = CompletionCallback(); + m_Finished = true; +} \ No newline at end of file diff --git a/base/asynctask.h b/base/asynctask.h index 9e25e7f32..d98dfd012 100644 --- a/base/asynctask.h +++ b/base/asynctask.h @@ -23,46 +23,26 @@ namespace icinga { -template -class AsyncTask : public Object +class I2_BASE_API AsyncTask : public Object { public: - typedef shared_ptr > Ptr; - typedef weak_ptr > WeakPtr; + typedef shared_ptr Ptr; + typedef weak_ptr WeakPtr; - typedef function&)> CompletionCallback; + typedef function CompletionCallback; - AsyncTask(const CompletionCallback& completionCallback) - : m_Finished(false), m_CompletionCallback(completionCallback) - { } + AsyncTask(const CompletionCallback& completionCallback); + ~AsyncTask(void); - ~AsyncTask(void) - { - assert(m_Finished); - } - - void Start(void) - { - assert(Application::IsMainThread()); - - Run(); - } + void Start(void); protected: virtual void Run(void) = 0; - void Finish(void) - { - Event::Post(boost::bind(&T::ForwardCallback, static_cast >(GetSelf()))); - } + void Finish(void); private: - void ForwardCallback(void) - { - m_CompletionCallback(GetSelf()); - m_CompletionCallback = CompletionCallback(); - m_Finished = true; - } + void ForwardCallback(void); bool m_Finished; CompletionCallback m_CompletionCallback; diff --git a/base/base.vcxproj b/base/base.vcxproj index ad0ca6176..3365e7e91 100644 --- a/base/base.vcxproj +++ b/base/base.vcxproj @@ -12,6 +12,7 @@ + diff --git a/base/base.vcxproj.filters b/base/base.vcxproj.filters index 58d2e0a6a..9e59fd00e 100644 --- a/base/base.vcxproj.filters +++ b/base/base.vcxproj.filters @@ -79,6 +79,9 @@ Quelldateien + + Quelldateien + diff --git a/base/i2-base.h b/base/i2-base.h index a0b1663fa..edc81aab0 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -55,6 +55,7 @@ #ifdef _MSC_VER # pragma warning(disable:4251) # pragma warning(disable:4275) +# pragma warning(disable:4345) # define _CRT_SECURE_NO_DEPRECATE # define _CRT_SECURE_NO_WARNINGS #else /* _MSC_VER */ diff --git a/base/process.cpp b/base/process.cpp index 50c4feabe..23bde9c51 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -31,7 +31,7 @@ deque Process::m_Tasks; condition_variable Process::m_TasksCV; Process::Process(const string& command, const CompletionCallback& completionCallback) - : AsyncTask(completionCallback), m_Command(command), m_UsePopen(false) + : AsyncTask(completionCallback), m_Command(command), m_UsePopen(false) { if (!m_ThreadCreated) { thread t(&Process::WorkerThreadProc); diff --git a/base/process.h b/base/process.h index a5d11b85a..ec9bde5b9 100644 --- a/base/process.h +++ b/base/process.h @@ -23,7 +23,7 @@ namespace icinga { -class I2_BASE_API Process : public AsyncTask +class I2_BASE_API Process : public AsyncTask { public: typedef shared_ptr Ptr; diff --git a/cib/checktask.cpp b/cib/checktask.cpp index e75f3799d..cab5ebeff 100644 --- a/cib/checktask.cpp +++ b/cib/checktask.cpp @@ -24,7 +24,7 @@ using namespace icinga; map CheckTask::m_Types; CheckTask::CheckTask(const Service& service, const CompletionCallback& completionCallback) - : AsyncTask(completionCallback), m_Service(service) + : AsyncTask(completionCallback), m_Service(service) { } Service& CheckTask::GetService(void) diff --git a/cib/checktask.h b/cib/checktask.h index db20668c7..a7c43c5a7 100644 --- a/cib/checktask.h +++ b/cib/checktask.h @@ -25,7 +25,7 @@ namespace icinga struct CheckTaskType; -class I2_CIB_API CheckTask : public AsyncTask +class I2_CIB_API CheckTask : public AsyncTask { public: typedef shared_ptr Ptr; diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index 6c7d5416e..b24fedba9 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -93,8 +93,9 @@ void CheckerComponent::CheckTimerHandler(void) Logger::Write(LogInformation, "checker", msgbuf.str()); } -void CheckerComponent::CheckCompletedHandler(const CheckTask::Ptr& task) +void CheckerComponent::CheckCompletedHandler(const AsyncTask::Ptr& atask) { + CheckTask::Ptr task = static_pointer_cast(atask); Service service = task->GetService(); service.RemoveTag("current_task"); diff --git a/components/checker/checkercomponent.h b/components/checker/checkercomponent.h index b3e6b7ff1..3700d4e31 100644 --- a/components/checker/checkercomponent.h +++ b/components/checker/checkercomponent.h @@ -60,7 +60,7 @@ private: void CheckTimerHandler(void); void ResultTimerHandler(void); - void CheckCompletedHandler(const CheckTask::Ptr& task); + void CheckCompletedHandler(const AsyncTask::Ptr& task); void AdjustCheckTimer(void);