One could embed a list of boolean (condition results) in a POD at compile-time and switch
on it.
Usage: main.cpp
#include <iostream> /* std::cout */
#include "mswitch.h" /* mswitch, mcase */
enum state
{
STATE_1,
STATE_2,
STATE_3,
STATE_4,
STATE_5,
STATE_6,
STATE_7,
STATE_8,
};
state f(int a, bool b, const std::string& str)
{
mswitch(a >= 0, b == true, str == "morning")
{
mcase(false, false, false): return STATE_1;
mcase(false, false, true) : return STATE_2;
mcase(false, true, false) : return STATE_3;
mcase(false, true, true) : return STATE_4;
mcase(true, false, false) : return STATE_5;
mcase(true, false, true) : return STATE_6;
mcase(true, true, false) : return STATE_7;
mcase(true, true, true) : return STATE_8;
}
return STATE_1;
}
int main()
{
std::cout << "State: " << f(1, true, "morning") << std::endl;
}
Syntaxic sugar: mswitch.h
#ifndef MSWITCH_GUARD_H
#define MSWITCH_GUARD_H
#include <initializer_list>
#include <cstddef>
namespace mswitch
{
constexpr long long encode(long long value, size_t size) { return value << 6 | (0x3F & size); }
class mswitch
{
std::initializer_list<bool> _flags;
public:
mswitch(std::initializer_list<bool> const& l) : _flags(l) {}
operator long long() const
{
long long result = 0;
size_t index = 0;
for (bool b : _flags) {
result |= b << index++;
}
return encode(result, _flags.size());
}
};
template<bool head, bool... tail>
struct mcase
{
constexpr mcase() = default;
constexpr operator long long() const
{
return encode(tll(), 1+sizeof...(tail));
}
constexpr long long tll() const { return head | mcase<tail...>().tll() << 1; }
};
template<bool b>
struct mcase<b>
{
constexpr mcase() = default;
constexpr operator long long() const { return encode(tll(), 1); }
constexpr long long tll() const { return b; }
};
}
#define mswitch(head, ...) switch(mswitch::mswitch{head, __VA_ARGS__})
#define mcase(head, ...) case mswitch::mcase<head, __VA_ARGS__>()
#endif // MSWITCH_GUARD_H
Compile with g++ -std=c++14 -O2 -Wall -pedantic main.cpp
How it works
The mswitch
and mcase
objects simply build (at compile-time if possible, using constexpr
functions) a bijection between a boolean list and a switch
able long long
. Since mcase
s are given compile-time constants, all switch
labels are in fact contiguous compile-time constant themselves.