Skip to content

Instantly share code, notes, and snippets.

@bnham
Created August 1, 2017 16:15
Show Gist options
  • Save bnham/eb1b67fc4eb79ab4ef1aa7a34c7a2a65 to your computer and use it in GitHub Desktop.
Save bnham/eb1b67fc4eb79ab4ef1aa7a34c7a2a65 to your computer and use it in GitHub Desktop.
test1
all: test1-arc test1-no-arc
.PHONY: all
LDLIBS = -framework Foundation
CXXFLAGS = -gdwarf -std=c++11
test1-arc: test1.mm
$(CXX) -fobjc-arc $(CXXFLAGS) $(LDLIBS) -o $@ test1.mm
test1-no-arc: test1.mm
$(CXX) -fno-objc-arc $(CXXFLAGS) $(LDLIBS) -o $@ test1.mm
clean:
rm -rf test1-arc* test1-no-arc*
.PHONY: clean
#import <Foundation/Foundation.h>
class ObjectWrapper {
public:
ObjectWrapper(id obj): obj_(obj) {}
id get() { return obj_; }
void set(id obj) { obj_ = obj; }
private:
id obj_;
};
@interface Foo : NSObject
+ (ObjectWrapper)foo;
@end
@implementation Foo
+ (ObjectWrapper)foo {
const char *status = (self == [Foo class]) ? "CORRECT" : "INCORRECT";
NSLog(@"%s: receiver is %s", __func__, status);
return ObjectWrapper{[[NSObject alloc] init]};
}
@end
static void callDirectly() {
[Foo foo];
}
static void callViaInvocation() {
SEL sel = @selector(foo);
NSMethodSignature *sig = [Foo methodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
invocation.target = [Foo class];
invocation.selector = sel;
[invocation invoke];
}
int main() {
@autoreleasepool {
callDirectly();
callViaInvocation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment