aboutsummaryrefslogtreecommitdiff
path: root/odb/qt/containers/list.hxx
blob: 5dd85ecdc857d4025563119ee896a9ffd4abc073 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// file      : odb/qt/containers/list.hxx
// copyright : Copyright (c) 2005-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_QT_CONTAINERS_LIST_HXX
#define ODB_QT_CONTAINERS_LIST_HXX

#include <odb/pre.hxx>
#include <odb/details/config.hxx> // ODB_CXX11

#include <QtCore/QtGlobal> // QT_VERSION
#include <QtCore/QList>

#ifdef ODB_CXX11
#  include <utility>          // std::move
#  if defined(ODB_CXX11_INITIALIZER_LIST) && \
  defined(Q_COMPILER_INITIALIZER_LISTS)
#    include <initializer_list>
#  endif
#endif

#include <odb/vector-impl.hxx>

// A QList-like container that keeps track of changes.
//
// Note that the style and order of definitions is (mostly) as
// appears in the qlist.h Qt header (except for some cleanups,
// such as superfluous inline use).
//
template <typename L>
class QOdbListIteratorImpl;

template <typename T>
class QOdbList: public odb::vector_base
{
public:
  typedef QList<T> base_list_type;
  typedef typename base_list_type::iterator base_iterator_type;

  QOdbList() {}
  QOdbList(const QOdbList &x): vector_base (x), l_ (x.l_) {}
  // ~QOdbList();
  QOdbList &operator=(const QOdbList &l);

#if QT_VERSION >= 0x040800
  void swap(QOdbList &other);
#endif

#ifdef ODB_CXX11
  QOdbList(QOdbList &&x): vector_base (std::move (x)), l_ (std::move (x.l_)) {}
  QOdbList &operator=(QOdbList &&other);
#if defined(ODB_CXX11_INITIALIZER_LIST) && \
  defined(Q_COMPILER_INITIALIZER_LISTS)
  QOdbList(std::initializer_list<T> il): l_ (il) {}
#endif
#endif

  // Implicit conversion.
  //
  bool operator==(const QList<T> &x) const {return l_ == x;}
  bool operator!=(const QList<T> &x) const {return l_ != x;}

  int size() const {return l_.size ();}
  void detach() {l_.detach ();}
  void detachShared() {l_.detachShared ();}
  bool isDetached() const {return l_.isDetached ();}
  void setSharable(bool sharable) {l_.setSharable (sharable);}
  // Implicit conversion.
  bool isSharedWith(const QList<T> &x) const {return l_.isSharedWith (x);}
  bool isEmpty() const {return l_.isEmpty ();}
  void clear();

  const T &at(int i) const {return l_.at (i);}
  const T &operator[](int i) const {return l_[i];}
  //T &operator[](int i);
  T &modify (int i);

  void reserve(int size);
  void append(const T &t);
  void append(const QList<T> &t); // Implicit conversion.
  void prepend(const T &t);
  void insert(int i, const T &t);
  void replace(int i, const T &t);
  void removeAt(int i);
  int removeAll(const T &t);
  bool removeOne(const T &t);
  T takeAt(int i);
  T takeFirst();
  T takeLast();
  void move(int from, int to);
  void swap(int i, int j);

  int indexOf(const T &t, int from = 0) const {return l_.indexOf (t, from);}
  int lastIndexOf(const T &t, int from = -1) const
  {return l_.lastIndexOf (t, from);}
  bool contains(const T &t) const {return l_.contains (t);}
  int count(const T &t) const {return l_.count (t);}

  typedef QOdbListIteratorImpl<QOdbList> iterator;
  typedef typename base_list_type::const_iterator const_iterator;

  // stl style
  iterator begin() {return iterator (this, l_.begin ());}
  const_iterator begin() const {return l_.begin ();}
  const_iterator cbegin() const {return l_.cbegin ();}
  const_iterator constBegin() const {return l_.constBegin ();}
  iterator end() {return iterator (this, l_.end ());}
  const_iterator end() const {return l_.end ();}
  const_iterator cend() const {return l_.cend ();}
  const_iterator constEnd() const {return l_.constEnd ();}

  // Return QList iterators. The begin() functions mark all
  // the elements as modified.
  //
  base_iterator_type mbegin ();
  base_iterator_type modifyBegin () {return mbegin ();}
  base_iterator_type mend () {return l_.end ();}
  base_iterator_type modifyEnd () {return mend ();}

  iterator insert(iterator before, const T &t);
  iterator erase(iterator pos);
  iterator erase(iterator first, iterator last);

  // more Qt
  typedef iterator Iterator;
  typedef const_iterator ConstIterator;

  int count() const {return l_.count ();}
  int length() const {return l_.length ();}
  //T& first();
  T& modifyFirst();
  const T& first() const {return l_.first ();}
  //T& last();
  T& modifyLast();
  const T& last() const {return l_.last ();}
  void removeFirst();
  void removeLast();
  bool startsWith(const T &t) const {return l_.startsWith (t);}
  bool endsWith(const T &t) const {return l_.endsWith (t);}
  QList<T> mid(int pos, int length = -1) const {return l_.mid (pos, length);}

  T value(int i) const {return l_.value (i);}
  T value(int i, const T &defValue) const {return l_.value (i, defValue);}

  // stl compatibility
  void push_back(const T &t) {append(t);}
  void push_front(const T &t) {prepend(t);}
  //T& front();
  T& modify_front() {return modifyFirst ();}
  const T& front() const {return l_.front ();}
  //T& back();
  T& modify_back() {return modifyLast ();}
  const T& back() const {return l_.back ();}
  void pop_front() {removeFirst();}
  void pop_back() {removeLast();}
  bool empty() const {return l_.empty ();}

  typedef int size_type;
  typedef T value_type;
  typedef value_type *pointer;
  typedef const value_type *const_pointer;
  typedef value_type &reference;
  typedef const value_type &const_reference;
  typedef typename base_list_type::difference_type difference_type;

  // comfort
  // Implicit conversion.
  QOdbList &operator+=(const QList<T> &l) {append (l); return *this;}
  QOdbList operator+(const QList<T> &l) const
  {QOdbList r (*this); r.append (l); return r;}
  QOdbList &operator+=(const T &t) {append (t); return *this;}
  QOdbList &operator<< (const T &t) {append (t); return *this;}
  QOdbList &operator<<(const QList<T> &l) {append (l); return *this;}

  QVector<T> toVector() const {return l_.toVector ();}
  QSet<T> toSet() const {return l_.toSet ();}

  static QOdbList fromVector(const QVector<T> &v)
  {return base_list_type::fromVector (v);}
  static QOdbList fromSet(const QSet<T> &s)
  {return base_list_type::fromSet (s);}

  static QOdbList fromStdList(const std::list<T> &l)
  {return base_list_type::fromStdList (l);}
  std::list<T> toStdList() const {return l_.toStdList ();}

  // Interfacing with the base list.
  //
  QOdbList (const base_list_type& x): l_ (x) {}
  QOdbList& operator= (const base_list_type&);
  operator const base_list_type& () const {return l_;}
  base_list_type& base () {return l_;}
  const base_list_type& base () const {return l_;}

#ifdef ODB_CXX11
  QOdbList (base_list_type&& x): l_ (std::move (x)) {}
  QOdbList& operator= (base_list_type&&);
#endif

  // Change tracking (the rest comes from vector_base).
  //
public:
  void _start () const {impl_.start (l_.size ());}

private:
  base_list_type l_;
};

template <typename L>
class QOdbListIteratorImpl
{
public:
  typedef L list_type;
  typedef typename list_type::base_iterator_type base_iterator_type;
  typedef typename list_type::const_iterator const_iterator_type;

  typedef typename base_iterator_type::iterator_category iterator_category;
  typedef typename base_iterator_type::difference_type difference_type;
  typedef typename base_iterator_type::value_type value_type;
  typedef typename base_iterator_type::pointer pointer;
  typedef typename base_iterator_type::reference reference;

  typedef typename list_type::size_type size_type;
  typedef typename list_type::const_reference const_reference;
  typedef typename list_type::const_pointer const_pointer;

  QOdbListIteratorImpl (): l_ (0), i_ () {}
  QOdbListIteratorImpl (list_type* l, const base_iterator_type& i)
      : l_ (l), i_ (i) {}

#ifndef QT_STRICT_ITERATORS
  operator const_iterator_type () const {return i_;}
#endif
  base_iterator_type base () const {return i_;}
  list_type* list () const {return l_;}

  // Note: const_{reference,pointer}.
  //
  const_reference operator* () const {return *i_;}
  const_pointer   operator-> () const {return i_.operator -> ();}
  const_reference operator[] (difference_type n) const {return i_[n];}

  // Modifiers.
  //
  reference modify () const;
  reference modify (difference_type n) const;

  QOdbListIteratorImpl& operator++ () {++i_; return *this;}
  QOdbListIteratorImpl  operator++ (int)
  {return QOdbListIteratorImpl (l_, i_++);}
  QOdbListIteratorImpl& operator-- () {--i_; return *this;}
  QOdbListIteratorImpl  operator-- (int)
  {return QOdbListIteratorImpl (l_, i_--);}

  QOdbListIteratorImpl operator+ (int n) const
  {return QOdbListIteratorImpl (l_, i_ + n);}
  QOdbListIteratorImpl& operator+= (int n) {i_ += n; return *this;}
  QOdbListIteratorImpl operator- (int n) const
  {return QOdbListIteratorImpl (l_, i_ - n);}
  QOdbListIteratorImpl& operator-= (int n) {i_ -= n; return *this;}

private:
  list_type* l_;
  base_iterator_type i_;
};

// operator==
//
template <typename L>
inline bool
operator== (const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () == y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline bool
operator== (const QOdbListIteratorImpl<L>& x,
            const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () == y;}

template <typename L>
inline bool
operator== (const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
            const QOdbListIteratorImpl<L>& y)
{return x == y.base ();}
#endif

// operator<
//
template <typename L>
inline bool
operator< (const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () < y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline bool
operator< (const QOdbListIteratorImpl<L>& x,
           const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () < y;}

template <typename L>
inline bool
operator< (const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
           const QOdbListIteratorImpl<L>& y)
{return x < y.base ();}
#endif

// operator!=
//
template <typename L>
inline bool
operator!= (const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () != y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline bool
operator!= (const QOdbListIteratorImpl<L>& x,
            const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () != y;}

template <typename L>
inline bool
operator!= (const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
            const QOdbListIteratorImpl<L>& y)
{return x != y.base ();}
#endif

// operator>
//
template <typename L>
inline bool
operator> (const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () > y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline bool
operator> (const QOdbListIteratorImpl<L>& x,
           const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () > y;}

template <typename L>
inline bool
operator> (const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
           const QOdbListIteratorImpl<L>& y)
{return x > y.base ();}
#endif

// operator>=
//
template <typename L>
inline bool
operator>= (const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () >= y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline bool
operator>= (const QOdbListIteratorImpl<L>& x,
            const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () >= y;}

template <typename L>
inline bool
operator>= (const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
            const QOdbListIteratorImpl<L>& y)
{return x >= y.base ();}
#endif

// operator<=
//
template <typename L>
inline bool
operator<= (const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () <= y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline bool
operator<= (const QOdbListIteratorImpl<L>& x,
            const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () <= y;}

template <typename L>
inline bool
operator<= (const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
            const QOdbListIteratorImpl<L>& y)
{return x <= y.base ();}
#endif

// operator-
//
template <typename L>
inline typename QOdbListIteratorImpl<L>::difference_type
operator-(const QOdbListIteratorImpl<L>& x, const QOdbListIteratorImpl<L>& y)
{return x.base () - y.base ();}

#ifndef QT_STRICT_ITERATORS
template <typename L>
inline typename QOdbListIteratorImpl<L>::difference_type
operator-(const QOdbListIteratorImpl<L>& x,
          const typename QOdbListIteratorImpl<L>::const_iterator_type& y)
{return x.base () - y;}

template <typename L>
inline typename QOdbListIteratorImpl<L>::difference_type
operator-(const typename QOdbListIteratorImpl<L>::const_iterator_type& x,
          const QOdbListIteratorImpl<L>& y)
{return x - y.base ();}
#endif

// operator+
//
template <typename L>
inline QOdbListIteratorImpl<L>
operator+(typename QOdbListIteratorImpl<L>::difference_type n,
          const QOdbListIteratorImpl<L>& x)
{return QOdbListIteratorImpl<L> (x.list (), n + x.base ());}

#include <odb/qt/containers/list.ixx>

#include <odb/qt/containers/list-traits.hxx>

#include <odb/post.hxx>

#endif // ODB_QT_CONTAINERS_LIST_HXX