summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qiterable.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-09-03 13:41:39 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-09-12 09:12:26 +0200
commitef93fdeb098f78a0afe1df72c53ba26bf7a11c60 (patch)
tree4862c44b7013f5965ba6c888d1059d24dfb5d87a /src/corelib/kernel/qiterable.cpp
parent73fe229eb434236402ec1685b1f17f96417001b9 (diff)
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 <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/kernel/qiterable.cpp')
-rw-r--r--src/corelib/kernel/qiterable.cpp67
1 files changed, 67 insertions, 0 deletions
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
@@ -134,6 +134,73 @@ qsizetype QSequentialIterable::size() const
}
/*!
+ * 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<QVariant>()) {
+ 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
const_iterator of the container.