I have a C application which has a large amount of highly fragmented configuration data and behavior that I would like to consolidate, eg:
// In one file
#define VAR_1 1
#define VAR_2 2
#define VAR_3 3
// In another file
const gNames[] = {
[VAR_1] = (const u8[]) "1",
[VAR_2] = (const u8[]) "2",
[VAR_3] = (const u8[]) "3",
};
// In yet another file
switch (var) {
case VAR_1:
// some behavior
break;
case VAR_3:
// some other behavior
break;
// And so on
I'd like to consolidate this data, eg:
const Behavior gBehaviors[] = {
[VAR_1] = {
.name = (const u8[]) "1",
.handleFooEvent = Var1FooHandler,
// ...
},
// ...
However as far as I can tell the inability to declare anonymous methods in C will make this pretty miserable as I wont be able to keep handlers and constants grouped together. I'm not particularly experienced with C++, but as far as I can tell there are a number of ways in which I can cleanly declare the individual behavior classes I need, however as C++ doesn't seem to have support for designated identifiers and I need to maintain indexing that matches with our existing #define enum I'm not sure how I can actually create a mapping that allows the C code to go from an enum to the appropriate behavior. Is there a reasonable way that I could make this work, either in C, C++, or even some other language/technology?
(const u8[])isn't valid C. I don't think it is valid C++ either unless some strange operator overloading was done?