summaryrefslogtreecommitdiffstats
path: root/lib/remote/apifunction.hpp
blob: e6113204d727a0c543258007e5de9bb8ef00d5d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#ifndef APIFUNCTION_H
#define APIFUNCTION_H

#include "remote/i2-remote.hpp"
#include "remote/messageorigin.hpp"
#include "base/registry.hpp"
#include "base/value.hpp"
#include "base/dictionary.hpp"
#include <vector>

namespace icinga
{

/**
 * A function available over the internal cluster API.
 *
 * @ingroup base
 */
class ApiFunction final : public Object
{
public:
	DECLARE_PTR_TYPEDEFS(ApiFunction);

	typedef std::function<Value(const MessageOrigin::Ptr& origin, const Dictionary::Ptr&)> Callback;

	ApiFunction(Callback function);

	Value Invoke(const MessageOrigin::Ptr& 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;
};

/**
 * A registry for API functions.
 *
 * @ingroup base
 */
class ApiFunctionRegistry : public Registry<ApiFunctionRegistry, ApiFunction::Ptr>
{
public:
	static ApiFunctionRegistry *GetInstance();
};

#define REGISTER_APIFUNCTION(name, ns, callback) \
	INITIALIZE_ONCE([]() { \
		ApiFunction::Ptr func = new ApiFunction(callback); \
		ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \
	})

}

#endif /* APIFUNCTION_H */