]> granicus.if.org Git - icinga2/commitdiff
Implement the Dictionary#keys method
authorGunnar Beutner <gunnar@beutner.name>
Tue, 11 Aug 2015 11:56:42 +0000 (13:56 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 11 Aug 2015 11:58:29 +0000 (13:58 +0200)
fixes #9882

doc/20-library-reference.md
lib/base/dictionary-script.cpp

index b3ed5b8ded3a485facdc751e04b98e541d068e4b..22e69504051f28f977465baaded0d430915a9aa6 100644 (file)
@@ -633,6 +633,14 @@ Signature:
 Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist
 in the dictionary.
 
+### <a id="dictionary-keys"></a> Dictionary#keys
+
+Signature:
+
+    function keys();
+
+Returns a list of keys for all items that are currently in the dictionary.
+
 ## <a id="scriptfunction-type"></a> Function type
 
 ### <a id="scriptfunction-call"></a> Function#call
index 4e2c9c31082f169a251129f048e532936d991102..8636783a1c3c9e3aa40416e50d5dc1805b8341c0 100644 (file)
@@ -21,6 +21,8 @@
 #include "base/function.hpp"
 #include "base/functionwrapper.hpp"
 #include "base/scriptframe.hpp"
+#include "base/array.hpp"
+#include <boost/foreach.hpp>
 
 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<Dictionary::Ptr>(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;