Created
January 10, 2015 02:51
-
-
Save chinmaygarde/9d7d4c64a7f578d1ec87 to your computer and use it in GitHub Desktop.
CFType Subclass Creation Boilerplate Code (Dash Snippet)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma mark - !!class!! structure definition | |
struct __!!class!! { | |
CFRuntimeBase _base; | |
// More stuff goes here | |
}; | |
#pragma mark - !!class!! core methods | |
static void __!!class!!Init(CFTypeRef self) { | |
} | |
static void __!!class!!Deallocate(CFTypeRef self) { | |
// Deallocate stuff | |
} | |
#pragma mark - !!class!! runtime methods | |
static CFTypeID _k!!class!!ID = _kCFRuntimeNotATypeID; | |
static CFRuntimeClass _k!!class!!Class = { | |
.version = 0, | |
.className = "!!class!!", | |
.init = __!!class!!Init, | |
.copy = NULL, | |
.finalize = __!!class!!Deallocate, | |
.equal = NULL, | |
.hash = NULL, | |
.copyFormattingDesc = NULL, | |
.copyDebugDesc = NULL, | |
}; | |
CFTypeID !!class!!GetTypeID(void) { | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_k!!class!!ID = _CFRuntimeRegisterClass((const CFRuntimeClass * const)&_k!!class!!Class); | |
}); | |
return _k!!class!!ID; | |
} | |
!!class!!Ref !!class!!Create(CFAllocatorRef allocator) { | |
uint32_t extra = sizeof(struct __!!class!!) - sizeof(CFRuntimeBase); | |
struct __!!class!! *newObject = (struct __!!class!! *)_CFRuntimeCreateInstance(allocator, !!class!!GetTypeID(), extra, NULL); | |
if (NULL == newObject) | |
return NULL; | |
return (!!class!!Ref)newObject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment