From 2453d20dd01b58cfebc423e1d1f3c0d3addc0def Mon Sep 17 00:00:00 2001 From: David Schulz Date: Tue, 14 Nov 2017 09:26:38 +0100 Subject: [PATCH] Utils: add filter and transform function for qobject container Add function that casts QObjects of a given container to a desired type and return the result via the same container with the desired value type. Change-Id: I45949c982c8dbdd0cce069d62cd2db5c8517f9f5 Reviewed-by: Eike Ziller --- src/libs/utils/algorithm.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libs/utils/algorithm.h b/src/libs/utils/algorithm.h index f24ad579770..3d0b9a6acb4 100644 --- a/src/libs/utils/algorithm.h +++ b/src/libs/utils/algorithm.h @@ -32,6 +32,7 @@ #include #include +#include #include namespace Utils @@ -371,6 +372,21 @@ C filteredUnique(const C &container) return result; } +////////////////// +// qobject_container_cast +///////////////// +template class Container, typename Base> +Container qobject_container_cast(const Container &container) +{ + Container result; + auto ins = inserter(result); + for (Base val : container) { + if (T target = qobject_cast(val)) + ins = target; + } + return result; +} + ////////////////// // sort /////////////////