]> granicus.if.org Git - icinga2/blob - lib/base/function.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / function.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef FUNCTION_H
4 #define FUNCTION_H
5
6 #include "base/i2-base.hpp"
7 #include "base/function-ti.hpp"
8 #include "base/value.hpp"
9 #include "base/functionwrapper.hpp"
10 #include "base/scriptglobal.hpp"
11 #include <vector>
12
13 namespace icinga
14 {
15
16 /**
17  * A script function that can be used to execute a script task.
18  *
19  * @ingroup base
20  */
21 class Function final : public ObjectImpl<Function>
22 {
23 public:
24         DECLARE_OBJECT(Function);
25
26         typedef std::function<Value (const std::vector<Value>& arguments)> Callback;
27
28         template<typename F>
29         Function(const String& name, F function, const std::vector<String>& args = std::vector<String>(),
30                 bool side_effect_free = false, bool deprecated = false)
31                 : Function(name, WrapFunction(function), args, side_effect_free, deprecated)
32         { }
33
34         Value Invoke(const std::vector<Value>& arguments = std::vector<Value>());
35         Value InvokeThis(const Value& otherThis, const std::vector<Value>& arguments = std::vector<Value>());
36
37         bool IsSideEffectFree() const
38         {
39                 return GetSideEffectFree();
40         }
41
42         bool IsDeprecated() const
43         {
44                 return GetDeprecated();
45         }
46
47         static Object::Ptr GetPrototype();
48
49         Object::Ptr Clone() const override;
50
51 private:
52         Callback m_Callback;
53
54         Function(const String& name, Callback function, const std::vector<String>& args,
55                 bool side_effect_free, bool deprecated);
56 };
57
58 /* Ensure that the priority is lower than the basic namespace initialization in scriptframe.cpp. */
59 #define REGISTER_FUNCTION(ns, name, callback, args) \
60         INITIALIZE_ONCE_WITH_PRIORITY([]() { \
61                 Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
62                 Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \
63                 nsp->SetAttribute(#name, std::make_shared<ConstEmbeddedNamespaceValue>(sf)); \
64         }, 10)
65
66 #define REGISTER_SAFE_FUNCTION(ns, name, callback, args) \
67         INITIALIZE_ONCE_WITH_PRIORITY([]() { \
68                 Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
69                 Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \
70                 nsp->SetAttribute(#name, std::make_shared<ConstEmbeddedNamespaceValue>(sf)); \
71         }, 10)
72
73 #define REGISTER_FUNCTION_NONCONST(ns, name, callback, args) \
74         INITIALIZE_ONCE_WITH_PRIORITY([]() { \
75                 Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), false); \
76                 Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \
77                 nsp->SetAttribute(#name, std::make_shared<EmbeddedNamespaceValue>(sf)); \
78         }, 10)
79
80 #define REGISTER_SAFE_FUNCTION_NONCONST(ns, name, callback, args) \
81         INITIALIZE_ONCE_WITH_PRIORITY([]() { \
82                 Function::Ptr sf = new icinga::Function(#ns "#" #name, callback, String(args).Split(":"), true); \
83                 Namespace::Ptr nsp = ScriptGlobal::Get(#ns); \
84                 nsp->SetAttribute(#name, std::make_shared<EmbeddedNamespaceValue>(sf)); \
85         }, 10)
86
87 }
88
89 #endif /* FUNCTION_H */