-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
46 lines (40 loc) · 1.17 KB
/
utils.h
File metadata and controls
46 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef UTILS_H
#define UTILS_H
#include <string>
#include <algorithm>
#include <cstdarg>
#include <array>
#include <string_view>
template<class T>
struct NameValuePair {
using value_type = T;
const T value;
const char* const name;
};
template<class Mapping, class V>
static std::string_view getNameForValue(Mapping a, V value) {
auto pos = std::find_if(std::begin(a), std::end(a), [&value](const typename Mapping::value_type& t){
return (t.value == value);
});
return pos->name;
}
template <typename Key, typename Value, std::size_t Size>
struct Map {
std::array<std::pair<Key, Value>, Size> data;
[[nodiscard]] constexpr Value at(const Key &key) const {
const auto itr =
std::find_if(begin(data), end(data),
[&key](const auto &v) { return v.first == key; });
return itr->second;
};
[[nodiscard]] constexpr bool find(const Key &key) const {
const auto itr =
std::find_if(begin(data), end(data),
[&key](const auto &v) { return v.first == key; });
if (itr != end(data)) {
return true;
}
return false;
};
};
#endif // UTILS_H