Skip to content

Instantly share code, notes, and snippets.

@haikusw
Forked from jonsterling/Objectify.h
Created August 20, 2010 19:07
Show Gist options
  • Save haikusw/540933 to your computer and use it in GitHub Desktop.
Save haikusw/540933 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "to_object.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
const char *str = "wut";
id o = $<const char *>(str);
NSLog(@"char: %s, string: %@, of class: %@", str, o, [o class]);
// => char: wut, string: wut, of class: NSCFString
[pool drain];
}
#import <Foundation/Foundation.h>
template <typename T> id $(T v);
#import <objc/message.h>
#import <string>
#import <map>
#import <typeinfo>
template <typename T> id $(T v) {
std::map<std::string,NSString *> ops;
ops[typeid(int).name()] = @"NSNumber.numberWithInt:";
ops[typeid(float).name()] = @"NSNumber.numberWithFloat:";
ops[typeid(double).name()] = @"NSNumber.numberWithDouble:";
ops[typeid(const char *).name()] = @"NSString.stringWithUTF8String:";
ops[typeid(CGPoint).name()] = @"NSValue.valueWithPoint:";
ops[typeid(CGRect).name()] = @"NSValue.valueWithRect:";
ops[typeid(CGSize).name()] = @"NSValue.valueWithSize:";
ops[typeid(void *).name()] = @"NSValue.valueWithPointer:";
NSArray *components = [ops[typeid(v).name()] componentsSeparatedByString:@"."];
Class klass = NSClassFromString([components objectAtIndex:0]);
SEL sel = NSSelectorFromString([components objectAtIndex:1]);
return objc_msgSend(klass,sel,v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment