Reputation: 6160
I need to preprocess this code:
line (0,0) (5,5)
where the (0,0) means start x and y coordinate and the second (5,5) means end x and y coordinates.
I was able to fetch the start coordinates using
#define line(x1,y1) myArray->shapes.push_back(new Line(x1,y1));
How can I process the second parentheses?
Upvotes: 1
Views: 532
Reputation: 171177
How about something like the following:
struct LineCreator {
LineCreator(type_of_shapes &shapes, int x1, int y1)
: shapes_(shapes), x1_(x1), y1_(y1)
{}
void operator() (int x2, int y2) {
shapes_.push_back(new Line(x1_, y1_, x2, y2));
}
private:
type_of_shapes &shapes_;
int x1_, y1_;
};
#define line(x, y) LineCreator(myArray->shapes, (x), (y))
Upvotes: 7
Reputation: 17131
Change it to:
line (0,0,5,5)
And now you can construct the following macro:
#define line(x1,y1,x2,y2) myArray->shapes.push_back(new Line(x1,y1)); \
myArray->shapes.push_back(new Line(x2,y2));
Upvotes: 1