c0dehunter
c0dehunter

Reputation: 6160

C++ Macro - process two pairs of parentheses

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

Answers (2)

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

OmnipotentEntity
OmnipotentEntity

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

Related Questions