Reputation: 5890
I'm trying to save some time / make my code readable. I have a lot of 'isXXX' messages that return BOOL. I am constantly adding more 'is' messages. Is it possible to make a macro that's expandable at pre-compile time via a list.
I want to specify: isMacro(1, 2, 3).
And for each of those, I want the macro to expand it into a full -(BOOL)is1 {...}, -(BOOL)is2...
It seems like this would be a good use of the precompiler macro expansion, but I'm not sure how to implement the isMacro(...) part. (Specifically, the ... that expands into full functions before compile).
--- Update:
The 'is' methods are all dynamically computed, but they are all common.
I'm testing a single variable against an enum value and determining whether it's equal. I can't @synthesize them, because it's dynamic. I have all of them in a @property for convenience. I just want something that's like @synthesize, where I can list all of them, and create a dynamic response for each isXXX method.
Also, I don't want to run isCheck:(opMode)mode, since there is no pre-compile checking to make sure it's a valid enum value.
All of the is functions are in the following format:
-(BOOL) isTurtle { return operationMode == Turtle; }
The key is that I want it to function as a property for simplicity. Therefore, I don't want something like is:(opMode)mode, where I have to specify BOOL res = [obj is:Tutle];
Upvotes: 0
Views: 277
Reputation: 523274
If you could use Boost.Preprocessor, the BOOST_PP_REPEAT_FROM_TO macro should suit your need.
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#define IS_METHODS(depth, n, aux) -(BOOL)is ## n { return aux == n; }
@implementation Foo
BOOST_PP_REPEAT_FROM_TO(1, 31, IS_METHODS, operationMode)
@end
If you can't, you're out of luck. Implementing BOOST_PP_REPEAT_FROM_TO is about the same effort as just writing the 30+ functions directly.
Also I don't see how [obj isMode:12]
is really worse than obj.is12
. The former also allows a variable mode, and is less cryptic to other programmers (think about the maintenance effort).
Upvotes: 2