mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 02:53:10 +01:00
add AbstractCache::forEach()
This commit is contained in:
@@ -176,6 +176,23 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Fn>
|
||||||
|
void forEach(Fn&& fn) const
|
||||||
|
/// Iterates over all key-value pairs in the
|
||||||
|
/// cache, using a functor or lambda expression.
|
||||||
|
///
|
||||||
|
/// The given functor must take the key and value
|
||||||
|
/// as parameters. Note that the value is passed
|
||||||
|
/// as the actual value (or reference),
|
||||||
|
/// not a Poco::SharedPtr.
|
||||||
|
{
|
||||||
|
typename TMutex::ScopedLock lock(_mutex);
|
||||||
|
for (const auto& p: _data)
|
||||||
|
{
|
||||||
|
fn(p.first, *p.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mutable FIFOEvent<ValidArgs<TKey>> IsValid;
|
mutable FIFOEvent<ValidArgs<TKey>> IsValid;
|
||||||
mutable FIFOEvent<KeySet> Replace;
|
mutable FIFOEvent<KeySet> Replace;
|
||||||
|
|||||||
@@ -221,6 +221,29 @@ void LRUCacheTest::testUpdate()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void LRUCacheTest::testForEach()
|
||||||
|
{
|
||||||
|
LRUCache<int, int> aCache(3);
|
||||||
|
|
||||||
|
std::map<int, int> values;
|
||||||
|
aCache.add(1, 100);
|
||||||
|
aCache.add(2, 200);
|
||||||
|
aCache.add(3, 300);
|
||||||
|
|
||||||
|
aCache.forEach(
|
||||||
|
[&values](int key, int value)
|
||||||
|
{
|
||||||
|
values[key] = value;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals (values.size(), 3);
|
||||||
|
assertEquals (values[1], 100);
|
||||||
|
assertEquals (values[2], 200);
|
||||||
|
assertEquals (values[3], 300);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void LRUCacheTest::onUpdate(const void* pSender, const Poco::KeyValueArgs<int, int>& args)
|
void LRUCacheTest::onUpdate(const void* pSender, const Poco::KeyValueArgs<int, int>& args)
|
||||||
{
|
{
|
||||||
++updateCnt;
|
++updateCnt;
|
||||||
@@ -260,6 +283,7 @@ CppUnit::Test* LRUCacheTest::suite()
|
|||||||
CppUnit_addTest(pSuite, LRUCacheTest, testCacheSizeN);
|
CppUnit_addTest(pSuite, LRUCacheTest, testCacheSizeN);
|
||||||
CppUnit_addTest(pSuite, LRUCacheTest, testDuplicateAdd);
|
CppUnit_addTest(pSuite, LRUCacheTest, testDuplicateAdd);
|
||||||
CppUnit_addTest(pSuite, LRUCacheTest, testUpdate);
|
CppUnit_addTest(pSuite, LRUCacheTest, testUpdate);
|
||||||
|
CppUnit_addTest(pSuite, LRUCacheTest, testForEach);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ public:
|
|||||||
void testCacheSizeN();
|
void testCacheSizeN();
|
||||||
void testDuplicateAdd();
|
void testDuplicateAdd();
|
||||||
void testUpdate();
|
void testUpdate();
|
||||||
|
void testForEach();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|||||||
Reference in New Issue
Block a user