summaryrefslogtreecommitdiff
path: root/zend/hashiterator.h
blob: 357ffa685818200809f2df5efbf743ac499bd5da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 *  HashIterator.h
 *
 *  This is an internal helper class that is used when iterating over a
 *  Php::Value object that holds a hash table (an array or an object that
 *  does not implement the Traversable interface - 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
 */

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Class definition
 */
class HashIterator : public ValueIteratorImpl
{
public:
    /**
     *  Constructor
     *  @param  hashtable       The hashtable to iterate over
     *  @param  first           Should it start on the first position?
     *  @param  tsrm_ls
     */
    HashIterator(HashTable *hashtable, bool first, bool is_array = false) : _table(hashtable), _is_array(is_array)
    {
        // reset the hash pointer to the internal position
        if (hashtable && first)
        {
            // we should be valid (this is undone later if necessary)
            _valid = true;

            // move to first position
            zend_hash_internal_pointer_reset_ex(_table, &_position);

            // read current data
            if (read()) return;

            // data was private, move on
            increment();
        }
        else
        {
            // start with invalid data
            invalidate();
        }
    }

    /**
     *  Copy constructor
     *  @param  that
     *  @param  tsrm_ls
     */
    HashIterator(const HashIterator &that TSRMLS_DC) :
        _table(that._table), _position(that._position), _is_array(that._is_array)
    {
        // read current position
        read();
    }

    /**
     *  Destructor
     */
    virtual ~HashIterator() = default;

    /**
     *  Clone the object
     *  @param  tsrm_ls
     *  @return ValueIteratorImpl
     */
    virtual ValueIteratorImpl *clone()
    {
        // create a new instance
        return new HashIterator(*this);
    }

    /**
     *  Increment position (pre-increment)
     *  @return bool
     */
    virtual bool increment() override
    {
        // leap out if we're not even iterating over a hash table
        if (!_table) return false;

        // leap out if already on an invalid pos (behind the last pos)
        if (!_valid) return false;

        // move the iterator forward
        if (zend_hash_move_forward_ex(_table, &_position) == SUCCESS)
        {
            // read current key and value
            if (read()) return true;

            // data was private or invalid, move further
            return increment();
        }
        else
        {
            // invalidate current position
            return invalidate();
        }
    }

    /**
     *  Decrement position (pre-decrement)
     *  @return bool
     */
    virtual bool decrement() override
    {
        // leap out if we're not even iterating over a hash table
        if (!_table) return false;

        // if position is invalid, it is one position behind the last position
        if (!_valid)
        {
            // move to last position
            zend_hash_internal_pointer_end_ex(_table, &_position);
        }
        else if (zend_hash_move_backwards_ex(_table, &_position) == FAILURE)
        {
            // invalidate current position
            return invalidate();
        }

        // read current key and value
        if (read()) return true;

        // data was private, move on
        return decrement();
    }

    /**
     *  Compare with other iterator
     *  @param  that
     *  @return bool
     */
    virtual bool equals(const ValueIteratorImpl *that) const override
    {
        // this always is a hash iterator
        HashIterator *other = (HashIterator *)that;

        // compare the tables and positions
        return _table == other->_table && _position == other->_position;
    }

    /**
     *  Derefecence, this returns a std::pair with the current key and value
     *  @return std::pair
     */
    virtual const std::pair<Value,Value> &current() const override
    {
        return _current;
    }

private:
    /**
     *  Are we at a possibly valid position?
     *  @var    bool
     */
    bool _valid = false;

    /**
     *  The hash table over which is being iterated
     *  @var    HashTable
     */
    HashTable *_table = nullptr;

    /**
     *  The position in the hash table
     *  @var    HashPosition
     */
    HashPosition _position;

    /**
     * Is a hash interator in array
     * @var     bool
     */
     bool _is_array = false;

    /**
     *  The current key and value
     *  @var    std::pair
     */
    std::pair<Value,Value> _current;

    /**
     *  Read current key and value
     *  @return bool
     */
    bool read()
    {
        // zval to read the current key in
        Value key;

        // read in the current key
        zend_hash_get_current_key_zval_ex(_table, key._val, &_position);

        // if the key is set to NULL, it means that the object is not at a valid position
        if (key.isNull()) return invalidate();

        // iterator is at a valid position, go fetch the data
        auto *value = zend_hash_get_current_data_ex(_table, &_position);

        // we can now update the current data
        _current = std::make_pair<Value,Value>(std::move(key), value);

        // if the key is private (it starts with a null character) we should return
        // false to report that the object is not in a completely valid state
        return _is_array || !_current.first.isString() || _current.first.rawValue()[0];
    }

    /**
     *  Invalidate the iterator
     *  @return bool
     */
    bool invalidate()
    {
        // no longer valid
        _valid = false;

        // make the data a pair of null ptrs
        _current = std::make_pair<Value,Value>(nullptr,nullptr);

        // done
        return false;
    }
};

/**
 *  End namespace
 */
}