1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
17 * A script function that can be used to execute a script task.
21 class Function final : public ObjectImpl<Function>
24 DECLARE_OBJECT(Function);
26 typedef std::function<Value (const std::vector<Value>& arguments)> Callback;
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)
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>());
37 bool IsSideEffectFree() const
39 return GetSideEffectFree();
42 bool IsDeprecated() const
44 return GetDeprecated();
47 static Object::Ptr GetPrototype();
49 Object::Ptr Clone() const override;
54 Function(const String& name, Callback function, const std::vector<String>& args,
55 bool side_effect_free, bool deprecated);
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)); \
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)); \
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)); \
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)); \
89 #endif /* FUNCTION_H */