1 /*
2 * Inkscape::Util::GSListIterator - STL iterator for GSList
3 * Inkscape::Util::GSListConstIterator - STL iterator for GSList
4 * Inkscape::Util::GListIterator - STL iterator for GList
5 * Inkscape::Util::GListConstIterator - STL iterator for GList
6 *
7 * Authors:
8 * MenTaLguY <mental@rydia.net>
9 *
10 * Copyright (C) 2005 MenTaLguY
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifndef SEEN_INKSCAPE_GLIB_LIST_ITERATORS_H
16 #define SEEN_INKSCAPE_GLIB_LIST_ITERATORS_H
18 #include <cstddef>
19 #include <iterator>
20 #include "glib/gslist.h"
21 #include "glib/glist.h"
23 namespace Inkscape {
25 namespace Util {
27 template <typename T> class GSListConstIterator;
28 template <typename T> class GSListIterator;
29 template <typename T> class GListConstIterator;
30 template <typename T> class GListIterator;
32 template <typename T>
33 class GSListConstIterator<T *> {
34 public:
35 typedef std::forward_iterator_tag iterator_category;
36 typedef T * const value_type;
37 typedef std::ptrdiff_t difference_type;
38 typedef value_type *pointer;
39 typedef value_type &reference;
41 GSListConstIterator(GSList const *list) : _list(list) {}
42 // default copy
43 // default assign
44 GSList const *list() const { return _list; }
46 reference operator*() const {
47 return *reinterpret_cast<pointer>(&_list->data);
48 }
50 bool operator==(GSListConstIterator const &other) {
51 return other._list == _list;
52 }
53 bool operator!=(GSListConstIterator const &other) {
54 return other._list != _list;
55 }
57 GSListConstIterator &operator++() {
58 _list = _list->next;
59 return *this;
60 }
61 GSListConstIterator operator++(int) {
62 GSListConstIterator saved=*this;
63 _list = _list->next;
64 return saved;
65 }
67 private:
68 GSList const *_list;
69 };
71 template <typename T>
72 class GSListIterator<T *> {
73 public:
74 typedef std::forward_iterator_tag iterator_category;
75 typedef T *value_type;
76 typedef std::ptrdiff_t difference_type;
77 typedef value_type *pointer;
78 typedef value_type &reference;
79 typedef value_type const &const_reference;
81 GSListIterator(GSList *list) : _list(list) {}
82 // default copy
83 // default assign
84 operator GSListConstIterator<T *>() const { return _list; }
85 GSList const *list() const { return _list; }
86 GSList *list() { return _list; }
88 const_reference operator*() const {
89 return *reinterpret_cast<pointer>(&_list->data);
90 }
91 reference operator*() {
92 return *reinterpret_cast<pointer>(&_list->data);
93 }
95 bool operator==(GSListIterator const &other) {
96 return other._list == _list;
97 }
98 bool operator!=(GSListIterator const &other) {
99 return other._list != _list;
100 }
102 GSListIterator &operator++() {
103 _list = _list->next;
104 return *this;
105 }
106 GSListIterator operator++(int) {
107 GSListIterator saved=*this;
108 _list = _list->next;
109 return saved;
110 }
112 private:
113 GSList *_list;
114 };
116 template <typename T>
117 class GListConstIterator<T *> {
118 public:
119 typedef std::bidirectional_iterator_tag iterator_category;
120 typedef T * const value_type;
121 typedef std::ptrdiff_t difference_type;
122 typedef value_type *pointer;
123 typedef value_type &reference;
125 GListConstIterator(GList const *list) : _list(list) {}
126 // default copy
127 // default assign
128 GList const *list() const { return _list; }
130 reference operator*() const {
131 return *reinterpret_cast<pointer>(&_list->data);
132 }
134 bool operator==(GListConstIterator const &other) {
135 return other._list == _list;
136 }
137 bool operator!=(GListConstIterator const &other) {
138 return other._list != _list;
139 }
141 GListConstIterator &operator++() {
142 _list = _list->next;
143 return *this;
144 }
145 GListConstIterator operator++(int) {
146 GListConstIterator saved=*this;
147 _list = _list->next;
148 return saved;
149 }
151 GListConstIterator &operator--() {
152 _list = _list->prev;
153 return *this;
154 }
155 GListConstIterator operator--(int) {
156 GListConstIterator saved=*this;
157 _list = _list->prev;
158 return saved;
159 }
161 private:
162 GList const *_list;
163 };
165 template <typename T>
166 class GListIterator<T *> {
167 public:
168 typedef std::bidirectional_iterator_tag iterator_category;
169 typedef T *value_type;
170 typedef std::ptrdiff_t difference_type;
171 typedef value_type *pointer;
172 typedef value_type &reference;
173 typedef value_type const &const_reference;
175 GListIterator(GList *list) : _list(list) {}
176 // default copy
177 // default assign
178 operator GSListConstIterator<T *>() const {
179 return reinterpret_cast<GSList *>(_list);
180 }
181 operator GSListIterator<T *>() const {
182 return reinterpret_cast<GSList *>(_list);
183 }
184 operator GListConstIterator<T *>() const { return _list; }
185 GList const *list() const { return _list; }
186 GList *list() { return _list; }
188 const_reference operator*() const {
189 return *reinterpret_cast<pointer>(&_list->data);
190 }
191 reference operator*() { return *reinterpret_cast<pointer>(&_list->data); }
193 bool operator==(GListIterator const &other) {
194 return other._list == _list;
195 }
196 bool operator!=(GListIterator const &other) {
197 return other._list != _list;
198 }
200 GListIterator &operator++() {
201 _list = _list->next;
202 return *this;
203 }
204 GListIterator operator++(int) {
205 GListIterator saved=*this;
206 _list = _list->next;
207 return saved;
208 }
210 GListIterator &operator--() {
211 _list = _list->prev;
212 return *this;
213 }
214 GListIterator operator--(int) {
215 GListIterator saved=*this;
216 _list = _list->prev;
217 return saved;
218 }
220 private:
221 GList *_list;
222 };
224 }
226 }
228 #endif
229 /*
230 Local Variables:
231 mode:c++
232 c-file-style:"stroustrup"
233 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
234 indent-tabs-mode:nil
235 fill-column:99
236 End:
237 */
238 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :