Code

Fixed const/non-const mismatch loop.
[inkscape.git] / src / util / forward-pointer-iterator.h
1 /*
2  * Inkscape::Util::ForwardPointerIterator - wraps a simple pointer
3  *                                          with various strategies
4  *                                          to determine sequence
5  *
6  * Authors:
7  *   MenTaLguY <mental@rydia.net>
8  *
9  * Copyright (C) 2004 MenTaLguY
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifndef SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
15 #define SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
17 #include <iterator>
18 #include "util/reference.h"
20 namespace Inkscape {
22 namespace Util {
24 template <typename BaseType, typename Strategy>
25 class ForwardPointerIterator;
27 template <typename BaseType, typename Strategy>
28 class ForwardPointerIterator<BaseType const, Strategy> {
29 public:
30     typedef std::forward_iterator_tag iterator_category;
31     typedef typename Traits::Reference<BaseType const>::LValue value_type;
32     typedef std::ptrdiff_t difference_type;
33     typedef typename Traits::Reference<BaseType const>::LValue reference;
34     typedef typename Traits::Reference<BaseType const>::RValue const_reference;
35     typedef typename Traits::Reference<BaseType const>::Pointer pointer;
37     typedef ForwardPointerIterator<BaseType const, Strategy> Self;
39     ForwardPointerIterator() : _p(NULL) {}
40     ForwardPointerIterator(pointer p) : _p(p) {}
42     operator pointer() const { return _p; }
43     reference operator*() const { return *_p; }
44     pointer operator->() const { return _p; }
46     bool operator==(Self const &other) const {
47         return _p == other._p;
48     }
49     bool operator!=(Self const &other) const {
50         return _p != other._p;
51     }
53     Self &operator++() {
54         _p = Strategy::next(_p);
55         return *this;
56     }
57     Self operator++(int) {
58         Self old(*this);
59         operator++();
60         return old;
61     }
63     operator bool() const { return _p != NULL; }
65 private:
66     pointer _p;
67 };
69 template <typename BaseType, typename Strategy>
70 class ForwardPointerIterator
71 : public ForwardPointerIterator<BaseType const, Strategy>
72 {
73 public:
74     typedef typename Traits::Reference<BaseType>::LValue value_type;
75     typedef typename Traits::Reference<BaseType>::LValue reference;
76     typedef typename Traits::Reference<BaseType>::RValue const_reference;
77     typedef typename Traits::Reference<BaseType>::Pointer pointer;
79     typedef ForwardPointerIterator<BaseType const, Strategy> Ancestor;
80     typedef ForwardPointerIterator<BaseType, Strategy> Self;
82     ForwardPointerIterator() : Ancestor() {}
83     ForwardPointerIterator(pointer p) : Ancestor(p) {}
85     operator pointer() const {
86         return const_cast<pointer>(Ancestor::operator->());
87     }
88     reference operator*() const {
89         return const_cast<reference>(Ancestor::operator*());
90     }
91     pointer operator->() const {
92         return const_cast<pointer>(Ancestor::operator->());
93     }
95     Self &operator++() {
96         Ancestor::operator++();
97         return *this;
98     }
99     Self operator++(int) {
100         Self old(*this);
101         operator++();
102         return old;
103     }
104 };
110 #endif
111 /*
112   Local Variables:
113   mode:c++
114   c-file-style:"stroustrup"
115   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
116   indent-tabs-mode:nil
117   fill-column:99
118   End:
119 */
120 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :