Maki
Formally verified low-code orchestration
Loading...
Searching...
No Matches
macros.h
1#pragma once
2
3#include <QModelIndex>
4#include <QString>
5
6#define UNUSED_RETURN(VALUE) static_cast<void>(VALUE)
7
8namespace maki
9{
10template <typename Container>
11std::vector<int> sortedRows(const Container& table)
12{
13 if (!table)
14 return {};
15
16 const auto selectedRows = table->selectionModel()->selectedRows();
17 if (selectedRows.empty())
18 return {};
19
20 std::vector<int> rows;
21 rows.reserve(selectedRows.size());
22 std::transform(selectedRows.cbegin(), selectedRows.cend(), std::back_inserter(rows), [](const QModelIndex& index) { return index.row(); });
23 std::sort(rows.begin(), rows.end(), std::greater<int>());
24
25 // Avoid applying the action multiple times to the same row.
26 rows.erase(std::unique(rows.begin(), rows.end()), rows.end());
27
28 return rows;
29}
30
31template <typename Container, typename Func>
32bool applyOnSortedTable(const Container& table, Func&& action)
33{
34 std::vector<int> rows = sortedRows(table);
35 if (rows.empty())
36 return false;
37
38 for (const int row : rows)
39 {
40 if (row < 0 || row >= table->rowCount())
41 continue;
42
43 std::invoke(action, row);
44 }
45
46 return true;
47}
48
49template <typename Container, typename Func>
50auto findIf(Container& container, Func&& predicate) -> std::optional<typename Container::value_type>
51{
52 const auto it = std::find_if(std::begin(container), std::end(container), std::forward<Func>(predicate));
53 if (it == std::cend(container))
54 return std::nullopt;
55
56 return *it;
57}
58
59template <typename Container, typename Func>
60auto findIf(const Container& container, Func&& predicate) -> std::optional<typename Container::value_type>
61{
62 const auto it = std::find_if(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
63 if (it == std::cend(container))
64 return std::nullopt;
65
66 return *it;
67}
68
69template <typename Container, typename Func>
70auto findIfRef(Container& container, Func&& predicate) -> typename Container::value_type*
71{
72 const auto it = std::find_if(std::begin(container), std::end(container), std::forward<Func>(predicate));
73 return it != std::end(container) ? std::addressof(*it) : nullptr;
74}
75
76template <typename Container, typename Func>
77auto findIfPtr(const Container& container, Func&& predicate) -> typename Container::value_type
78{
79 static_assert(std::is_pointer_v<typename Container::value_type>, "findIfPtr requires a container of pointers");
80 const auto it = std::find_if(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
81 return it != std::cend(container) ? *it : nullptr;
82}
83
84template <typename Container, typename Projection, typename Value>
85auto findBy(const Container& container, Projection&& projection, const Value& value) -> std::optional<typename Container::value_type>
86{
87 return findIf(container, [&](const auto& item) { return std::invoke(projection, item) == value; });
88}
89
90template <typename Container, typename Value, typename Projection>
91auto findByRef(Container& container, Projection&& projection, const Value& value) -> typename Container::value_type*
92{
93 auto it = std::find_if(std::begin(container), std::end(container), [&](const auto& item) { return std::invoke(projection, item) == value; });
94
95 return it != std::end(container) ? std::addressof(*it) : nullptr;
96}
97
98template <class T, typename Container, typename Func>
99T countIf(const Container& container, Func&& predicate)
100{
101 return std::count_if(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
102}
103
104template <class T, typename Container, typename Func>
105T accumulate(const Container& container, Func&& predicate)
106{
107 return std::accumulate(std::cbegin(container), std::cend(container), T{0}, std::forward<Func>(predicate));
108}
109
110template <typename Container, typename Func>
111bool anyOf(const Container& container, Func&& predicate)
112{
113 return std::any_of(std::cbegin(container), std::cend(container), std::forward<Func>(predicate));
114}
115
116template <typename Container1, typename Container2, typename Func>
117void transform(const Container1& source, Container2& destination, Func&& predicate)
118{
119 std::transform(cbegin(source), cend(source), std::back_inserter(destination), std::forward<Func>(predicate));
120}
121
122template <typename T>
123struct IsSharedPtr : std::false_type
124{
125};
126
127template <typename T>
128struct IsSharedPtr<std::shared_ptr<T>> : std::true_type
129{
130};
131
132template <typename Container, typename Func>
133auto takeIf(Container& container, Func&& predicate) -> typename Container::value_type
134{
135 using Value = typename Container::value_type;
136 static_assert(IsSharedPtr<Value>::value, "takeIf requires a container of shared_ptrs");
137 auto it = std::find_if(container.begin(), container.end(), std::forward<Func>(predicate));
138 if (it == container.end())
139 return nullptr;
140
141 // Transfer ownership from the container into the result.
142 auto result = std::move(*it);
143
144 // Remove the empty entry.
145 container.erase(it);
146
147 return result;
148}
149
150template <typename Container, typename Func>
151void removeIf(Container& container, Func&& predicate)
152{
153 container.erase(std::remove_if(container.begin(), container.end(), std::forward<Func>(predicate)));
154}
155
156inline bool isInt(const QString& value)
157{
158 bool ok = false;
159 UNUSED_RETURN(value.toInt(&ok));
160 return ok;
161}
162
163inline bool isUInt(const QString& value)
164{
165 bool ok = false;
166 UNUSED_RETURN(value.toUInt(&ok));
167 return ok;
168}
169
170inline bool isDouble(const QString& value)
171{
172 bool ok = false;
173 UNUSED_RETURN(value.toDouble(&ok));
174 return ok;
175}
176
177} // namespace maki
Contains UI helper widgets and layout grouping utilities used by MAKI.
Definition macros.h:9
Definition macros.h:124