I am working on my c++ library for microcontrollers and I need a way to specify different enum class content based on the template type.
I have this code so far:
#include <cstdint>
#include <type_traits>
enum class timer_e {
timer1,
timer2
};
template<typename>
struct is_basic_timer : std::false_type {};
template<>
struct is_basic_timer<timer_e::timer1> : std::true_type {};
template<typename>
struct is_advanced_timer : std::false_type {};
template<>
struct is_advanced_timer<timer_e::timer2> : std::true_type {};
template<timer_e T>
class Timer {
public:
// Prototype for options enum.
enum class options;
typename std::enable_if<is_basic_timer<T>::value, decltype(options)>::type options : std::uint32_t {
basic_option_1,
basic_option_2
};
typename std::enable_if<is_advanced_timer<T>::value, decltype(options)>::type options : std::uint32_t {
basic_option1,
basic_option2,
advanced_option1,
advanced_option2
};
static void enableOption(options o) {
SFR |= static_cast<std::uint32_t> (o);
}
static void disableOptions(options o) {
SFR &= ~static_cast<std::uint32_t> (o);
}
private:
Timer() = delete;
};
The Timer class is supposed to be only used statically. I wish to modify the options enum contents based on the template type to avoid unsupported options being set in the special function register (SFR). The above code is my best attempt, but the compiler does not like my use of decltype.
Is there a way to declare different enum class content based on template type via the type traits as defined above?