]> granicus.if.org Git - icinga2/blob - lib/base/typetype-script.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / typetype-script.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/type.hpp"
4 #include "base/dictionary.hpp"
5 #include "base/function.hpp"
6 #include "base/functionwrapper.hpp"
7 #include "base/scriptframe.hpp"
8
9 using namespace icinga;
10
11 static void InvokeAttributeHandlerHelper(const Function::Ptr& callback,
12         const Object::Ptr& object, const Value& cookie)
13 {
14         callback->Invoke({ object });
15 }
16
17 static void TypeRegisterAttributeHandler(const String& fieldName, const Function::Ptr& callback)
18 {
19         ScriptFrame *vframe = ScriptFrame::GetCurrentFrame();
20         Type::Ptr self = static_cast<Type::Ptr>(vframe->Self);
21         REQUIRE_NOT_NULL(self);
22
23         int fid = self->GetFieldId(fieldName);
24         self->RegisterAttributeHandler(fid, std::bind(&InvokeAttributeHandlerHelper, callback, _1, _2));
25 }
26
27 Object::Ptr TypeType::GetPrototype()
28 {
29         static Dictionary::Ptr prototype = new Dictionary({
30                 { "register_attribute_handler", new Function("Type#register_attribute_handler", TypeRegisterAttributeHandler, { "field", "callback" }, false) }
31         });
32
33         return prototype;
34 }
35