I have the following C++11 code in my microcontroller project:
template<std::uint32_t... I>
struct mask_or;
template<>
struct mask_or<> {
static constexpr std::uint32_t value = 0;
};
template<std::uint32_t first, std::uint32_t... rest>
struct mask_or<first, rest...> {
static constexpr std::uint32_t value = first | mask_or<rest...>::value;
};
This works fine, and allows me to pass a variable amount of uint32_t's as template arguments. The compiler will then OR all of them and substitute each call to a constant value. For the microcontroller, this is ideal because it does not have to do OR operations before assigning to the register.
For some cases, I want to use an enum class like below as values:
enum class gpiopin : std::uint32_t {
p0 = GPIO_IDR_IDR_0, // 0x00000001
...
p14 = GPIO_IDR_IDR_14, // 0x00004000
p15 = GPIO_IDR_IDR_15 // 0x00008000
};
As I have multiple of these enum classes, I am looking for a generic way of using the enum class values in the above mask_or code. In summary, I want to be able to do this:
SFR = mask_or<gpiopin::p1, gpiopin::p14>::value;
Ideally, I would like the mask_or<...>::value to be constexpr to keep the code size low and speed high.
I feel like this should be possible, but I cannot seem to make it work. Can anyone help me?