OhDoh
OhDoh

Reputation: 433

Macro declaration

I have found this macro while digging in the source code of tweejump game.

#define ccp(__X__,__Y__) CGPointMake(__X__,__Y__)

For me cpp it is just alias to CGPointMake. Why is needed this ?

Upvotes: 1

Views: 236

Answers (2)

gbulmer
gbulmer

Reputation: 4290

  1. cpp is less to type, and
  2. someone could change the mapping, for example to wrap some debugging around the call

#ifndef DEBUG
#define ccp(__X__,__Y__) CGPointMake(__X__,__Y__)
#else
#define ccp(__X__,__Y__) myassert2(((__X__) > 0),((__Y__) > 0), \
CGPointMake(__X__,__Y__))
#endif

Why would we want to do that? We can get the error for the line of source code which has a bug:

#define myassert2(xc1, c2, fun)  ((c1) && (c2)? fun((c1),(c2)): \
       fprintf(stderr, "Error in file %s, line %d, c1=%d c2=%d\", \
                        __FILE__, __LINE__, (c1), (c2)) 

BTW, in general it is safer for a macro to always wrap the arguments in parenthesis:

#define ccp(__X__,__Y__) CGPointMake((__X__),(__Y__))

but as jamesdlin pointed out, in this case of a function call, it doesn't matter.

IMHO it is better to have a 'rule' that is safe, easy to explain and remember, and only vary away for strong reason, rather than have a more complex rule. After all, there are more interesting problems to solve (in my code) than whether or not to wrap a macro's arguments in parenthesis :-)

(I am especially mindful of simple rules due to a recent BBC Horizon program which offered evidence that humans can not successfully think about three things at once!-(

(HELP! What is the easy way to format C preprocessor source code like #if ...? the {} didn't work, back ticks didn't work, 4 leading spaces didn't work. They all eat some of the leading # characters)

Upvotes: 3

Richard J. Ross III
Richard J. Ross III

Reputation: 55543

It is just that: an alias for CGPointMake. it is much easier to write 3 characters than 9.

Upvotes: 3

Related Questions