From ef93fdeb098f78a0afe1df72c53ba26bf7a11c60 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 3 Sep 2020 13:41:39 +0200 Subject: Provide methods for adding values to a sequential iterable Provide functionality to add and remove values, so that you can use a sequential iterable as stack or queue if the underlying container supports this. To this end, provide a way to specify whether the value should be added or removed at the beginning or the end of the iterable. Change-Id: If63d302f3ca085e56d601116ce4dfaa6b94a0c4f Reviewed-by: Edward Welbourne --- src/corelib/kernel/qiterable.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'src/corelib/kernel/qiterable.cpp') diff --git a/src/corelib/kernel/qiterable.cpp b/src/corelib/kernel/qiterable.cpp index dd7f194686..5aed1832af 100644 --- a/src/corelib/kernel/qiterable.cpp +++ b/src/corelib/kernel/qiterable.cpp @@ -133,6 +133,73 @@ qsizetype QSequentialIterable::size() const return size; } +/*! + * Adds \a value to the container, at \a position, if possible. + */ +void QSequentialIterable::addValue(const QVariant &value, Position position) +{ + QVariant converted; + const void *valuePtr; + if (valueMetaType() == QMetaType::fromType()) { + valuePtr = &value; + } else if (valueMetaType() == value.metaType()) { + valuePtr = value.constData(); + } else if (value.canConvert(valueMetaType())) { + converted = value; + converted.convert(valueMetaType()); + valuePtr = converted.constData(); + } else { + converted = QVariant(valueMetaType()); + valuePtr = converted.constData(); + } + + switch (position) { + case AtBegin: + if (metaSequence().canAddValueAtBegin()) + metaSequence().addValueAtBegin(mutableIterable(), valuePtr); + break; + case AtEnd: + if (metaSequence().canAddValueAtEnd()) + metaSequence().addValueAtEnd(mutableIterable(), valuePtr); + break; + case Unspecified: + if (metaSequence().canAddValue()) + metaSequence().addValue(mutableIterable(), valuePtr); + break; + } +} + +/*! + * Removes a value from the container, at \a position, if possible. + */ +void QSequentialIterable::removeValue(Position position) +{ + switch (position) { + case AtBegin: + if (metaSequence().canRemoveValueAtBegin()) + metaSequence().removeValueAtBegin(mutableIterable()); + break; + case AtEnd: + if (metaSequence().canRemoveValueAtEnd()) + metaSequence().removeValueAtEnd(mutableIterable()); + break; + case Unspecified: + if (metaSequence().canRemoveValue()) + metaSequence().removeValue(mutableIterable()); + break; + } +} + +/*! + Returns whether it is possible to iterate over the container in forward + direction. This corresponds to the std::forward_iterator_tag iterator trait + of the iterator and const_iterator of the container. +*/ +bool QSequentialIterable::canForwardIterate() const +{ + return m_metaSequence.hasForwardIterator(); +} + /*! Returns whether it is possible to iterate over the container in reverse. This corresponds to the std::bidirectional_iterator_tag iterator trait of the -- cgit v1.2.3