]> git.saurik.com Git - apple/objc4.git/blob - test/accessors.m
objc4-680.tar.gz
[apple/objc4.git] / test / accessors.m
1 // TEST_CFLAGS -framework Foundation
2
3 #import <Foundation/Foundation.h>
4 #import <objc/runtime.h>
5 #import <objc/objc-abi.h>
6 #include "test.h"
7
8 @interface Test : NSObject {
9 NSString *_value;
10 // _object is at the last optimized property offset
11 id _object __attribute__((aligned(64)));
12 }
13 @property(readonly) Class cls;
14 @property(copy) NSString *value;
15 @property(assign) id object;
16 @end
17
18 typedef struct {
19 void *isa;
20 void *_value;
21 // _object is at the last optimized property offset
22 void *_object __attribute__((aligned(64)));
23 } TestDefs;
24
25 @implementation Test
26
27 // Question: why can't this code be automatically generated?
28
29 #if !__has_feature(objc_arc)
30 - (void)dealloc {
31 self.value = nil;
32 self.object = nil;
33 [super dealloc];
34 }
35 #endif
36
37 - (Class)cls { return objc_getProperty(self, _cmd, 0, YES); }
38
39 - (NSString*)value { return (NSString*) objc_getProperty(self, _cmd, offsetof(TestDefs, _value), YES); }
40 - (void)setValue:(NSString*)inValue { objc_setProperty(self, _cmd, offsetof(TestDefs, _value), inValue, YES, YES); }
41
42 - (id)object { return objc_getProperty(self, _cmd, offsetof(TestDefs, _object), YES); }
43 - (void)setObject:(id)inObject { objc_setProperty(self, _cmd, offsetof(TestDefs, _object), inObject, YES, NO); }
44
45 - (NSString *)description {
46 return [NSString stringWithFormat:@"value = %@, object = %@", self.value, self.object];
47 }
48
49 @end
50
51 int main() {
52 PUSH_POOL {
53
54 NSMutableString *value = [NSMutableString stringWithUTF8String:"test"];
55 id object = [NSNumber numberWithInt:11];
56 Test *t = AUTORELEASE([Test new]);
57 t.value = value;
58 [value setString:@"yuck"]; // mutate the string.
59 testassert(t.value != value); // must copy, since it was mutable.
60 testassert([t.value isEqualToString:@"test"]);
61
62 Class testClass = [Test class];
63 Class cls = t.cls;
64 testassert(testClass == cls);
65 cls = t.cls;
66 testassert(testClass == cls);
67
68 t.object = object;
69 t.object = object;
70
71 // NSLog(@"t.object = %@, t.value = %@", t.object, t.value);
72 // NSLog(@"t.object = %@, t.value = %@", t.object, t.value); // second call will optimized getters.
73
74 } POP_POOL;
75
76 succeed(__FILE__);
77
78 return 0;
79 }