icinga2/lib/remote/apifunction.hpp

82 lines
2.8 KiB
C++
Raw Normal View History

2013-09-17 13:18:26 +02:00
/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
2013-09-17 13:18:26 +02:00
* *
* 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. *
******************************************************************************/
#ifndef APIFUNCTION_H
#define APIFUNCTION_H
2013-09-17 13:18:26 +02:00
2014-05-25 16:23:35 +02:00
#include "remote/i2-remote.hpp"
#include "remote/messageorigin.hpp"
#include "base/registry.hpp"
#include "base/value.hpp"
#include "base/dictionary.hpp"
#include <vector>
#include <boost/function.hpp>
2013-09-17 13:18:26 +02:00
namespace icinga
{
/**
* An API function.
2013-09-17 13:18:26 +02:00
*
* @ingroup base
2013-09-17 13:18:26 +02:00
*/
class I2_REMOTE_API ApiFunction : public Object
2013-09-17 13:18:26 +02:00
{
public:
2014-11-07 12:32:25 +01:00
DECLARE_PTR_TYPEDEFS(ApiFunction);
2013-09-17 13:18:26 +02:00
typedef boost::function<Value(const MessageOrigin& origin, const Dictionary::Ptr&)> Callback;
ApiFunction(const Callback& function);
Value Invoke(const MessageOrigin& origin, const Dictionary::Ptr& arguments);
static ApiFunction::Ptr GetByName(const String& name);
static void Register(const String& name, const ApiFunction::Ptr& function);
static void Unregister(const String& name);
private:
Callback m_Callback;
2013-09-17 13:18:26 +02:00
};
/**
* A registry for API functions.
*
* @ingroup base
*/
class I2_REMOTE_API ApiFunctionRegistry : public Registry<ApiFunctionRegistry, ApiFunction::Ptr>
{
public:
static ApiFunctionRegistry *GetInstance(void);
};
#define REGISTER_APIFUNCTION(name, ns, callback) \
2014-11-11 13:22:16 +01:00
namespace { namespace UNIQUE_NAME(apif) { namespace apif ## name { \
void RegisterFunction(void) \
{ \
ApiFunction::Ptr func = new ApiFunction(callback); \
ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \
} \
INITIALIZE_ONCE(RegisterFunction); \
} } }
2013-09-17 13:18:26 +02:00
}
#endif /* APIFUNCTION_H */