J
Dies hier ist Lösung meines Problems. Nicht besonders elegant, aber es funktioniert. Mein Compiler unterstützt kein C++11, daher kann ich keine initializer lists, auto etc, benutzen welches den code vereinfachen würde. Dank nochmal für die Hilfe und kritische Fragestellungen
#include <iostream>
struct action
{
int counter_limit;
void (*transition)(void);
};
struct action_list
{
size_t size;
const action *table;
};
class ButtonHandler {
public:
ButtonHandler(std::size_t table_size, const action* begin) {
mTable.size = table_size;
mTable.table = begin;
}
void operator()(int counter) const {
const action *a = mTable.table;
for (std::size_t i=mTable.size; i; --i, ++a) {
if (counter < a->counter_limit) {
a->transition();
return;
}
}
}
private:
action_list mTable;
};
void state1() {
//std::cout << __func__;
printf("state1");
}
void state4() {
//std::cout << __func__;
printf("state4");
}
void state8() {
//std::cout << __func__;
printf("state8");
}
const action action_table[] =
{
{ 4, state1 },
{ 8, state4 },
{ 11, state8 },
};
int main()
{
const ButtonHandler btn_h(
sizeof(action_table)/sizeof(action_table[0]),
action_table
);
for (int counter = 0 ; counter < 11; ++counter)
btn_h(counter);
}