Re: case/switch metaprogramming
OK, I figured it out. Using Victor's code as a starting point, I
added the ability to add/remove individual cases automatically:
#include <iostream>
using namespace std;
struct Dev {
static bool const is_supported = true;
virtual ~Dev() {}
};
template <int id> struct Device {
static bool const is_supported = false;
};
template <> struct Device<0> : public Dev {
virtual ~Device() { cout << 0 << endl; }
};
template <> struct Device<99> : public Dev {
virtual ~Device() { cout << 99 << endl; }
};
// ----- now for the switch/case statement -----
template <bool is_supported, int n> struct device_selector;
template <int n>
struct device_selector<true, n> { // supported devices
Dev* operator()(int const id) {
if (n == id) return new Device<n>();
device_selector<Device<n-1>::is_supported, n-1> s;
return s(id);
}
};
template <int n>
struct device_selector<false, n> { // unsupported devices
Dev* operator()(int const id) {
device_selector<Device<n-1>::is_supported, n-1> s;
return s(id);
}
};
template <>
struct device_selector<true, 0> { // default case is supported
Dev* operator()(int const id) {
return new Device<0>();
}
};
Dev* get_device(int const id) {
int const high = 240;
device_selector<Device<high>::is_supported, high> s;
return s(id);
}
// ----- end of switch/case statement ---
#include <iostream>
int main() {
delete get_device(99); // prints "99"
delete get_device(12); // prints "0"
}
Now the only thing I need to do in order to add something to the
switch statement is implement it! (And make sure the declaraion is
#included, of course.)
The default template depth of the gcc I'm currently using is 500, and
that can be increased with a command-line switch. I would expect any
good compiler to support several hundred by default at least, and to
provide a mechanism for supporting many more.
Additionally, it looks like gcc optimizes this even better than the
explicit switch/case at -O3. In fact, the generated code is
completely flat! It's all inlined, so there is essentially no extra
runtime cost.
Very nice. Now if I could use function partial specialization, this
code would be even more concise.
Aaron
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]