I have the following struct:
struct myHandler
{
bool test1;
bool test2;
...
bool test400;
};
Which has about 400 test values. So currently I have the following function:
static inline void DoSomething(myHandler& hdler, int test, bool passed)
{
#define SETTEST(a,b,c) case b: a.test##b=c;
switch (test)
{
SETTEST(hdler, 1, passed)
SETTEST(hdler, 2, passed)
...
SETTEST(hdler, 400, passed)
}
#undef SETTEST
};
I am obviously trying to get rid of repetitive code as there are a lot of tests. So I have tried the following two options: Option1:
static inline void DoSomething(myHandler& hdler, int test, bool passed)
{
#define SETTEST(a,b,index,c) if(index==b) a.test##b=c;
for(i = 1; i <= 400; ++i)
{
SETTEST(hdler, test, i, passed)
}
#undef SETTEST
};
Obviously this does not compile, as when it concatenates using ## it takes "test" literally. Don't know how or if I can get around this.
Option2:
template<typename FUNCTION>
inline void setTest(FUNCTION f) {
for (int i = 1; i <= 5; ++i) {
f(i);
}
};
static inline void DoSomething(myHandler& hdler, int test, bool passed
{
setTest([&](const int testNum) {
if(test == testNum) {
hdler.test##testNum = passed;
}
});
};
Obviously option2 won't compile either, I don't know how to access the member of the struct using some dynamic form. Now, there are some changes to the actual struct that will solve this easily, however modifying the struct is not an option as it gets formed through some other methods, and this is just example code to portray my problem. I just want to know if there is a way to do what I am trying to achieve (without modifying the struct) that I have missed. Really I am leaning towards option 2, however from what I have read there is no way of concatenating dynamically to access a member. Any help is appreciated.