Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created May 3, 2014 18:56
Show Gist options
  • Save mikeash/87986e9f32d72fe56efd to your computer and use it in GitHub Desktop.
Save mikeash/87986e9f32d72fe56efd to your computer and use it in GitHub Desktop.
#if OBJC
@interface MyThingy : NSObject {
#else
struct MyThingy {
#endif
int field1;
char *field2;
#if OBJC
}
@end
#else
};
typedef struct MyThingy MyThingy;
#endif
#if OBJC
@interface MyThingy ()
- (void)method1;
- (void)method2;
@end
#endif
MyThingy *CreateMyThingy(void) {
#if OBJC
return [[MyThingy alloc] init];
#else
return calloc(1, sizeof(MyThingy));
#endif
}
void FreeMyThingy(MyThingy *thingy) {
#if OBJC
[thingy release]; // or just nothing for ARC
#else
InternalThingyCleanup(thingy);
free(thingy);
}
static void InternalThingyCleanup(MyThingy *thingy) {
free(thingy->field2);
// and any other field cleanup that's needed
}
#if OBJC
@implementation MyThingy
- (void)dealloc {
InternalThingyCleanup(self);
[super dealloc]; // or nothing for ARC
}
- (void)method1 {
ThingyFunction1(self);
}
- (void)method2 {
ThingyFunction2(self);
}
@end
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment