src/basics.h

changeset 212
27259810da6d
parent 207
5315358a7d91
child 213
ee5758ddb6d2
equal deleted inserted replaced
211:b27b90fb993f 212:27259810da6d
20 #include <algorithm> 20 #include <algorithm>
21 #include <cmath> 21 #include <cmath>
22 #include <compare> 22 #include <compare>
23 #include <memory> 23 #include <memory>
24 #include <optional> 24 #include <optional>
25 #include <set>
25 #include <type_traits> 26 #include <type_traits>
26 #include <QDataStream> 27 #include <QDataStream>
27 #include <QDebug> 28 #include <QDebug>
28 #include <QObject> 29 #include <QObject>
29 #include <QPointF> 30 #include <QPointF>
116 } 117 }
117 118
118 template<typename T, typename R, typename K> 119 template<typename T, typename R, typename K>
119 R* findInMap(std::map<T, R>& map, K&& key) 120 R* findInMap(std::map<T, R>& map, K&& key)
120 { 121 {
122 static_assert(std::is_convertible_v<K, T>, "bad type for key parameter");
121 auto pair = map.find(key); 123 auto pair = map.find(key);
122 if (pair != map.end()) 124 if (pair != map.end())
123 { 125 {
124 return &pair->second; 126 return &pair->second;
125 } 127 }
302 template<typename T, glm::qualifier Q> 304 template<typename T, glm::qualifier Q>
303 constexpr unsigned int qHash(const glm::vec<3, T, Q>& key) 305 constexpr unsigned int qHash(const glm::vec<3, T, Q>& key)
304 { 306 {
305 return qHash(key.x) ^ rotl10(qHash(key.y)) ^ rotl20(qHash(key.z)); 307 return qHash(key.x) ^ rotl10(qHash(key.y)) ^ rotl20(qHash(key.z));
306 } 308 }
309
310 template<typename K, typename V, typename Fn>
311 void forValueInMap(const std::map<K, V>& map, Fn&& fn)
312 {
313 for (const auto& it : map) {
314 fn(it.second);
315 }
316 }
317
318 inline QString joined(QString value, const QString& separator, const QString& element)
319 {
320 if (not value.isEmpty()) {
321 value += separator;
322 }
323 value += element;
324 return value;
325 }

mercurial