AusweisApp2
FuncUtils.h
gehe zur Dokumentation dieser Datei
1 
7 #pragma once
8 
9 #include <functional>
10 #include <type_traits>
11 
12 #include <QVector>
13 
14 namespace governikus
15 {
16 
17 #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
18 
19 /*
20  * Usage example: map<Reader, QString>([](const Reader& r){ return r.getName(); }, readers)
21  *
22  * where readers has type QVector<Reader>
23  */
24 template<typename S, typename T>
25 typename std::enable_if<!std::is_void<T>::value, QVector<T>>::type map(const std::function<T(const S&)>& pFunc, const QVector<S>& pItems)
26 {
27  const auto sz = pItems.size();
28  QVector<T> result(sz);
29  for (int index = 0; index < sz; ++index)
30  {
31  result[index] = pFunc(pItems[index]);
32  }
33 
34  return result;
35 }
36 
37 
38 #endif
39 
40 
41 /*
42  * Usage example: map<Reader, QString>([](const Reader& r){ return r.getName(); }, readers)
43  *
44  * where readers has type QList<Reader>
45  */
46 template<typename S, typename T>
47 typename std::enable_if<!std::is_void<T>::value, QList<T>>::type map(const std::function<T(const S&)>& pFunc, const QList<S>& pItems)
48 {
49  const auto sz = pItems.size();
50  QList<T> result;
51  for (int index = 0; index < sz; ++index)
52  {
53  result.append(pFunc(pItems[index]));
54  }
55 
56  return result;
57 }
58 
59 
60 /*
61  * Usage example: filter<Reader>([](const Reader& r){ return r.getCard() != nullptr; }, readers)
62  *
63  * where readers has type QVector<Reader>
64  */
65 template<typename T>
66 typename std::enable_if<!std::is_void<T>::value, QVector<T>>::type filter(const std::function<bool(const T&)>& pFunc, const QVector<T>& pItems)
67 {
68  QVector<T> result;
69  for (const T& item : pItems)
70  {
71  if (pFunc(item))
72  {
73  result += item;
74  }
75  }
76 
77  return result;
78 }
79 
80 
81 } // namespace governikus
#define T(v)
Definition: http_parser.cpp:237
Implementation of ActivationContext for Intent based activation on Android systems.
Definition: ActivationContext.h:15
std::enable_if<!std::is_void< T >::value, QVector< T > >::type filter(const std::function< bool(const T &)> &pFunc, const QVector< T > &pItems)
Definition: FuncUtils.h:66
std::enable_if<!std::is_void< T >::value, QList< T > >::type map(const std::function< T(const S &)> &pFunc, const QList< S > &pItems)
Definition: FuncUtils.h:47