aboutsummaryrefslogtreecommitdiff
path: root/odb/qt/containers/mutable-list-iterator.hxx
blob: 46085178eda65195d3a1f6973f75560ed5ca42e1 (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
// file      : odb/qt/containers/mutable-list-iterator.hxx
// copyright : Copyright (c) 2005-2013 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_QT_CONTAINERS_MUTABLE_LIST_ITERATOR_HXX
#define ODB_QT_CONTAINERS_MUTABLE_LIST_ITERATOR_HXX

#include <odb/pre.hxx>

#include <QtCore/QMutableListIterator>

#include <odb/qt/list.hxx>

// Java-style QMutableListIterator-like iterator. The implementation
// is based on what's found in qiterator.h.
//
template <typename T>
class QMutableOdbListIterator
{
public:
  QMutableOdbListIterator(QOdbList<T> &x)
      : c (&x)
  {
    c->setSharable(false);
    i = c->begin();
    n = c->end();
  }

  ~QMutableOdbListIterator() {c->setSharable(true);}

  QMutableOdbListIterator &operator=(QOdbList<T> &x)
  {
    c->setSharable(true);
    c = &x;
    c->setSharable(false);
    i = c->begin();
    n = c->end();
    return *this;
  }

  void toFront() {i = c->begin(); n = c->end();}
  void toBack() {i = c->end(); n = i;}
  bool hasNext() const {return c->constEnd() != const_iterator(i.base ());}
  bool hasPrevious() const
  {
    return c->constBegin() != const_iterator(i.base ());
  }

  bool findNext(const T &t)
  {
    while (c->constEnd() != const_iterator((n = i).base ()))
      if (*i++ == t)
        return true;
    return false;
  }

  bool findPrevious(const T &t)
  {
    while (c->constBegin() != const_iterator(i.base ()))
      if (*(n = --i) == t)
        return true;

    n = c->end();
    return false;
  }

  T &next() {n = i++; return n.modify ();}
  T &peekNext() const {return i.modify ();}
  T &previous() {n = --i; return n.modify ();}
  T &peekPrevious() const {iterator p (i); return (--p).modify ();}

  void remove()
  {
    if (c->constEnd() != const_iterator(n.base ()))
    {
      i = c->erase (n);
      n = c->end();
    }
  }

  void setValue(const T &t) const
  {
    if (c->constEnd() != const_iterator(n.base ()))
      n.modify () = t;
  }

  T &value()
  {
    Q_ASSERT(c->constEnd() != const_iterator(n.base ()));
    return n.modify ();
  }

  const T &value() const
  {
    Q_ASSERT(c->constEnd() != const_iterator(n.base ()));
    return *n;
  }

  void insert(const T &t) {n = i = c->insert(i, t); ++i;}

private:
  typedef typename QOdbList<T>::iterator iterator;
  typedef typename QOdbList<T>::const_iterator const_iterator;

  QOdbList<T>* c;
  iterator i, n;
};

#include <odb/post.hxx>

#endif // ODB_QT_CONTAINERS_MUTABLE_LIST_ITERATOR_HXX