From 757710797e03d45766ce0309e69aff6627f90e31 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Sun, 20 Dec 2015 22:08:49 +0000 Subject: [PATCH] refs #545 Refactoring: extract method elementsStartingWith. --- src/blackmisc/valuecache.cpp | 4 ++-- src/blackmisc/valuecache.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/blackmisc/valuecache.cpp b/src/blackmisc/valuecache.cpp index 030335924..b73408528 100644 --- a/src/blackmisc/valuecache.cpp +++ b/src/blackmisc/valuecache.cpp @@ -117,7 +117,7 @@ namespace BlackMisc { QMutexLocker lock(&m_mutex); CVariantMap map; - for (const auto &element : makeRange(m_elements.lowerBound(keyPrefix), m_elements.lowerBound(keyPrefix + QChar(QChar::LastValidCodePoint)))) + for (const auto &element : elementsStartingWith(keyPrefix)) { implementationOf(map).insert(map.cend(), element->m_key, element->m_value); } @@ -128,7 +128,7 @@ namespace BlackMisc { QMutexLocker lock(&m_mutex); CValueCachePacket map; - for (const auto &element : makeRange(m_elements.lowerBound(keyPrefix), m_elements.lowerBound(keyPrefix + QChar(QChar::LastValidCodePoint)))) + for (const auto &element : elementsStartingWith(keyPrefix)) { map.insert(element->m_key, element->m_value, element->m_timestamp); } diff --git a/src/blackmisc/valuecache.h b/src/blackmisc/valuecache.h index 109216cf2..de345fca8 100644 --- a/src/blackmisc/valuecache.h +++ b/src/blackmisc/valuecache.h @@ -13,6 +13,7 @@ #define BLACKMISC_VALUECACHE_H #include "blackmisc/valuecacheprivate.h" +#include "blackmisc/range.h" namespace BlackMisc { @@ -160,7 +161,23 @@ namespace BlackMisc //! of CValueCache instances in all processes including this one. The slot will do its own round-trip detection. void valuesChangedByLocal(const BlackMisc::CValueCachePacket &values); + private: + struct Element; // remove forward declaration when elementsStartingWith uses C++14 auto deduced return type + protected: + //! Returns a range referring to all elements which start with the given prefix. + //! \todo Use C++14 auto deduced return type. + //! @{ + auto elementsStartingWith(const QString &keyPrefix) -> CRange>::iterator> + { + return makeRange(m_elements.lowerBound(keyPrefix), m_elements.lowerBound(keyPrefix + QChar(QChar::LastValidCodePoint))); + } + auto elementsStartingWith(const QString &keyPrefix) const -> CRange>::const_iterator> + { + return makeRange(m_elements.lowerBound(keyPrefix), m_elements.lowerBound(keyPrefix + QChar(QChar::LastValidCodePoint))); + } + //! @} + //! Save specific values to Json files in a given directory. //! \threadsafe CStatusMessage saveToFiles(const QString &directory, const CVariantMap &values) const;