diff options
author | 2016-08-24 13:35:53 +0200 | |
---|---|---|
committer | 2016-08-24 14:40:06 +0200 | |
commit | 21e614d91a57efc2e6fcd17b5fdaedfc17057c71 (patch) | |
tree | a8afe9c67dcf5f7f71e446cc3bc92c4ffb7709b6 /src/emu/emucore.h | |
parent | 0a58a5a05e7ada5e1ba1d3c688468581ca4359d9 (diff) |
emucore: add template functions to deal with conversion between strongly typed enums and integral types
As seen in Scott Meyers' "Effective Modern C++".
Diffstat (limited to 'src/emu/emucore.h')
-rw-r--r-- | src/emu/emucore.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/emu/emucore.h b/src/emu/emucore.h index 6e80277113b..b27c8576585 100644 --- a/src/emu/emucore.h +++ b/src/emu/emucore.h @@ -346,6 +346,25 @@ inline Dest downcast(Source &src) return static_cast<Dest>(src); } +// template function which takes a strongly typed enumerator and returns its value as a compile-time constant +template <typename E> +using enable_enum_t = typename std::enable_if_t<std::is_enum<E>::value, typename std::underlying_type_t<E>>; + +template <typename E> +constexpr inline enable_enum_t<E> +underlying_value(E e) noexcept +{ + return static_cast< typename std::underlying_type<E>::type >( e ); +} + +// template function which takes an integral value and returns its representation as enumerator (even strongly typed) +template <typename E , typename T> +constexpr inline typename std::enable_if_t<std::is_enum<E>::value && std::is_integral<T>::value, E> +enum_value(T value) noexcept +{ + return static_cast<E>(value); +} + //************************************************************************** |