Skip to content

Instantly share code, notes, and snippets.

@p0nce
Created April 27, 2016 22:41
Show Gist options
  • Select an option

  • Save p0nce/14ee28efc5a0b096f6fad00e698576f2 to your computer and use it in GitHub Desktop.

Select an option

Save p0nce/14ee28efc5a0b096f6fad00e698576f2 to your computer and use it in GitHub Desktop.
//
// main.m
// feet
//
// Created by ponce on 24/03/2016.
// Copyright © 2016 Auburn Sounds. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <CoreAudio/CoreAudio.h>
#import <AudioUnit/AudioUnit.h>
#import <AudioToolbox/AudioToolbox.h>
#import <objc/runtime.h>
#define UseExtendedThingResource 1
#import <CoreServices/CoreServices.h>
@protocol AUCocoaUIBaseP
- (unsigned)interfaceVersion;
//- (NSView * __nullable)uiViewForAudioUnit:(AudioUnit)inAudioUnit withSize:(NSSize)inPreferredSize;
@end
@interface MyClass : NSObject <AUCocoaUIBaseP, AUCocoaUIBase>
@end
@implementation MyClass
- (unsigned)interfaceVersion
{
return 0;
}
- (NSView * __nullable)uiViewForAudioUnit:(AudioUnit)inAudioUnit withSize:(NSSize)inPreferredSize
{
return NULL;
}
@end
OBJC_EXPORT unsigned interfaceVersion(id self, SEL selector)
{
return 0;
}
int main(int argc, const char * argv[])
{
MyClass* instance = [[MyClass alloc] init];
assert(instance != NULL);
// Print type encodings
Class myClassClass = objc_getClass("MyClass");
assert(myClassClass != NULL);
Method method = class_getInstanceMethod(myClassClass, sel_registerName("interfaceVersion"));
assert(method != NULL);
printf("%s\n", method_getTypeEncoding(method));
{
struct objc_method_description desc = protocol_getMethodDescription(objc_getProtocol("AUCocoaUIBase"), sel_registerName("interfaceVersion"), YES, YES);
printf("desc.name = %s\n", desc.name);
printf("desc.types = %s\n", desc.types);
}
{
struct objc_method_description desc = protocol_getMethodDescription(objc_getProtocol("AUCocoaUIBase"), sel_registerName("uiViewForAudioUnit:withSize:"), YES, YES);
printf("desc.name = %s\n", desc.name);
printf("desc.types = %s\n", desc.types);
}
// Trying to create a class at runtime that follows AUCocoaUIBase
Class nsObject = objc_getClass("NSObject");
assert(nsObject != NULL);
Class clazz = objc_allocateClassPair(nsObject, "LOLMYCLASS", 0);
//class_addMethod(clazz, sel_registerName("interfaceVersion"), (IMP)(&interfaceVersion), "I@:");
//class_addMethod(clazz, sel_registerName("uiViewForAudioUnit:withSize:"), NULL, "@@:^v{_NSSize=dd}");
class_addMethod(clazz, sel_registerName("interfaceVersion"), (IMP)(&interfaceVersion), "I@:");
class_addMethod(clazz, sel_registerName("uiViewForAudioUnit:withSize:"), NULL, "@40@0:8^{ComponentInstanceRecord=[1q]}16{CGSize=dd}24");
Protocol* protocol = objc_getProtocol("AUCocoaUIBase");
assert(protocol != NULL);
//class_addProtocol(clazz, protocol);
objc_registerClassPair(clazz);
if (class_conformsToProtocol(clazz, protocol))
{
NSLog(@"OK, clazz follow protocol ");
}
else
{
NSLog(@"Nope, class doesn't follow protocol");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment