I’ve got a struct for defining the graphics of a feature in my 2D engine. In that, there are a series of SDL_Rects called “middle, left_edge, top_left_corner” etc. In part of my code, I’m initialising them off a series of arguments types into the scripting engine/command line, that are stored as a vector of std::strings.
As a matter of style, and avoiding stupid mistakes due to repetition (and having to correct any mistakes 9 times), is there any way to clean up this code? Or would this be reasonable?
//RectZeroes is just an SDL_Rect of {0,0,0,0} to ensure that it is initialised
SDL_Rect middle = RectZeroes;
if (args.size() >= 6 )
{
middle.x = boost::lexical_cast<int>(args[3]);
middle.y = boost::lexical_cast<int>(args[4]);
middle.w = boost::lexical_cast<int>(args[5]);
middle.h = boost::lexical_cast<int>(args[6]);
}
SDL_Rect left_edge = RectZeroes;
if (args.size() >= 10 )
{
left_edge.x = boost::lexical_cast<int>(args[7]);
left_edge.y = boost::lexical_cast<int>(args[8]);
left_edge.w = boost::lexical_cast<int>(args[9]);
left_edge.h = boost::lexical_cast<int>(args[10]);
}
//And so on
I agree that this isn’t a pleasant code. To avoid the repetition, do the same as always in programming: encapsulate – in this case, encapsulate the structure and giving it a proper constructor, and write a thin wrapper around
boost::lexical_cast<int>(x).Or, if you want to avoid wrapping
SDL_Rectthen write an initialiser function:And then:
Better yet, make the initialiser return the constructed structure. This makes it even more convenient to use (namely, in an initialiser):