From d0ee242b3a617fa8676e56388ce01a8a676bf7d1 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Fri, 21 Nov 2014 11:23:05 +0100 Subject: fixed issue 107, casting a value to a std::map was not working correctly --- include/value.h | 25 ++++++++++++++++++------- zend/value.cpp | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/include/value.h b/include/value.h index 85935ec..ea56487 100644 --- a/include/value.h +++ b/include/value.h @@ -526,14 +526,19 @@ public: // must be an array or an object, otherwise the map is empty if (!isArray() && !isObject()) return std::map(); - // get the original map value - std::map map(mapValue()); - // result variable std::map result; - - // loop through the original map, and copy everything to the result - for (auto &iter : map) result[iter.first] = iter.second; + + // iterate over the values + iterate([&result](const Value &key, const Value &value) { + + // first convert the value to the appropriate type (otherwise + // compiler errors occur) + T val = value; + + // add the value to the array + result[key] = val; + }); // done return result; @@ -674,7 +679,7 @@ public: { return stringValue(); } - + /** * Cast to byte array * @return const char * @@ -1039,6 +1044,12 @@ public: private: + /** + * Iterate over key value pairs + * @param callback + */ + void iterate(const std::function &callback) const; + /** * Call function with a number of parameters * @param argc Number of parameters diff --git a/zend/value.cpp b/zend/value.cpp index dfea847..544926a 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -1738,7 +1738,7 @@ ValueIterator Value::createIterator(bool begin) const // check type if (isArray()) return ValueIterator(new HashIterator(Z_ARRVAL_P(_val), begin, true)); - // get access to the hast table + // get access to the hash table if (isObject()) { // we need the TSRMLS_CC variable @@ -1785,6 +1785,20 @@ ValueIterator Value::end() const return createIterator(false); } +/** + * Iterate over key value pairs + * @param callback + */ +void Value::iterate(const std::function &callback) const +{ + // iterate over the object + for (const auto &iter : *this) + { + // call the callback + callback(iter.first, iter.second); + } +} + /** * Does the array contain a certain index? * @param index -- cgit v1.2.3