containers: add C++11-style c{begin,end}() as alias for const{Begin,End}()

C++11 adds cbegin()/cend() functions for the same reason Qt has
constBegin()/constEnd(). This patch adds these functions to the
Qt containers with the same implementation as constBegin()/constEnd().

It also fixes the return types in the documentation of existing
constFind() functions (documentation only).

C++11 only adds cbegin()/cend() (and crbegin()/crend(), which Qt doesn't have).
In particular, it doesn't add cfind(), so I didn't supply these, even though
Qt comes with constFind().

This is a forward-port of https://qt.gitorious.org/qt/qt/merge_requests/1365.

Change-Id: Ida086b64246b24e25254eafbcb06c8e33388502b
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2011-08-29 17:49:48 +02:00 committed by Qt by Nokia
parent 7c7e47493e
commit d823646db3
21 changed files with 217 additions and 15 deletions

View File

@ -799,6 +799,12 @@ const QConstByteArrayData<1> QByteArray::shared_empty = { { Q_REFCOUNT_INITIALIZ
\internal
*/
/*! \fn QByteArray::const_iterator QByteArray::cbegin() const
\since 5.0
\internal
*/
/*! \fn QByteArray::const_iterator QByteArray::constBegin() const
\internal
@ -814,6 +820,12 @@ const QConstByteArrayData<1> QByteArray::shared_empty = { { Q_REFCOUNT_INITIALIZ
\internal
*/
/*! \fn QByteArray::const_iterator QByteArray::cend() const
\since 5.0
\internal
*/
/*! \fn QByteArray::const_iterator QByteArray::constEnd() const
\internal

View File

@ -358,9 +358,11 @@ public:
typedef const_iterator ConstIterator;
iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
const_iterator constBegin() const;
iterator end();
const_iterator end() const;
const_iterator cend() const;
const_iterator constEnd() const;
// stl compatibility
@ -492,12 +494,16 @@ inline QByteArray::iterator QByteArray::begin()
{ detach(); return d->data(); }
inline QByteArray::const_iterator QByteArray::begin() const
{ return d->data(); }
inline QByteArray::const_iterator QByteArray::cbegin() const
{ return d->data(); }
inline QByteArray::const_iterator QByteArray::constBegin() const
{ return d->data(); }
inline QByteArray::iterator QByteArray::end()
{ detach(); return d->data() + d->size; }
inline QByteArray::const_iterator QByteArray::end() const
{ return d->data() + d->size; }
inline QByteArray::const_iterator QByteArray::cend() const
{ return d->data() + d->size; }
inline QByteArray::const_iterator QByteArray::constEnd() const
{ return d->data() + d->size; }
inline QByteArray &QByteArray::operator+=(char c)

View File

@ -1045,6 +1045,15 @@ void QHashData::checkSanity()
\overload
*/
/*! \fn QHash::const_iterator QHash::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first item
in the hash.
\sa begin(), cend()
*/
/*! \fn QHash::const_iterator QHash::constBegin() const
Returns a const \l{STL-style iterator} pointing to the first item
@ -1074,6 +1083,15 @@ void QHashData::checkSanity()
\sa constBegin(), end()
*/
/*! \fn QHash::const_iterator QHash::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the hash.
\sa cbegin(), end()
*/
/*! \fn QHash::iterator QHash::erase(iterator pos)
Removes the (key, value) pair associated with the iterator \a pos
@ -1114,7 +1132,7 @@ void QHashData::checkSanity()
\overload
*/
/*! \fn QHash::iterator QHash::constFind(const Key &key) const
/*! \fn QHash::const_iterator QHash::constFind(const Key &key) const
\since 4.1
Returns an iterator pointing to the item with the \a key in the

View File

@ -440,9 +440,11 @@ public:
// STL style
inline iterator begin() { detach(); return iterator(d->firstNode()); }
inline const_iterator begin() const { return const_iterator(d->firstNode()); }
inline const_iterator cbegin() const { return const_iterator(d->firstNode()); }
inline const_iterator constBegin() const { return const_iterator(d->firstNode()); }
inline iterator end() { detach(); return iterator(e); }
inline const_iterator end() const { return const_iterator(e); }
inline const_iterator cend() const { return const_iterator(e); }
inline const_iterator constEnd() const { return const_iterator(e); }
iterator erase(iterator it);

View File

@ -330,6 +330,15 @@ const QLinkedListData QLinkedListData::shared_null = {
\overload
*/
/*! \fn QLinkedList::const_iterator QLinkedList::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first item
in the list.
\sa begin(), cend()
*/
/*! \fn QLinkedList::const_iterator QLinkedList::constBegin() const
Returns a const \l{STL-style iterator} pointing to the first item
@ -351,6 +360,15 @@ const QLinkedListData QLinkedListData::shared_null = {
\overload
*/
/*! \fn QLinkedList::const_iterator QLinkedList::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the list.
\sa cbegin(), end()
*/
/*! \fn QLinkedList::const_iterator QLinkedList::constEnd() const
Returns a const \l{STL-style iterator} pointing to the imaginary

View File

@ -180,9 +180,11 @@ public:
// stl style
inline iterator begin() { detach(); return e->n; }
inline const_iterator begin() const { return e->n; }
inline const_iterator cbegin() const { return e->n; }
inline const_iterator constBegin() const { return e->n; }
inline iterator end() { detach(); return e; }
inline const_iterator end() const { return e; }
inline const_iterator cend() const { return e; }
inline const_iterator constEnd() const { return e; }
iterator insert(iterator before, const T &t);
iterator erase(iterator pos);

View File

@ -921,6 +921,15 @@ void **QListData::erase(void **xi)
\overload
*/
/*! \fn QList::const_iterator QList::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first item
in the list.
\sa begin(), cend()
*/
/*! \fn QList::const_iterator QList::constBegin() const
Returns a const \l{STL-style iterator} pointing to the first item
@ -942,6 +951,15 @@ void **QListData::erase(void **xi)
\overload
*/
/*! \fn QList::const_iterator QList::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the list.
\sa cbegin(), end()
*/
/*! \fn QList::const_iterator QList::constEnd() const
Returns a const \l{STL-style iterator} pointing to the imaginary

View File

@ -262,9 +262,11 @@ public:
// stl style
inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); }
inline const_iterator begin() const { return reinterpret_cast<Node *>(p.begin()); }
inline const_iterator cbegin() const { return reinterpret_cast<Node *>(p.begin()); }
inline const_iterator constBegin() const { return reinterpret_cast<Node *>(p.begin()); }
inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); }
inline const_iterator end() const { return reinterpret_cast<Node *>(p.end()); }
inline const_iterator cend() const { return reinterpret_cast<Node *>(p.end()); }
inline const_iterator constEnd() const { return reinterpret_cast<Node *>(p.end()); }
iterator insert(iterator before, const T &t);
iterator erase(iterator pos);

View File

@ -682,6 +682,15 @@ void QMapData::dump()
\overload
*/
/*! \fn QMap::const_iterator QMap::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first item
in the map.
\sa begin(), cend()
*/
/*! \fn QMap::const_iterator QMap::constBegin() const
Returns a const \l{STL-style iterator} pointing to the first item
@ -703,6 +712,15 @@ void QMapData::dump()
\overload
*/
/*! \fn QMap::const_iterator QMap::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the map.
\sa cbegin(), end()
*/
/*! \fn QMap::const_iterator QMap::constEnd() const
Returns a const \l{STL-style iterator} pointing to the imaginary
@ -744,7 +762,7 @@ void QMapData::dump()
\overload
*/
/*! \fn QMap::iterator QMap::constFind(const Key &key) const
/*! \fn QMap::const_iterator QMap::constFind(const Key &key) const
\since 4.1
Returns an const iterator pointing to the item with key \a key in the

View File

@ -362,12 +362,14 @@ public:
// STL style
inline iterator begin() { detach(); return iterator(e->forward[0]); }
inline const_iterator begin() const { return const_iterator(e->forward[0]); }
inline const_iterator cbegin() const { return const_iterator(e->forward[0]); }
inline const_iterator constBegin() const { return const_iterator(e->forward[0]); }
inline iterator end() {
detach();
return iterator(e);
}
inline const_iterator end() const { return const_iterator(e); }
inline const_iterator cend() const { return const_iterator(e); }
inline const_iterator constEnd() const { return const_iterator(e); }
iterator erase(iterator it);

View File

@ -164,9 +164,11 @@ public:
// STL style
inline iterator begin() { return q_hash.begin(); }
inline const_iterator begin() const { return q_hash.begin(); }
inline const_iterator cbegin() const { return q_hash.begin(); }
inline const_iterator constBegin() const { return q_hash.constBegin(); }
inline iterator end() { return q_hash.end(); }
inline const_iterator end() const { return q_hash.end(); }
inline const_iterator cend() const { return q_hash.end(); }
inline const_iterator constEnd() const { return q_hash.constEnd(); }
iterator erase(iterator i)
{ return q_hash.erase(reinterpret_cast<typename Hash::iterator &>(i)); }

View File

@ -343,6 +343,15 @@
item in the set.
*/
/*! \fn QSet::const_iterator QSet::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} positioned at the first
item in the set.
\sa begin(), cend()
*/
/*! \fn QSet::const_iterator QSet::constBegin() const
Returns a const \l{STL-style iterator} positioned at the first
@ -367,6 +376,15 @@
imaginary item after the last item in the set.
*/
/*! \fn QSet::const_iterator QSet::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the set.
\sa cbegin(), end()
*/
/*! \fn QSet::const_iterator QSet::constEnd() const
Returns a const \l{STL-style iterator} pointing to the imaginary

View File

@ -859,6 +859,15 @@ int QString::grow(int size)
\overload begin()
*/
/*! \fn QString::const_iterator QString::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first character
in the string.
\sa begin(), cend()
*/
/*! \fn QString::const_iterator QString::constBegin() const
Returns a const \l{STL-style iterator} pointing to the first character
@ -880,6 +889,15 @@ int QString::grow(int size)
\overload end()
*/
/*! \fn QString::const_iterator QString::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the list.
\sa cbegin(), end()
*/
/*! \fn QString::const_iterator QString::constEnd() const
Returns a const \l{STL-style iterator} pointing to the imaginary

View File

@ -558,9 +558,11 @@ public:
typedef const_iterator ConstIterator;
iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
const_iterator constBegin() const;
iterator end();
const_iterator end() const;
const_iterator cend() const;
const_iterator constEnd() const;
// STL compatibility
@ -904,12 +906,16 @@ inline QString::iterator QString::begin()
{ detach(); return reinterpret_cast<QChar*>(d->data()); }
inline QString::const_iterator QString::begin() const
{ return reinterpret_cast<const QChar*>(d->data()); }
inline QString::const_iterator QString::cbegin() const
{ return reinterpret_cast<const QChar*>(d->data()); }
inline QString::const_iterator QString::constBegin() const
{ return reinterpret_cast<const QChar*>(d->data()); }
inline QString::iterator QString::end()
{ detach(); return reinterpret_cast<QChar*>(d->data() + d->size); }
inline QString::const_iterator QString::end() const
{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
inline QString::const_iterator QString::cend() const
{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
inline QString::const_iterator QString::constEnd() const
{ return reinterpret_cast<const QChar*>(d->data() + d->size); }
inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const

View File

@ -160,9 +160,11 @@ public:
inline iterator begin() { return ptr; }
inline const_iterator begin() const { return ptr; }
inline const_iterator cbegin() const { return ptr; }
inline const_iterator constBegin() const { return ptr; }
inline iterator end() { return ptr + s; }
inline const_iterator end() const { return ptr + s; }
inline const_iterator cend() const { return ptr + s; }
inline const_iterator constEnd() const { return ptr + s; }
iterator insert(iterator before, int n, const T &x);
inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }

View File

@ -451,6 +451,15 @@
\overload
*/
/*! \fn QVarLengthArray::const_iterator QVarLengthArray::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first item
in the array.
\sa begin(), cend()
*/
/*! \fn QVarLengthArray::const_iterator QVarLengthArray::constBegin() const
\since 4.8
@ -475,6 +484,15 @@
\overload
*/
/*! \fn QVarLengthArray::const_iterator QVarLengthArray::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the array.
\sa cbegin(), end()
*/
/*! \fn QVarLengthArray::const_iterator QVarLengthArray::constEnd() const
\since 4.8

View File

@ -698,6 +698,15 @@ int QVectorData::grow(int sizeofTypedData, int size, int sizeofT, bool excessive
\overload
*/
/*! \fn QVector::const_iterator QVector::cbegin() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the first item
in the vector.
\sa begin(), cend()
*/
/*! \fn QVector::const_iterator QVector::constBegin() const
Returns a const \l{STL-style iterator} pointing to the first item
@ -719,6 +728,15 @@ int QVectorData::grow(int sizeofTypedData, int size, int sizeofT, bool excessive
\overload
*/
/*! \fn QVector::const_iterator QVector::cend() const
\since 5.0
Returns a const \l{STL-style iterator} pointing to the imaginary
item after the last item in the vector.
\sa cbegin(), end()
*/
/*! \fn QVector::const_iterator QVector::constEnd() const
Returns a const \l{STL-style iterator} pointing to the imaginary

View File

@ -245,9 +245,11 @@ public:
#endif
inline iterator begin() { detach(); return p->array; }
inline const_iterator begin() const { return p->array; }
inline const_iterator cbegin() const { return p->array; }
inline const_iterator constBegin() const { return p->array; }
inline iterator end() { detach(); return p->array + d->size; }
inline const_iterator end() const { return p->array + d->size; }
inline const_iterator cend() const { return p->array + d->size; }
inline const_iterator constEnd() const { return p->array + d->size; }
iterator insert(iterator before, int n, const T &x);
inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }

View File

@ -285,6 +285,8 @@ void tst_QMap::beginEnd()
QVERIFY( map.constBegin() == map.constEnd() );
map.insert( "0", "a" );
map.insert( "1", "b" );
QVERIFY( map.constBegin() == map.cbegin() );
QVERIFY( map.constEnd() == map.cend() );
// make a copy. const function shouldn't detach
StringMap map2 = map;

View File

@ -393,9 +393,9 @@ void tst_QSet::begin()
{
QSet<int>::const_iterator i = set1.constBegin();
QSet<int>::const_iterator j = set1.constBegin();
QSet<int>::const_iterator j = set1.cbegin();
QSet<int>::const_iterator k = set2.constBegin();
QSet<int>::const_iterator ell = set2.constBegin();
QSet<int>::const_iterator ell = set2.cbegin();
QVERIFY(i == j);
QVERIFY(k == ell);
@ -407,9 +407,9 @@ void tst_QSet::begin()
{
QSet<int>::const_iterator i = set1.constBegin();
QSet<int>::const_iterator j = set1.constBegin();
QSet<int>::const_iterator j = set1.cbegin();
QSet<int>::const_iterator k = set2.constBegin();
QSet<int>::const_iterator ell = set2.constBegin();
QSet<int>::const_iterator ell = set2.cbegin();
QVERIFY(i == j);
QVERIFY(k == ell);
@ -421,9 +421,9 @@ void tst_QSet::begin()
{
QSet<int>::const_iterator i = set1.constBegin();
QSet<int>::const_iterator j = set1.constBegin();
QSet<int>::const_iterator j = set1.cbegin();
QSet<int>::const_iterator k = set2.constBegin();
QSet<int>::const_iterator ell = set2.constBegin();
QSet<int>::const_iterator ell = set2.cbegin();
QVERIFY(i == j);
QVERIFY(k == ell);
@ -439,9 +439,9 @@ void tst_QSet::end()
{
QSet<int>::const_iterator i = set1.constEnd();
QSet<int>::const_iterator j = set1.constEnd();
QSet<int>::const_iterator j = set1.cend();
QSet<int>::const_iterator k = set2.constEnd();
QSet<int>::const_iterator ell = set2.constEnd();
QSet<int>::const_iterator ell = set2.cend();
QVERIFY(i == j);
QVERIFY(k == ell);
@ -456,9 +456,9 @@ void tst_QSet::end()
{
QSet<int>::const_iterator i = set1.constEnd();
QSet<int>::const_iterator j = set1.constEnd();
QSet<int>::const_iterator j = set1.cend();
QSet<int>::const_iterator k = set2.constEnd();
QSet<int>::const_iterator ell = set2.constEnd();
QSet<int>::const_iterator ell = set2.cend();
QVERIFY(i == j);
QVERIFY(k == ell);
@ -473,9 +473,9 @@ void tst_QSet::end()
{
QSet<int>::const_iterator i = set1.constEnd();
QSet<int>::const_iterator j = set1.constEnd();
QSet<int>::const_iterator j = set1.cend();
QSet<int>::const_iterator k = set2.constEnd();
QSet<int>::const_iterator ell = set2.constEnd();
QSet<int>::const_iterator ell = set2.cend();
QVERIFY(i == j);
QVERIFY(k == ell);

View File

@ -550,6 +550,10 @@ void tst_Collections::list()
QList<QString>::const_iterator cit = list.constBegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
cit = list.cbegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
}
{
@ -967,6 +971,10 @@ void tst_Collections::linkedList()
QLinkedList<QString>::const_iterator cit = list.constBegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
cit = list.cbegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
}
{
@ -1607,6 +1615,10 @@ void tst_Collections::hash()
QHash<int, QString>::const_iterator cit = hash.constBegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
cit = hash.cbegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
}
{
@ -1924,6 +1936,10 @@ void tst_Collections::map()
QMap<int, QString>::const_iterator cit = map.constBegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
cit = map.cbegin();
QVERIFY((*cit).toLower() == "xello");
QVERIFY(cit->toUpper() == "XELLO");
}
{
@ -2902,7 +2918,7 @@ void tst_Collections::linkedlist_stl()
QCOMPARE(int(stdList.size()), elements.size());
std::list<QString>::const_iterator it = stdList.begin();
QLinkedList<QString>::const_iterator it2 = list.constBegin();
QLinkedList<QString>::const_iterator it2 = list.cbegin();
for (uint j = 0; j < stdList.size(); ++j, ++it, ++it2)
QCOMPARE(*it, *it2);
@ -3001,9 +3017,11 @@ void instantiateContainer()
#ifndef QT_NO_STL
typename ContainerType::const_iterator constIt;
constIt = constContainer.begin();
constIt = container.cbegin();
container.constBegin();
constIt = constContainer.end();
constIt = constContainer.cend();
container.constEnd();
Q_UNUSED(constIt)
#endif