summaryrefslogtreecommitdiff
path: root/include/valueiterator.h
blob: 11c412eadd188e1d91959da078016ab781753dfd (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
/**
 *  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
 */
}