From 02de634c12f663e86bd58503946ba37743d6d2d1 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 22 Jan 2013 09:21:50 +0100 Subject: [PATCH] Implement REGISTER_SCRIPTFUNCTION() and clean up how check types are registered. --- components/checker/checkercomponent.cpp | 4 ---- lib/base/dynamictype.h | 2 +- lib/base/scriptfunction.h | 21 ++++++++++++++++++++- lib/icinga/nullchecktask.cpp | 8 ++------ lib/icinga/nullchecktask.h | 3 --- lib/icinga/pluginchecktask.cpp | 8 ++------ lib/icinga/pluginchecktask.h | 3 +-- 7 files changed, 26 insertions(+), 23 deletions(-) diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp index 928289a47..744349b94 100644 --- a/components/checker/checkercomponent.cpp +++ b/components/checker/checkercomponent.cpp @@ -37,10 +37,6 @@ void CheckerComponent::Start(void) m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this)); m_CheckTimer->Start(); - /* TODO: figure out a way to register check types */ - PluginCheckTask::Register(); - NullCheckTask::Register(); - m_ResultTimer = boost::make_shared(); m_ResultTimer->SetInterval(5); m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this)); diff --git a/lib/base/dynamictype.h b/lib/base/dynamictype.h index 7b47c75dc..818223b7b 100644 --- a/lib/base/dynamictype.h +++ b/lib/base/dynamictype.h @@ -100,7 +100,7 @@ shared_ptr DynamicObjectFactory(const Dictionary::Ptr& serializedUpdate) } #define REGISTER_TYPE_ALIAS(type, alias, attributeDesc) \ - static RegisterTypeHelper g_Register ## type(alias, DynamicObjectFactory, attributeDesc, (attributeDesc == NULL) ? 0 : sizeof(attributeDesc) / sizeof((static_cast(attributeDesc))[0])) + static RegisterTypeHelper g_RegisterDT_ ## type(alias, DynamicObjectFactory, attributeDesc, (attributeDesc == NULL) ? 0 : sizeof(attributeDesc) / sizeof((static_cast(attributeDesc))[0])) #define REGISTER_TYPE(type, attributeDesc) \ REGISTER_TYPE_ALIAS(type, #type, attributeDesc) diff --git a/lib/base/scriptfunction.h b/lib/base/scriptfunction.h index 86a459415..107521143 100644 --- a/lib/base/scriptfunction.h +++ b/lib/base/scriptfunction.h @@ -52,7 +52,26 @@ private: static map m_Functions; }; +/** + * Helper class for registering ScriptFunction implementation classes. + * + * @ingroup base + */ +class RegisterFunctionHelper +{ +public: + RegisterFunctionHelper(const String& name, const ScriptFunction::Callback& function) + { + if (!ScriptFunction::GetByName(name)) { + ScriptFunction::Ptr func = boost::make_shared(function); + ScriptFunction::Register(name, func); + } + } +}; + +#define REGISTER_SCRIPTFUNCTION(name, callback) \ + static RegisterFunctionHelper g_RegisterSF_ ## type(name, callback) + } #endif /* SCRIPTFUNCTION_H */ - diff --git a/lib/icinga/nullchecktask.cpp b/lib/icinga/nullchecktask.cpp index 3df02340e..9d6e92c2a 100644 --- a/lib/icinga/nullchecktask.cpp +++ b/lib/icinga/nullchecktask.cpp @@ -21,6 +21,8 @@ using namespace icinga; +REGISTER_SCRIPTFUNCTION("native::NullCheck", &NullCheckTask::ScriptFunc); + void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector& arguments) { if (arguments.size() < 1) @@ -37,9 +39,3 @@ void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector& task->FinishResult(cr); } - -void NullCheckTask::Register(void) -{ - ScriptFunction::Ptr func = boost::make_shared(&NullCheckTask::ScriptFunc); - ScriptFunction::Register("native::NullCheck", func); -} diff --git a/lib/icinga/nullchecktask.h b/lib/icinga/nullchecktask.h index 13b268180..a38abaf0f 100644 --- a/lib/icinga/nullchecktask.h +++ b/lib/icinga/nullchecktask.h @@ -31,9 +31,6 @@ namespace icinga class I2_ICINGA_API NullCheckTask { public: - static void Register(void); - -private: static void ScriptFunc(const ScriptTask::Ptr& task, const vector& arguments); }; diff --git a/lib/icinga/pluginchecktask.cpp b/lib/icinga/pluginchecktask.cpp index b699c4c1d..e2ca0d1bb 100644 --- a/lib/icinga/pluginchecktask.cpp +++ b/lib/icinga/pluginchecktask.cpp @@ -21,6 +21,8 @@ using namespace icinga; +REGISTER_SCRIPTFUNCTION("native::PluginCheck", &PluginCheckTask::ScriptFunc); + PluginCheckTask::PluginCheckTask(const ScriptTask::Ptr& task, const Process::Ptr& process) : m_Task(task), m_Process(process) { } @@ -124,9 +126,3 @@ void PluginCheckTask::ProcessCheckOutput(const Dictionary::Ptr& result, String& result->Set("output", text); result->Set("performance_data_raw", perfdata); } - -void PluginCheckTask::Register(void) -{ - ScriptFunction::Ptr func = boost::make_shared(&PluginCheckTask::ScriptFunc); - ScriptFunction::Register("native::PluginCheck", func); -} diff --git a/lib/icinga/pluginchecktask.h b/lib/icinga/pluginchecktask.h index d8dd8b3bf..b5b46e028 100644 --- a/lib/icinga/pluginchecktask.h +++ b/lib/icinga/pluginchecktask.h @@ -31,10 +31,9 @@ namespace icinga class I2_ICINGA_API PluginCheckTask { public: - static void Register(void); + static void ScriptFunc(const ScriptTask::Ptr& task, const vector& arguments); private: - static void ScriptFunc(const ScriptTask::Ptr& task, const vector& arguments); static void ProcessFinishedHandler(PluginCheckTask ct); static void ProcessCheckOutput(const Dictionary::Ptr& result, String& output);