QAssociativeIterable: add find()

This is like value(), but returns an iterator instead of the value().

[ChangeLog][QtCore][QAssociativeIterable] Added find().

Change-Id: I029fc8f91cef78f718d419587a2a50ffd2bf7632
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@theqtcompany.com>
This commit is contained in:
Marc Mutz 2014-12-22 21:31:02 +01:00
parent b9365aed6a
commit 914c5eb36a
3 changed files with 17 additions and 16 deletions

View File

@ -4001,11 +4001,11 @@ void QAssociativeIterable::const_iterator::end()
m_impl.end();
}
void find(QAssociativeIterable::const_iterator &it, const QVariant &key)
void QAssociativeIterable::const_iterator::find(const QVariant &key)
{
Q_ASSERT(key.userType() == it.m_impl._metaType_id_key);
Q_ASSERT(key.userType() == m_impl._metaType_id_key);
const QtMetaTypePrivate::VariantData dkey(key.userType(), key.constData(), 0 /*key.flags()*/);
it.m_impl.find(dkey);
m_impl.find(dkey);
}
/*!
@ -4035,7 +4035,7 @@ QAssociativeIterable::const_iterator QAssociativeIterable::end() const
}
/*!
\internal
\since 5.5
Returns a QAssociativeIterable::const_iterator for the given key \a key
in the container, if the types are convertible.
@ -4046,12 +4046,12 @@ QAssociativeIterable::const_iterator QAssociativeIterable::end() const
\sa begin(), end(), value()
*/
QAssociativeIterable::const_iterator find(const QAssociativeIterable &iterable, const QVariant &key)
QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant &key) const
{
QAssociativeIterable::const_iterator it(iterable, new QAtomicInt(0));
const_iterator it(*this, new QAtomicInt(0));
QVariant key_ = key;
if (key_.canConvert(iterable.m_impl._metaType_id_key) && key_.convert(iterable.m_impl._metaType_id_key))
find(it, key_);
if (key_.canConvert(m_impl._metaType_id_key) && key_.convert(m_impl._metaType_id_key))
it.find(key_);
else
it.end();
return it;
@ -4059,10 +4059,12 @@ QAssociativeIterable::const_iterator find(const QAssociativeIterable &iterable,
/*!
Returns the value for the given \a key in the container, if the types are convertible.
\sa find()
*/
QVariant QAssociativeIterable::value(const QVariant &key) const
{
const const_iterator it = find(*this, key);
const const_iterator it = find(key);
if (it == end())
return QVariant();
return *it;

View File

@ -631,9 +631,7 @@ public:
void begin();
void end();
// ### Qt 5.5: make find() (1st one) a member function
friend void find(const_iterator &it, const QVariant &key);
friend const_iterator find(const QAssociativeIterable &iterable, const QVariant &key);
void find(const QVariant &key);
public:
~const_iterator();
const_iterator(const const_iterator &other);
@ -663,9 +661,7 @@ public:
const_iterator begin() const;
const_iterator end() const;
private: // ### Qt 5.5: make it a public find() member function:
friend const_iterator find(const QAssociativeIterable &iterable, const QVariant &key);
public:
const_iterator find(const QVariant &key) const;
QVariant value(const QVariant &key) const;

View File

@ -4407,10 +4407,13 @@ void tst_QVariant::iterateContainerElements()
QCOMPARE(varMap.value(QString::number(key)).value<MAPPED_TYPE>(), expected); \
QCOMPARE(varHash.value(QString::number(key)).value<MAPPED_TYPE>(), expected); \
QCOMPARE(actual, expected); \
const QAssociativeIterable::const_iterator it = mappingIter.find(key); \
QVERIFY(it != mappingIter.end()); \
QCOMPARE(it.value().value<MAPPED_TYPE>(), expected); \
} \
QCOMPARE(numSeen, (int)std::distance(mapping.begin(), mapping.end())); \
QCOMPARE(containerIter, containerEnd); \
\
QVERIFY(mappingIter.find(10) == mappingIter.end()); \
}
TEST_ASSOCIATIVE_ITERATION(QHash, int, bool)