From: Gunnar Beutner Date: Mon, 15 May 2017 13:54:48 +0000 (+0200) Subject: Implement the Dictionary#values prototype function X-Git-Tag: v2.7.0~66^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1b77f4b3367e17a7528123c1e306a0c1947454fe;p=icinga2 Implement the Dictionary#values prototype function --- diff --git a/doc/18-library-reference.md b/doc/18-library-reference.md index 0a3f91225..9d8cbc0ca 100644 --- a/doc/18-library-reference.md +++ b/doc/18-library-reference.md @@ -1220,6 +1220,14 @@ Signature: Returns a list of keys for all items that are currently in the dictionary. +### Dictionary#values + +Signature: + + function values(); + +Returns a list of values for all items that are currently in the dictionary. + ## Function type Inherits methods from the [Object type](18-library-reference.md#object-type). diff --git a/lib/base/dictionary-script.cpp b/lib/base/dictionary-script.cpp index 908b719b2..66aa26cbd 100644 --- a/lib/base/dictionary-script.cpp +++ b/lib/base/dictionary-script.cpp @@ -79,6 +79,18 @@ static Array::Ptr DictionaryKeys(void) return keys; } +static Array::Ptr DictionaryValues(void) +{ + ScriptFrame *vframe = ScriptFrame::GetCurrentFrame(); + Dictionary::Ptr self = static_cast(vframe->Self); + Array::Ptr keys = new Array(); + ObjectLock olock(self); + for (const Dictionary::Pair& kv : self) { + keys->Add(kv.second); + } + return keys; +} + Object::Ptr Dictionary::GetPrototype(void) { static Dictionary::Ptr prototype; @@ -92,6 +104,7 @@ Object::Ptr Dictionary::GetPrototype(void) prototype->Set("contains", new Function("Dictionary#contains", WrapFunction(DictionaryContains), { "key" }, true)); prototype->Set("shallow_clone", new Function("Dictionary#shallow_clone", WrapFunction(DictionaryShallowClone), {}, true)); prototype->Set("keys", new Function("Dictionary#keys", WrapFunction(DictionaryKeys), {}, true)); + prototype->Set("values", new Function("Dictionary#values", WrapFunction(DictionaryValues), {}, true)); } return prototype;