-
Notifications
You must be signed in to change notification settings - Fork 0
/
enums.hpp
53 lines (44 loc) · 1.07 KB
/
enums.hpp
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
47
48
49
50
51
52
53
/**
*** This is a set of tools for enum class objects.
*** Provides advancing and converting enums to/from their underlying types.
**/
#pragma once
/// STL
#include <iostream>
#include <type_traits>
#include <cmath>
template <typename E, E first, E head>
void advanceEnum(E& v)
{
if(v == head)
v = first;
}
template <typename E, E first, E head, E next, E... tail>
void advanceEnum(E& v)
{
if(v == head)
v = next;
else
advanceEnum<E, first, next, tail...>(v);
}
template <typename E, E first, E... values>
struct EnumValues
{
static void advance(E& v)
{
advanceEnum<E, first, first, values...>(v);
}
};
template <typename E>
constexpr auto to_underlying(E e) -> typename std::underlying_type<E>::type
{
return static_cast<typename std::underlying_type<E>::type>(e);
}
template <typename E>
inline E from_underlying(std::istream& in)
{
static constexpr uint64_t maximum_value = std::pow(2, sizeof(E) * 8) - 1;
uint32_t tmp;
in >> tmp;
return (tmp <= maximum_value ? static_cast<E>(tmp) : E());
}