/*! * \brief Template functions that allow to map and filter over QVectors. * * \copyright Copyright (c) 2017-2018 Governikus GmbH & Co. KG, Germany */ #pragma once #include #include #include namespace governikus { /* * Usage example: map([](const Reader& r){ return r.getName(); }, readers) * * where readers has type QVector */ template typename std::enable_if::value, QVector >::type map(const std::function& pFunc, const QVector& pItems) { const int sz = pItems.size(); QVector result(sz); for (int index = 0; index < sz; ++index) { result[index] = pFunc(pItems[index]); } return result; } /* * Usage example: map([](const Reader& r){ return r.getName(); }, readers) * * where readers has type QList */ template typename std::enable_if::value, QList >::type map(const std::function& pFunc, const QList& pItems) { const int sz = pItems.size(); QList result; for (int index = 0; index < sz; ++index) { result.append(pFunc(pItems[index])); } return result; } /* * Usage example: filter([](const Reader& r){ return r.getCard() != nullptr; }, readers) * * where readers has type QVector */ template typename std::enable_if::value, QVector >::type filter(const std::function& pFunc, const QVector& pItems) { QVector result; for (const T& item : pItems) { if (pFunc(item)) { result += item; } } return result; } }