]> git.sesse.net Git - casparcg/blob - common/enum_class.h
Created abstraction for property tree get(), get_child() and get_value() to be able...
[casparcg] / common / enum_class.h
1 #pragma once
2
3 #include <type_traits>
4
5 #include <boost/range/irange.hpp>
6
7 #include "linq.h"
8
9 // Macro that defines & and &= for an enum class. Add more when needed.
10
11 #define ENUM_ENABLE_BITWISE(enum_class) \
12         static enum_class operator&(enum_class lhs, enum_class rhs) \
13         { \
14                 return static_cast<enum_class>( \
15                                 static_cast<std::underlying_type<enum_class>::type>(lhs) \
16                                         & static_cast<std::underlying_type<enum_class>::type>(rhs)); \
17         }; \
18         static enum_class& operator&=(enum_class& lhs, enum_class rhs) \
19         { \
20                 lhs = lhs & rhs; \
21                 return lhs; \
22         }; \
23         static enum_class operator | (enum_class lhs, enum_class rhs) \\r
24         { \\r
25                 return static_cast<enum_class>( \\r
26                                 static_cast<std::underlying_type<enum_class>::type>(lhs) \\r
27                                         | static_cast<std::underlying_type<enum_class>::type>(rhs)); \\r
28         }; \\r
29         static enum_class& operator|=(enum_class& lhs, enum_class rhs) \
30         { \
31                 lhs = lhs | rhs; \
32                 return lhs; \
33         }; \
34         static enum_class operator ^ (enum_class lhs, enum_class rhs) \\r
35         { \\r
36                 return static_cast<enum_class>( \\r
37                                 static_cast<std::underlying_type<enum_class>::type>(lhs) \\r
38                                         ^ static_cast<std::underlying_type<enum_class>::type>(rhs)); \\r
39         }; \\r
40         static enum_class& operator^=(enum_class& lhs, enum_class rhs) \
41         { \
42                 lhs = lhs ^ rhs; \
43                 return lhs; \
44         };
45
46 namespace caspar {
47
48 // For enum classes starting at 0 and without any gaps with a terminating count constant.
49 template <typename E>
50 const std::vector<E>& enum_constants()
51 {
52         typedef typename std::underlying_type<E>::type integer;
53
54         static const auto ints = boost::irange(static_cast<integer>(0), static_cast<integer>(E::count));
55         static const auto result = cpplinq::from(ints.begin(), ints.end())
56                 //.cast<E>()
57                 .select([](int i) { return static_cast<E>(i); })
58                 .to_vector();
59
60         return result;
61 }
62
63 }