]> git.sesse.net Git - casparcg/blob - common/enum_class.h
- Removed need of non-deterministic sleeps during server shutdown.
[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
24 namespace caspar {
25
26 // For enum classes starting at 0 and without any gaps with a terminating count constant.
27 template <typename E>
28 const std::vector<E>& enum_constants()
29 {
30         typedef typename std::underlying_type<E>::type integer;
31
32         static const auto ints = boost::irange(static_cast<integer>(0), static_cast<integer>(E::count));
33         static const auto result = cpplinq::from(ints.begin(), ints.end())
34                 //.cast<E>()
35                 .select([](int i) { return static_cast<E>(i); })
36                 .to_vector();
37
38         return result;
39 }
40
41 }