summaryrefslogtreecommitdiff
path: root/include/valueiterator.h
blob: 037f06145c1c0ce7a9932e82eaf229cabbadbac0 (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
/**
 *  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);
    
    /**
     *  Copy constructor
     *  @param  that
     */
    ValueIterator(const ValueIterator &that);
    
    /**
     *  Destructor
     */
    virtual ~ValueIterator() {}

    /**
     *  Increment position (pre-increment)
     *  @return ValueIterator
     */
    ValueIterator &operator++();
    
    /**
     *  Increment position (post-increment)
     *  @return ValueIterator
     */
    ValueIterator operator++(int)
    {
        // make a copy
        ValueIterator copy(*this);
        
        // increment current object
        ++(*this);
        
        // and return the unchanged original
        return copy;
    }
    
    /**
     *  Decrement position (pre-decrement)
     *  @return ValueIterator
     */
    ValueIterator &operator--();

    /**
     *  Increment position (post-decrement)
     *  @return ValueIterator
     */
    ValueIterator operator--(int)
    {
        // make a copy
        ValueIterator copy(*this);
        
        // decrement current object
        --(*this);
        
        // and return the unchanged original
        return copy;
    }
    
    /**
     *  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
 */
}