Skip to content

Instantly share code, notes, and snippets.

@theoknock
Last active September 20, 2022 23:46
Show Gist options
  • Save theoknock/df1bf212130ce99d55f8010f7d530ab2 to your computer and use it in GitHub Desktop.
Save theoknock/df1bf212130ce99d55f8010f7d530ab2 to your computer and use it in GitHub Desktop.
Generic function using id type (2nd edition)
typedef const typeof(id(^)(void)) retained_object;
static id (^retainable_object)(id(^)(void)) = ^ id (id(^object)(void)) {
return ^{
return object();
};
};
typeof (retained_object) *(^(^retain_object)(id (^__strong)(void)))(void) = ^ (id(^retainable_object)(void)) {
typeof(retained_object) * object_address;
object_address = &retainable_object;
typeof(retained_object) * persistent_object = (typeof(retained_object) *)CFBridgingRetain(retainable_object);
return ^ typeof(retained_object) * {
return persistent_object;
};
};
static void (^(^iterator)(const unsigned long))(id(^)(void)) = ^ (const unsigned long object_count) {
id const * retained_objects_ref[object_count];
return ^ (id const * retained_objects_t[]) {
return ^ (id(^object)(void)) {
object();
int index = 0UL;
int * index_t = &index;
for (; (*index_t) < object_count; ((*index_t) = (*index_t) + 1UL)) printf("retained_object: %p\n", (*((id * const)retained_objects_t + (~-object_count - index)) = retain_object(retainable_object(object()))));
};
}(retained_objects_ref);
};
@theoknock
Copy link
Author

Sample usage:

iterator(1000)(^ id { return (^{ printf("stored block\n"); }); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment