Lens C Preprocessor: getting the current class

Is there a way to get the class of the current region in the preprocessor?

I am currently doing the following:

I have a macro:

#define DATA_SOURCE_DEF_CONSTR(CLASS) + (CLASS *)dataSource { \
CLASS *source = [[[CLASS alloc] init] autorelease]; \
return source; \
}

and then I use this macro in many classes, for example:

DATA_SOURCE_DEF_CONSTR(SpecialDataSource)

I would like something like:

#define DATA_SOURCE_DEF_CONSTR + (__CLASS__ *)dataSource { \
__CLASS__ *source = [[[__CLASS__ alloc] init] autorelease]; \
return source; \
}

And call it that:

@implementation ...

DATA_SOURCE_DEF_CONSTR

...

@end

Is this possible in Objective-C with a preprocessor?

+5
source share
2 answers

I do not understand what you are trying to achieve. Why not just add a category to NSObject, for example:

@implementation NSObject (handyConstructor)

+ autoreleasedInstance { return [[[self class] alloc] init] autorelease]; }

@end

Is there a reason why you want this to be done by the preprocessor in particular?

+2
source

instancetype, ; . :

#define DATA_SOURCE_DEF_CONSTR + (instancetype)dataSource { \
return [[[[self class] alloc] init] autorelease]; \
}
+1

All Articles