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(); 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()*/); 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 Returns a QAssociativeIterable::const_iterator for the given key \a key
in the container, if the types are convertible. in the container, if the types are convertible.
@ -4046,12 +4046,12 @@ QAssociativeIterable::const_iterator QAssociativeIterable::end() const
\sa begin(), end(), value() \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; QVariant key_ = key;
if (key_.canConvert(iterable.m_impl._metaType_id_key) && key_.convert(iterable.m_impl._metaType_id_key)) if (key_.canConvert(m_impl._metaType_id_key) && key_.convert(m_impl._metaType_id_key))
find(it, key_); it.find(key_);
else else
it.end(); it.end();
return it; 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. 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 QVariant QAssociativeIterable::value(const QVariant &key) const
{ {
const const_iterator it = find(*this, key); const const_iterator it = find(key);
if (it == end()) if (it == end())
return QVariant(); return QVariant();
return *it; return *it;

View File

@ -631,9 +631,7 @@ public:
void begin(); void begin();
void end(); void end();
// ### Qt 5.5: make find() (1st one) a member function void find(const QVariant &key);
friend void find(const_iterator &it, const QVariant &key);
friend const_iterator find(const QAssociativeIterable &iterable, const QVariant &key);
public: public:
~const_iterator(); ~const_iterator();
const_iterator(const const_iterator &other); const_iterator(const const_iterator &other);
@ -663,9 +661,7 @@ public:
const_iterator begin() const; const_iterator begin() const;
const_iterator end() const; const_iterator end() const;
private: // ### Qt 5.5: make it a public find() member function: const_iterator find(const QVariant &key) const;
friend const_iterator find(const QAssociativeIterable &iterable, const QVariant &key);
public:
QVariant value(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(varMap.value(QString::number(key)).value<MAPPED_TYPE>(), expected); \
QCOMPARE(varHash.value(QString::number(key)).value<MAPPED_TYPE>(), expected); \ QCOMPARE(varHash.value(QString::number(key)).value<MAPPED_TYPE>(), expected); \
QCOMPARE(actual, 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(numSeen, (int)std::distance(mapping.begin(), mapping.end())); \
QCOMPARE(containerIter, containerEnd); \ QCOMPARE(containerIter, containerEnd); \
\ QVERIFY(mappingIter.find(10) == mappingIter.end()); \
} }
TEST_ASSOCIATIVE_ITERATION(QHash, int, bool) TEST_ASSOCIATIVE_ITERATION(QHash, int, bool)