From d30d1d4dda6a741f61f68ce542f0773ad4cb489b Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 11 Aug 2015 13:56:42 +0200 Subject: [PATCH] Implement the Dictionary#keys method fixes #9882 --- doc/20-library-reference.md | 8 ++++++++ lib/base/dictionary-script.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/doc/20-library-reference.md b/doc/20-library-reference.md index b3ed5b8de..22e695040 100644 --- a/doc/20-library-reference.md +++ b/doc/20-library-reference.md @@ -633,6 +633,14 @@ Signature: Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist in the dictionary. +### Dictionary#keys + +Signature: + + function keys(); + +Returns a list of keys for all items that are currently in the dictionary. + ## Function type ### Function#call diff --git a/lib/base/dictionary-script.cpp b/lib/base/dictionary-script.cpp index 4e2c9c310..8636783a1 100644 --- a/lib/base/dictionary-script.cpp +++ b/lib/base/dictionary-script.cpp @@ -21,6 +21,8 @@ #include "base/function.hpp" #include "base/functionwrapper.hpp" #include "base/scriptframe.hpp" +#include "base/array.hpp" +#include using namespace icinga; @@ -66,6 +68,18 @@ static Dictionary::Ptr DictionaryClone(void) return self->ShallowClone(); } +static Array::Ptr DictionaryKeys(void) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + Dictionary::Ptr self = static_cast(vframe->Self); + Array::Ptr keys = new Array(); + ObjectLock olock(self); + BOOST_FOREACH(const Dictionary::Pair& kv, self) { + keys->Add(kv.first); + } + return keys; +} + Object::Ptr Dictionary::GetPrototype(void) { static Dictionary::Ptr prototype; @@ -78,6 +92,7 @@ Object::Ptr Dictionary::GetPrototype(void) prototype->Set("remove", new Function(WrapFunction(DictionaryRemove))); prototype->Set("contains", new Function(WrapFunction(DictionaryContains), true)); prototype->Set("clone", new Function(WrapFunction(DictionaryClone), true)); + prototype->Set("keys", new Function(WrapFunction(DictionaryKeys), true)); } return prototype;