summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-16 14:55:51 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2014-03-16 14:55:51 +0100
commitec6d84016bec41b05e275641f990831572171969 (patch)
treeff76497cf1a000d96892dd9b2ed2ab6144a3bf89 /include
parenta83e9b3af70f35400ec5d62a4b400d55b05c3492 (diff)
added Value::begin() and Value::end() methods to make it possible to iterate over a value
Diffstat (limited to 'include')
-rw-r--r--include/value.h21
-rw-r--r--include/valueiterator.h129
2 files changed, 150 insertions, 0 deletions
diff --git a/include/value.h b/include/value.h
index 8fea5a6..94cc1cf 100644
--- a/include/value.h
+++ b/include/value.h
@@ -32,6 +32,7 @@ namespace Php {
* Forward definitions
*/
class Base;
+class ValueIterator;
template <class Type> class HashMember;
/**
@@ -498,6 +499,25 @@ public:
}
/**
+ * Define the iterator type
+ */
+ typedef ValueIterator iterator;
+
+ /**
+ * Return an iterator for iterating over the values
+ * This is only meaningful for Value objects that hold an array or an object
+ * @return iterator
+ */
+ iterator begin() const;
+
+ /**
+ * Return an iterator for iterating over the values
+ * This is only meaningful for Value objects that hold an array or an object
+ * @return iterator
+ */
+ iterator end() const;
+
+ /**
* The number of members in case of an array or object
* @return int
*/
@@ -926,6 +946,7 @@ protected:
friend class ClassBase;
friend class Iterator;
friend class Extension;
+ friend class ValueIterator;
};
/**
diff --git a/include/valueiterator.h b/include/valueiterator.h
new file mode 100644
index 0000000..11c412e
--- /dev/null
+++ b/include/valueiterator.h
@@ -0,0 +1,129 @@
+/**
+ * ValueIterator.h
+ *
+ * This is an internal helper class that is used when iterating over a
+ * Php::Value object - stl style.
+ *
+ * Thus, when you do c++ things like "for (auto &iter : value)", internally
+ * a ValueIterator object is being used.
+ *
+ * @author Emiel Bruijntjes
+ * @copyright 2014 Copernica BV
+ */
+
+/**
+ * Forward declaration
+ */
+struct _hashtable;
+struct bucket;
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class ValueIterator
+{
+public:
+ /**
+ * Constructor
+ * @param hashtable The hashtable to iterate over
+ * @param first Should it start on the first position?
+ */
+ ValueIterator(struct _hashtable *hashtable, bool first);
+
+ /**
+ * Destructor
+ */
+ virtual ~ValueIterator() {}
+
+ /**
+ * Increment position
+ * @return ValueIterator
+ */
+ ValueIterator &operator++();
+
+ /**
+ * Decrement position
+ * @return ValueIterator
+ */
+ ValueIterator &operator--();
+
+ /**
+ * Compare with other iterator
+ * @param that
+ * @return bool
+ */
+ bool operator==(const ValueIterator &that) const
+ {
+ return _position == that._position;
+ }
+
+ /**
+ * Compare with other iterator
+ * @param that
+ * @return bool
+ */
+ bool operator!=(const ValueIterator &that) const
+ {
+ return _position != that._position;
+ }
+
+ /**
+ * Derefecence, this returns a std::pair with the current key and value
+ * @return std::pair
+ */
+ const std::pair<Value,Value> &operator*() const
+ {
+ return _current;
+ }
+
+ /**
+ * Dereference, this returns a std::pair with the current key and value
+ * @return std::pair
+ */
+ const std::pair<Value,Value> *operator->() const
+ {
+ return &_current;
+ }
+
+private:
+ /**
+ * The hash table over which is being iterated
+ * @var HashTable
+ */
+ struct _hashtable *_table = nullptr;
+
+ /**
+ * The position in the hash table
+ * @var HashPosition
+ */
+ struct bucket *_position = nullptr;
+
+ /**
+ * The current key and value
+ * @var std::pair
+ */
+ std::pair<Value,Value> _current;
+
+ /**
+ * Read current key and value
+ * @return ValueIterator
+ */
+ ValueIterator &read();
+
+ /**
+ * Invalidate the iterator
+ * @return ValueIterator
+ */
+ ValueIterator &invalidate();
+};
+
+/**
+ * End namespace
+ */
+}
+