I have an interface that has ~16 input field boxes. They are all declared as public pointers within a class, initialized, etc. However, as my code has grown more and more with private functions doing database committals, error checking, temporary storage routines, etc it has become very painful if a field has to get removed or a new one added I must delve into all these private functions and explicitly remove/add the field; and always with regard to the field ordering.
There has to be a simpler way!
This is my idea and am hoping anyone can shoot it down or build upon it:
My thought is to store pointers to all the input fields in a array of pointers and then all these private helper functions walk the array; however some of these private functions are static some are non-static; thus some more pointer magic is required, or should I have two of these array-of-pointer functions: one for static functions to use and one for non-static functions to use?
To further complicate things, the method invoked upon the widgets varies depending on what the private function is doing… Some may call “->value(foo)” some may call “->location(1),->location(2),” incrementing in order of the widgets. Is there a way to pass the method invoked and the parameters to be passed to this new helper function containing the array of input field pointers?
Food for thought:
Maybe I’m trying to get too fancy by saving myself the burden of scrolling all around my code whenever I need to make a change? Maybe this will add too much overhead with all the extra pointer indirection? Is it better to suffer?
Thanks, any help is appreciated. If some code examples are really required I can churn out some.
Code Example (this won’t compile and is typed freehand as an example)
class Foo
{
public:
InputBox * in1;
InputBox * in2;
InputBox * in3;
ExternalDataSource * exds; // Pretend this object you can retrieve values out of
private:
static void clearFieldsFunc1(void * v); // callback bound to a button
static void loadFieldFunc2(void * v); // callback bound to a button
void printFieldsFunc3(); // not a callback, just called from various functions
}
Foo::Foo()
{
in1= new InputBox (0,0,10,10); // Box x,y,w,h
in2= new InputBox (15,0,10,10);
in3= new InputBox (30,0,10,10);
exds = new ExernalDataSource("US Intelligence Agency");
}
// Clears the fields
void Foo::clearFieldsFunc1(void * v)
{
Foo * fptr = ((Foo*)v);
fptr->in1->clear();
fptr->in2->clear();
fptr->in3->clear();
}
// Loads the fields
void Foo::loadFieldFunc2(void * v)
{
Foo * fptr = ((Foo*)v);
fptr->in1->value(fptr->exds->getValue(1));
fptr->in2->value(fptr->exds->getValue(2));
fptr->in3->value(fptr->exds->getValue(3));
}
// Prints the fields
void Foo::printFieldsFunc3()
{
printf("%s\n",this->in1->value());
printf("%s\n",this->in2->value());
printf("%s\n",this->in3->value());
}
You could possible add a container of
InputBoxas a member toFooand iterate it to make life simpler.