summaryrefslogtreecommitdiffstats
path: root/src/util/forward-pointer-iterator.h
blob: 9fe3bb852ef1088516d01f88018febeabe7e1f6b (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Inkscape::Util::ForwardPointerIterator - wraps a simple pointer
 *                                          with various strategies
 *                                          to determine sequence
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2004 MenTaLguY
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifndef SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H
#define SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H

#include <iterator>
#include <cstddef>
#include "util/reference.h"

namespace Inkscape {

namespace Util {

template <typename BaseType, typename Strategy>
class ForwardPointerIterator;

template <typename BaseType, typename Strategy>
class ForwardPointerIterator<BaseType const, Strategy> {
public:
    typedef std::forward_iterator_tag iterator_category;
    typedef typename Traits::Reference<BaseType const>::LValue value_type;
    typedef std::ptrdiff_t difference_type;
    typedef typename Traits::Reference<BaseType const>::LValue reference;
    typedef typename Traits::Reference<BaseType const>::RValue const_reference;
    typedef typename Traits::Reference<BaseType const>::Pointer pointer;

    typedef ForwardPointerIterator<BaseType const, Strategy> Self;

    ForwardPointerIterator() = default;
    ForwardPointerIterator(pointer p) : _p(p) {}

    operator pointer() const { return _p; }
    reference operator*() const { return *_p; }
    pointer operator->() const { return _p; }

    bool operator==(Self const &other) const {
        return _p == other._p;
    }
    bool operator!=(Self const &other) const {
        return _p != other._p;
    }

    Self &operator++() {
        _p = Strategy::next(_p);
        return *this;
    }
    Self operator++(int) {
        Self old(*this);
        operator++();
        return old;
    }

    operator bool() const { return _p != nullptr; }

private:
    pointer _p;
};

template <typename BaseType, typename Strategy>
class ForwardPointerIterator
: public ForwardPointerIterator<BaseType const, Strategy>
{
public:
    typedef typename Traits::Reference<BaseType>::LValue value_type;
    typedef typename Traits::Reference<BaseType>::LValue reference;
    typedef typename Traits::Reference<BaseType>::RValue const_reference;
    typedef typename Traits::Reference<BaseType>::Pointer pointer;

    typedef ForwardPointerIterator<BaseType const, Strategy> Ancestor;
    typedef ForwardPointerIterator<BaseType, Strategy> Self;

    ForwardPointerIterator() : Ancestor() {}
    ForwardPointerIterator(pointer p) : Ancestor(p) {}

    operator pointer() const {
        return const_cast<pointer>(Ancestor::operator->());
    }
    reference operator*() const {
        return const_cast<reference>(Ancestor::operator*());
    }
    pointer operator->() const {
        return const_cast<pointer>(Ancestor::operator->());
    }

    Self &operator++() {
        Ancestor::operator++();
        return *this;
    }
    Self operator++(int) {
        Self old(*this);
        operator++();
        return old;
    }
};

}

}

#endif
/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :