Skip to content
Newer
Older
100644 181 lines (144 sloc) 3.94 KB
dc12427 @mikeash initial commit with roughed-out API start
authored Sep 14, 2010
1
2 #import "MARTNSObject.h"
3
05ba39b @mikeash implement rt_subclasses
authored Sep 14, 2010
4 #import <objc/runtime.h>
dc12427 @mikeash initial commit with roughed-out API start
authored Sep 14, 2010
5
73735e9 @lemnar add protocols query methods to NSObject category
lemnar authored Sep 24, 2010
6 #import "RTProtocol.h"
ac90912 @mikeash add ivar methods
authored Sep 15, 2010
7 #import "RTIvar.h"
f5f0d31 @lemnar add property query functions to NSObject category
lemnar authored Sep 24, 2010
8 #import "RTProperty.h"
c90eafb @mikeash add basic method querying
authored Sep 14, 2010
9 #import "RTMethod.h"
ac90912 @mikeash add ivar methods
authored Sep 15, 2010
10 #import "RTUnregisteredClass.h"
c90eafb @mikeash add basic method querying
authored Sep 14, 2010
11
dc12427 @mikeash initial commit with roughed-out API start
authored Sep 14, 2010
12
05ba39b @mikeash implement rt_subclasses
authored Sep 14, 2010
13 @implementation NSObject (MARuntime)
dc12427 @mikeash initial commit with roughed-out API start
authored Sep 14, 2010
14
05ba39b @mikeash implement rt_subclasses
authored Sep 14, 2010
15 + (NSArray *)rt_subclasses
16 {
17 Class *buffer = NULL;
18
19 int count, size;
20 do
21 {
22 count = objc_getClassList(NULL, 0);
23 buffer = realloc(buffer, count * sizeof(*buffer));
24 size = objc_getClassList(buffer, count);
25 } while(size != count);
26
27 NSMutableArray *array = [NSMutableArray array];
28 for(int i = 0; i < count; i++)
29 {
30 Class candidate = buffer[i];
31 Class superclass = candidate;
32 while(superclass)
33 {
34 if(superclass == self)
35 {
36 [array addObject: candidate];
37 break;
38 }
39 superclass = class_getSuperclass(superclass);
40 }
41 }
42 free(buffer);
43 return array;
44 }
dc12427 @mikeash initial commit with roughed-out API start
authored Sep 14, 2010
45
ac90912 @mikeash add ivar methods
authored Sep 15, 2010
46 + (RTUnregisteredClass *)rt_createUnregisteredSubclassNamed: (NSString *)name
52e76e0 @mikeash implement subclass creation
authored Sep 14, 2010
47 {
622a59d @lemnar move unregistered class allocator into initializer
lemnar authored Sep 28, 2010
48 return [RTUnregisteredClass unregisteredClassWithName: name withSuperclass: self];
ac90912 @mikeash add ivar methods
authored Sep 15, 2010
49 }
50
51 + (Class)rt_createSubclassNamed: (NSString *)name
52 {
53 return [[self rt_createUnregisteredSubclassNamed: name] registerClass];
52e76e0 @mikeash implement subclass creation
authored Sep 14, 2010
54 }
55
56 + (void)rt_destroyClass
57 {
58 objc_disposeClassPair(self);
59 }
60
ddded36 @mikeash add metaclass code
authored Sep 14, 2010
61 + (BOOL)rt_isMetaClass
62 {
63 return class_isMetaClass(self);
64 }
65
6b2d831 @mikeash add set superclass method
authored Sep 14, 2010
66 #ifdef __clang__
67 #pragma clang diagnostic push
68 #endif
69 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
70 + (Class)rt_setSuperclass: (Class)newSuperclass
71 {
44f30b7 @mikeash fix missing return statement in rt_setSuperclass:
authored Sep 18, 2010
72 return class_setSuperclass(self, newSuperclass);
6b2d831 @mikeash add set superclass method
authored Sep 14, 2010
73 }
74 #ifdef __clang__
75 #pragma clang diagnostic pop
76 #endif
77
29f91d8 @mikeash add instance size method
authored Sep 14, 2010
78 + (size_t)rt_instanceSize
79 {
80 return class_getInstanceSize(self);
81 }
82
73735e9 @lemnar add protocols query methods to NSObject category
lemnar authored Sep 24, 2010
83 + (NSArray *)rt_protocols
84 {
85 unsigned int count;
86 Protocol **protocols = class_copyProtocolList(self, &count);
87
88 NSMutableArray *array = [NSMutableArray array];
89 for(unsigned i = 0; i < count; i++)
90 [array addObject: [RTProtocol protocolWithObjCProtocol: protocols[i]]];
91
92 free(protocols);
93 return array;
94 }
95
c90eafb @mikeash add basic method querying
authored Sep 14, 2010
96 + (NSArray *)rt_methods
97 {
98 unsigned int count;
99 Method *methods = class_copyMethodList(self, &count);
100
101 NSMutableArray *array = [NSMutableArray array];
102 for(unsigned i = 0; i < count; i++)
103 [array addObject: [RTMethod methodWithObjCMethod: methods[i]]];
104
37d6bc9 @lemnar free() arrays returned by class_copy* functions
lemnar authored Sep 24, 2010
105 free(methods);
c90eafb @mikeash add basic method querying
authored Sep 14, 2010
106 return array;
107 }
108
e082ffa @mikeash add methodForSelector:
authored Sep 14, 2010
109 + (RTMethod *)rt_methodForSelector: (SEL)sel
110 {
111 Method m = class_getInstanceMethod(self, sel);
112 if(!m) return nil;
113
114 return [RTMethod methodWithObjCMethod: m];
115 }
116
b7bd6c9 @mikeash add method adding
authored Sep 14, 2010
117 + (void)rt_addMethod: (RTMethod *)method
118 {
119 class_addMethod(self, [method selector], [method implementation], [[method signature] UTF8String]);
120 }
121
ac90912 @mikeash add ivar methods
authored Sep 15, 2010
122 + (NSArray *)rt_ivars
123 {
124 unsigned int count;
125 Ivar *list = class_copyIvarList(self, &count);
126
127 NSMutableArray *array = [NSMutableArray array];
128 for(unsigned i = 0; i < count; i++)
129 [array addObject: [RTIvar ivarWithObjCIvar: list[i]]];
130
37d6bc9 @lemnar free() arrays returned by class_copy* functions
lemnar authored Sep 24, 2010
131 free(list);
ac90912 @mikeash add ivar methods
authored Sep 15, 2010
132 return array;
133 }
134
135 + (RTIvar *)rt_ivarForName: (NSString *)name
136 {
137 Ivar ivar = class_getInstanceVariable(self, [name UTF8String]);
138 if(!ivar) return nil;
139 return [RTIvar ivarWithObjCIvar: ivar];
140 }
141
f5f0d31 @lemnar add property query functions to NSObject category
lemnar authored Sep 24, 2010
142 + (NSArray *)rt_properties
143 {
144 unsigned int count;
145 objc_property_t *list = class_copyPropertyList(self, &count);
146
147 NSMutableArray *array = [NSMutableArray array];
148 for(unsigned i = 0; i < count; i++)
149 [array addObject: [RTProperty propertyWithObjCProperty: list[i]]];
150
37d6bc9 @lemnar free() arrays returned by class_copy* functions
lemnar authored Sep 24, 2010
151 free(list);
f5f0d31 @lemnar add property query functions to NSObject category
lemnar authored Sep 24, 2010
152 return array;
153 }
154
155 + (RTProperty *)rt_propertyForName: (NSString *)name
156 {
157 objc_property_t property = class_getProperty(self, [name UTF8String]);
158 if(!property) return nil;
159 return [RTProperty propertyWithObjCProperty: property];
160 }
161
c4d5556 @rentzsch Make 10.6-compatible by only offering dynamic property addition when …
rentzsch authored Nov 27, 2011
162 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
b261adb Add support for dynamically adding properties (now requires 10.7).
wolf authored Nov 27, 2011
163 + (BOOL)rt_addProperty: (RTProperty *)property
164 {
165 return [property addToClass:self];
166 }
c4d5556 @rentzsch Make 10.6-compatible by only offering dynamic property addition when …
rentzsch authored Nov 27, 2011
167 #endif
b261adb Add support for dynamically adding properties (now requires 10.7).
wolf authored Nov 27, 2011
168
ddded36 @mikeash add metaclass code
authored Sep 14, 2010
169 - (Class)rt_class
170 {
171 return object_getClass(self);
172 }
173
225452d @mikeash add set class
authored Sep 14, 2010
174 - (Class)rt_setClass: (Class)newClass
175 {
6b9e45e @mikeash fix rt_setClass: lacking a return statement
authored Sep 18, 2010
176 return object_setClass(self, newClass);
225452d @mikeash add set class
authored Sep 14, 2010
177 }
178
dc12427 @mikeash initial commit with roughed-out API start
authored Sep 14, 2010
179 @end
180
Something went wrong with that request. Please try again.