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/list.ixx | 317 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 317 insertions(+) create mode 100644 odb/qt/containers/list.ixx (limited to 'odb/qt/containers/list.ixx') diff --git a/odb/qt/containers/list.ixx b/odb/qt/containers/list.ixx new file mode 100644 index 0000000..90a10e6 --- /dev/null +++ b/odb/qt/containers/list.ixx @@ -0,0 +1,317 @@ +// file : odb/qt/containers/list.ixx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// +// QOdbList +// + +template +inline QOdbList& QOdbList:: +operator= (const QOdbList& x) +{ + l_ = x.l_; + if (_tracking ()) + impl_.assign (static_cast (l_.size ())); + return *this; +} + +template +inline QOdbList& QOdbList:: +operator= (const base_list_type& x) +{ + l_ = x; + if (_tracking ()) + impl_.assign (static_cast (l_.size ())); + return *this; +} + +#ifdef ODB_CXX11 +template +inline QOdbList& QOdbList:: +operator= (QOdbList&& x) +{ + l_ = std::move (x.l_); + if (_tracking ()) + impl_.assign (static_cast (l_.size ())); + return *this; +} + +template +inline QOdbList& QOdbList:: +operator= (base_list_type&& x) +{ + l_ = std::move (x); + if (_tracking ()) + impl_.assign (static_cast (l_.size ())); + return *this; +} +#endif + +template +inline void QOdbList:: +swap (QOdbList& x) +{ + l_.swap (x.l_); + vector_base::swap (x); +} + +template +inline void QOdbList:: +clear() +{ + l_.clear (); + if (_tracking ()) + impl_.clear (); +} + +template +inline T& QOdbList:: +modify (int i) +{ + T& r (l_[i]); + if (_tracking ()) + impl_.modify (static_cast (i)); + return r; +} + +template +inline void QOdbList:: +reserve (int n) +{ + l_.reserve (n); + if (_tracking ()) + impl_.reserve (static_cast (n)); +} + +template +inline void QOdbList:: +append (const T& x) +{ + l_.append (x); + if (_tracking ()) + impl_.push_back (); +} + +template +inline void QOdbList:: +append (const QList& x) +{ + l_.append (x); + if (_tracking ()) + impl_.push_back (static_cast (x.size ())); +} + +template +inline void QOdbList:: +prepend (const T& x) +{ + l_.prepend (x); + if (_tracking ()) + impl_.insert (0); +} + +template +inline void QOdbList:: +insert (int i, const T& x) +{ + l_.insert (i, x); + if (_tracking ()) + impl_.insert (static_cast (i)); +} + +template +inline void QOdbList:: +replace (int i, const T& x) +{ + l_.insert (i, x); + if (_tracking ()) + impl_.modify (static_cast (i)); +} + +template +inline void QOdbList:: +removeAt (int i) +{ + l_.removeAt (i); + if (_tracking ()) + impl_.erase (static_cast (i)); +} + +template +inline int QOdbList:: +removeAll (const T& x) +{ + // We have to re-implement this one ourselves since we need to + // know the indexes of the removed elements. + // + int r (0); + for (int i (l_.indexOf (x)); i != -1; i = l_.indexOf (x, i)) + { + removeAt (i); + r++; + } + return r; +} + +template +inline bool QOdbList:: +removeOne (const T& x) +{ + // We have to re-implement this one ourselves since we need to + // know the index of the removed element. + // + int i (l_.indexOf (x)); + if (i != -1) + removeAt (i); + return i != -1; +} + +template +inline T QOdbList:: +takeAt (int i) +{ + if (_tracking ()) + impl_.erase (static_cast (i)); + return l_.takeAt (i); +} + +template +inline T QOdbList:: +takeFirst () +{ + if (_tracking ()) + impl_.erase (0); + return l_.takeFirst (); +} + +template +inline T QOdbList:: +takeLast () +{ + if (_tracking ()) + impl_.pop_back (); + return l_.takeLast (); +} + +template +inline void QOdbList:: +move (int from, int to) +{ + l_.move (from, to); + if (_tracking ()) + { + impl_.erase (static_cast (from)); + impl_.insert (static_cast (to)); + } +} + +template +inline void QOdbList:: +swap (int i, int j) +{ + l_.swap (i, j); + if (_tracking ()) + { + impl_.modify (static_cast (i)); + impl_.modify (static_cast (j)); + } +} + +template +inline typename QOdbList::base_iterator_type QOdbList:: +mbegin () +{ + if (_tracking ()) + impl_.modify (0, static_cast (l_.size ())); + return l_.begin (); +} + +template +inline typename QOdbList::iterator QOdbList:: +insert (iterator p, const T& x) +{ + if (_tracking ()) + impl_.insert (static_cast (p.base () - l_.begin ())); + return iterator (this, l_.insert (p.base (), x)); +} + +template +inline typename QOdbList::iterator QOdbList:: +erase (iterator p) +{ + if (_tracking ()) + impl_.erase (static_cast (p.base () - l_.begin ())); + return iterator (this, l_.erase (p.base ())); +} + +template +inline typename QOdbList::iterator QOdbList:: +erase (iterator f, iterator l) +{ + if (_tracking ()) + impl_.erase (static_cast (f.base () - l_.begin ()), + static_cast (l - f)); + return iterator (this, l_.erase (f.base (), l.base ())); +} + +template +inline T& QOdbList:: +modify_first () +{ + T& r (l_.first ()); + if (_tracking ()) + impl_.modify (0); + return r; +} + +template +inline T& QOdbList:: +modify_last () +{ + T& r (l_.last ()); + if (_tracking ()) + impl_.modify (static_cast (l_.size () - 1)); + return r; +} + +template +inline void QOdbList:: +removeFirst () +{ + l_.removeFirst (); + if (_tracking ()) + impl_.erase (0); +} + +template +inline void QOdbList:: +removeLast () +{ + l_.removeLast (); + if (_tracking ()) + impl_.pop_back (); +} + +// +// QOdbListIteratorImpl +// + +template +inline typename QOdbListIteratorImpl::reference QOdbListIteratorImpl:: +modify () const +{ + if (l_->_tracking ()) + l_->_impl ().modify (static_cast (i_ - l_->base ().begin ())); + return *i_; +} + +template +inline typename QOdbListIteratorImpl::reference QOdbListIteratorImpl:: +modify (difference_type n) const +{ + if (l_->_tracking ()) + l_->_impl ().modify ( + static_cast (i_ - l_->base ().begin () + n)); + return i_[n]; +} -- cgit v1.1