From e93dcc2ecabee46d31aedf9f4ddc8058956c78ad Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 5 Feb 2013 15:50:07 +0200 Subject: Add support for change-tracking containers ODB now supports "smart" ordered containers. Such containers get extra functions for updating and deleting individual elements. Based on this functionality implement two change-tracking containers: odb::vector (equivalent to std::vector) and QOdbList (equivalent to QList). New tests: common/container/change-tracking and qt/common/container/change- tracking. --- odb/qt/containers/mutable-list-iterator.hxx | 111 ++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 odb/qt/containers/mutable-list-iterator.hxx (limited to 'odb/qt/containers/mutable-list-iterator.hxx') diff --git a/odb/qt/containers/mutable-list-iterator.hxx b/odb/qt/containers/mutable-list-iterator.hxx new file mode 100644 index 0000000..ff9d9f5 --- /dev/null +++ b/odb/qt/containers/mutable-list-iterator.hxx @@ -0,0 +1,111 @@ +// file : odb/qt/containers/mutable-list-iterator.hxx +// copyright : Copyright (c) 2005-2012 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 + +#include + +#include + +// Java-style QMutableListIterator-like iterator. The implementation +// is based on what's found in qiterator.h. +// +template +class QMutableOdbListIterator +{ +public: + QMutableOdbListIterator(QOdbList &x) + : c (&x) + { + c->setSharable(false); + i = c->begin(); + n = c->end(); + } + + ~QMutableOdbListIterator() {c->setSharable(true);} + + QMutableOdbListIterator &operator=(QOdbList &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::iterator iterator; + typedef typename QOdbList::const_iterator const_iterator; + + QOdbList* c; + iterator i, n; +}; + +#include + +#endif // ODB_QT_CONTAINERS_MUTABLE_LIST_ITERATOR_HXX -- cgit v1.1