From 7c8aa8226dd1549c60e961d20168d325358ee05b Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Tue, 28 Jan 2014 20:19:06 +0000 Subject: [PATCH] const corrections --- src/blackmisc/collection.h | 5 +++-- src/blackmisc/containerbase.h | 10 +++++----- src/blackmisc/sequence.h | 16 +++++++++++----- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/blackmisc/collection.h b/src/blackmisc/collection.h index d5411c687..4a435f575 100644 --- a/src/blackmisc/collection.h +++ b/src/blackmisc/collection.h @@ -110,12 +110,12 @@ namespace BlackMisc iterator end() { return pimpl() ? pimpl()->end() : iterator(); } /*! - * \brief Returns iterator one past the end of the collection. + * \brief Returns const iterator one past the end of the collection. */ const_iterator end() const { return pimpl() ? pimpl()->end() : const_iterator(); } /*! - * \brief Returns iterator one past the end of the collection. + * \brief Returns const iterator one past the end of the collection. */ const_iterator cend() const { return pimpl() ? pimpl()->cend() : const_iterator(); } @@ -176,6 +176,7 @@ namespace BlackMisc * \brief Efficient find method using the find of the implementation container. Typically O(log n). * \return An iterator to the position of the found element, or the end iterator if not found. * \pre The sequence must be initialized. + * \warning Take care that the returned non-const iterator is not compared with a const iterator. */ iterator find(const T &value) { Q_ASSERT(pimpl()); return pimpl()->find(value); } diff --git a/src/blackmisc/containerbase.h b/src/blackmisc/containerbase.h index 8f14dc6db..8aac09834 100644 --- a/src/blackmisc/containerbase.h +++ b/src/blackmisc/containerbase.h @@ -91,7 +91,7 @@ namespace BlackMisc template bool contains(Predicate p) const { - return std::any_of(derived().begin(), derived().end(), p); + return std::any_of(derived().cbegin(), derived().cend(), p); } /*! @@ -99,7 +99,7 @@ namespace BlackMisc */ bool contains(const T &object) const { - return derived().find(object) != derived().end(); + return derived().find(object) != derived().cend(); } /*! @@ -156,7 +156,7 @@ namespace BlackMisc { QString str; // qualifying stringify with this-> to workaround bug in GCC 4.7.2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56402 - std::for_each(derived().begin(), derived().end(), [ & ](const T &value) { str += (str.isEmpty() ? "{" : ",") + this->stringify(value, i18n); }); + std::for_each(derived().cbegin(), derived().cend(), [ & ](const T &value) { str += (str.isEmpty() ? "{" : ",") + this->stringify(value, i18n); }); if (str.isEmpty()) { str = "{"; } return str += "}"; } @@ -183,7 +183,7 @@ namespace BlackMisc //const auto &o = static_cast(other); //if (derived().size() < o.derived().size()) { return -1; } //if (derived().size() > o.derived().size()) { return 1; } - //for (auto i1 = derived().begin(), i2 = o.derived().begin(); i1 != derived().end() && i2 != o.derived().end(); ++i1, ++i2) + //for (auto i1 = derived().cbegin(), i2 = o.derived().cbegin(); i1 != derived().cend() && i2 != o.derived().cend(); ++i1, ++i2) //{ // if (*i1 < *i2) { return -1; } // if (*i1 > *i2) { return 1; } @@ -194,7 +194,7 @@ namespace BlackMisc virtual void marshallToDbus(QDBusArgument &argument) const { argument.beginArray(qMetaTypeId()); - std::for_each(derived().begin(), derived().end(), [ & ](const T &value) { argument << value; }); + std::for_each(derived().cbegin(), derived().cend(), [ & ](const T &value) { argument << value; }); argument.endArray(); } diff --git a/src/blackmisc/sequence.h b/src/blackmisc/sequence.h index c7c21acd6..95a2e7a48 100644 --- a/src/blackmisc/sequence.h +++ b/src/blackmisc/sequence.h @@ -95,12 +95,12 @@ namespace BlackMisc iterator begin() { return pimpl() ? pimpl()->begin() : iterator(); } /*! - * \brief Returns iterator at the beginning of the sequence. + * \brief Returns const iterator at the beginning of the sequence. */ const_iterator begin() const { return pimpl() ? pimpl()->begin() : const_iterator(); } /*! - * \brief Returns iterator at the beginning of the sequence. + * \brief Returns const iterator at the beginning of the sequence. */ const_iterator cbegin() const { return pimpl() ? pimpl()->cbegin() : const_iterator(); } @@ -110,12 +110,12 @@ namespace BlackMisc iterator end() { return pimpl() ? pimpl()->end() : iterator(); } /*! - * \brief Returns iterator one past the end of the sequence. + * \brief Returns const iterator one past the end of the sequence. */ const_iterator end() const { return pimpl() ? pimpl()->end() : const_iterator(); } /*! - * \brief Returns iterator one past the end of the sequence. + * \brief Returns const iterator one past the end of the sequence. */ const_iterator cend() const { return pimpl() ? pimpl()->cend() : const_iterator(); } @@ -221,8 +221,14 @@ namespace BlackMisc /*! * \brief Return an iterator to the first element equal to the given object, or the end iterator if not found. O(n). + * \warning Take care that the returned non-const iterator is not compared with a const iterator. */ - iterator find(const T &object) const { return std::find(begin(), end(), object); } + iterator find(const T &object) { return std::find(begin(), end(), object); } + + /*! + * \brief Return an iterator to the first element equal to the given object, or the end iterator if not found. O(n). + */ + const_iterator find(const T &object) const { return std::find(cbegin(), cend(), object); } /*! * \brief Modify by applying a value map to each element for which a given predicate returns true.