diff options
author | Martijn Otto <martijn.otto@copernica.com> | 2014-10-22 15:15:52 +0200 |
---|---|---|
committer | Martijn Otto <martijn.otto@copernica.com> | 2014-10-22 15:15:52 +0200 |
commit | e5b5f1a5d46ead5ed610c23363d8922e0404f715 (patch) | |
tree | cb13f68c38173c09baf5b9693a6e4d8f75a14279 | |
parent | 6b2eaeb89e0f2b0ff158ab318b2ed53c46921bad (diff) |
Allow casting a Php::Value to a std::setv1.2.1
-rw-r--r-- | include/value.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/include/value.h b/include/value.h index 5f387be..85935ec 100644 --- a/include/value.h +++ b/include/value.h @@ -477,6 +477,40 @@ public: } /** + * Convert the object to a set + * + * This only works for regular arrays that are indexed by a number, start + * with position 0 and have no empty spaces. + * + * return std::vector + */ + template <typename T> + std::set<T> setValue() const + { + // only works for arrays, other types return an empty set + if (!isArray()) return std::set<T>(); + + // allocate a result + std::set<T> result; + + // how many elements are we inserting + size_t count = size(); + + // and fill the result set + for (size_t i = 0; i<count; i++) + { + // check if the index exists + if (!contains(i)) continue; + + // get the value and add it to the vector + result.insert(get(i)); + } + + // done + return result; + } + + /** * Convert the object to a map with string index and Php::Value value * @return std::map */ @@ -670,6 +704,16 @@ public: } /** + * Convert the object to a set + * @return std::set + */ + template <typename T> + operator std::set<T>() const + { + return setValue<T>(); + } + + /** * Convert the object to a map with string index and Php::Value value * @return std::map */ |