ObjFW  Check-in [93a3badbb7]

Overview
Comment:Add introspection for instance variables.

Currently only for the Apple and new GNU runtime.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 93a3badbb7cac6f56d1cac15d5315c40e3638a3e28ae19acb9232bc2b88aaa61
User & Date: js on 2011-10-16 19:41:42
Other Links: manifest | tags
Context
2011-10-16
19:51
Add introspection for instance variables for the old GNU runtime. check-in: 51053dafc6 user: js tags: trunk
19:41
Add introspection for instance variables. check-in: 93a3badbb7 user: js tags: trunk
2011-10-12
15:10
Change the order of includes to satisfy newer MinGW versions. check-in: 27500362e7 user: js tags: trunk
Changes

Modified src/OFIntrospection.h from [646b5aa4e8] to [5767ab8f57].

54
55
56
57
58
59
60






































61
62
63
64
65
66
67

68
69
70
71
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103






104

105
 * \brief Returns the type encoding for the method.
 *
 * \return The type encoding for the method
 */
- (const char*)typeEncoding;
@end







































/**
 * \brief A class for introspecting classes.
 */
@interface OFIntrospection: OFObject
{
	OFMutableArray *classMethods;
	OFMutableArray *instanceMethods;

}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, copy) OFArray *classMethods;
@property (readonly, copy) OFArray *instanceMethods;

#endif

/**
 * \brief Creates a new introspection for the specified class.
 *
 * \return A new, autoreleased introspection for the specified class
 */
+ introspectionWithClass: (Class)class_;

/**
 * \brief Initializes an already allocated OFIntrospection with the specified
 *	  class.
 *
 * \return An initialized OFIntrospection
 */
- initWithClass: (Class)class_;

/**
 * \brief Returns the class methods of the class.
 *
 * \return The class methods of the class
 */
- (OFArray*)classMethods;

/**
 * \brief Returns the instance methods of the class.
 *
 * \return The instance methods of the class
 */
- (OFArray*)instanceMethods;







/* TODO: ivars, properties */

@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>





>




















|






|



>
>
>
>
>
>
|
>

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
 * \brief Returns the type encoding for the method.
 *
 * \return The type encoding for the method
 */
- (const char*)typeEncoding;
@end

/**
 * \brief A class for describing an instance variable.
 */
@interface OFInstanceVariable: OFObject
{
	OFString *name;
	ptrdiff_t offset;
	const char *typeEncoding;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, copy) OFString *name;
@property (readonly) ptrdiff_t offset;
@property (readonly) const char *typeEncoding;
#endif

/**
 * \brief Returns the name of the instance variable.
 *
 * \return The name of the instance variable
 */
- (OFString*)name;

/**
 * \brief Returns the offset of the instance variable.
 *
 * \return The offset of the instance variable
 */
- (ptrdiff_t)offset;

/**
 * \brief Returns the type encoding for the instance variable.
 *
 * \return The type encoding for the instance variable
 */
- (const char*)typeEncoding;
@end

/**
 * \brief A class for introspecting classes.
 */
@interface OFIntrospection: OFObject
{
	OFMutableArray *classMethods;
	OFMutableArray *instanceMethods;
	OFMutableArray *instanceVariables;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, copy) OFArray *classMethods;
@property (readonly, copy) OFArray *instanceMethods;
@property (readonly, copy) OFArray *instanceVariables;
#endif

/**
 * \brief Creates a new introspection for the specified class.
 *
 * \return A new, autoreleased introspection for the specified class
 */
+ introspectionWithClass: (Class)class_;

/**
 * \brief Initializes an already allocated OFIntrospection with the specified
 *	  class.
 *
 * \return An initialized OFIntrospection
 */
- initWithClass: (Class)class_;

/**
 * \brief Returns the class methods of the class.
 *
 * \return An array of OFMethods
 */
- (OFArray*)classMethods;

/**
 * \brief Returns the instance methods of the class.
 *
 * \return An array of OFMethods
 */
- (OFArray*)instanceMethods;

/**
 * \brief Returns the instance variables of the class.
 *
 * \return An array of OFInstanceVariables
 */
- (OFArray*)instanceVariables;

/* TODO: properties, protocols */
@end

Modified src/OFIntrospection.m from [7933a51094] to [6a25e3dc19].

94
95
96
97
98
99
100



















































101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

116



117
118
119

120

121
122
123
124
125
126
127

- (OFString*)description
{
	return [OFString stringWithFormat: @"<OFMethod: %@ [%s]>",
					   name, typeEncoding];
}
@end




















































@implementation OFIntrospection
+ introspectionWithClass: (Class)class
{
	return [[[self alloc] initWithClass: class] autorelease];
}

- initWithClass: (Class)class
{
	self = [super init];

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
#if defined(OF_APPLE_RUNTIME) || defined(OF_GNU_RUNTIME)
		Method *methodList;

		unsigned i, count;




		classMethods = [[OFMutableArray alloc] init];
		instanceMethods = [[OFMutableArray alloc] init];



		methodList = class_copyMethodList(((OFObject*)class)->isa,
		    &count);
		@try {
			for (i = 0; i < count; i++) {
				[classMethods addObject: [[[OFMethod alloc]
				    _initWithMethod: methodList[i]]
				    autorelease]];







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>















>

>
>
>



>

>







94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

- (OFString*)description
{
	return [OFString stringWithFormat: @"<OFMethod: %@ [%s]>",
					   name, typeEncoding];
}
@end

@implementation OFInstanceVariable
#if defined(OF_APPLE_RUNTIME) || defined(OF_GNU_RUNTIME)
- _initWithIvar: (Ivar)ivar
{
	self = [super init];

	@try {
		name = [[OFString alloc]
		    initWithCString: ivar_getName(ivar)
			   encoding: OF_STRING_ENCODING_ASCII];
		offset = ivar_getOffset(ivar);
		typeEncoding = ivar_getTypeEncoding(ivar);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
#endif

- (void)dealloc
{
	[name release];

	[super dealloc];
}

- (OFString*)name
{
	OF_GETTER(name, YES);
}

- (ptrdiff_t)offset
{
	return offset;
}

- (const char*)typeEncoding
{
	return typeEncoding;
}

- (OFString*)description
{
	return [OFString stringWithFormat:
	    @"<OFInstanceVariable: %@ [%s] @ 0x%tx>",
	    name, typeEncoding, offset];
}
@end

@implementation OFIntrospection
+ introspectionWithClass: (Class)class
{
	return [[[self alloc] initWithClass: class] autorelease];
}

- initWithClass: (Class)class
{
	self = [super init];

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
#if defined(OF_APPLE_RUNTIME) || defined(OF_GNU_RUNTIME)
		Method *methodList;
		Ivar *ivarList;
		unsigned i, count;
#elif defined(OF_OLD_GNU_RUNTIME)
		MethodList_t methodList;
#endif

		classMethods = [[OFMutableArray alloc] init];
		instanceMethods = [[OFMutableArray alloc] init];
		instanceVariables = [[OFMutableArray alloc] init];

#if defined(OF_APPLE_RUNTIME) || defined(OF_GNU_RUNTIME)
		methodList = class_copyMethodList(((OFObject*)class)->isa,
		    &count);
		@try {
			for (i = 0; i < count; i++) {
				[classMethods addObject: [[[OFMethod alloc]
				    _initWithMethod: methodList[i]]
				    autorelease]];
138
139
140
141
142
143
144
145
146






147
148
149

150

151
152
153
154
155
156
157
				    _initWithMethod: methodList[i]]
				    autorelease]];
				[pool releaseObjects];
			}
		} @finally {
			free(methodList);
		}
#elif defined(OF_OLD_GNU_RUNTIME)
		MethodList_t methodList;







		classMethods = [[OFMutableArray alloc] init];
		instanceMethods = [[OFMutableArray alloc] init];



		for (methodList = class->class_pointer->methods;
		    methodList != NULL; methodList = methodList->method_next) {
			int i;

			for (i = 0; i < methodList->method_count; i++)
				[classMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:







|
|
>
>
>
>
>
>
|
|
<
>
|
>







195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

212
213
214
215
216
217
218
219
220
221
				    _initWithMethod: methodList[i]]
				    autorelease]];
				[pool releaseObjects];
			}
		} @finally {
			free(methodList);
		}

		ivarList = class_copyIvarList(class, &count);
		@try {
			for (i = 0; i < count; i++) {
				[instanceVariables addObject:
				    [[[OFInstanceVariable alloc]
				    _initWithIvar: ivarList[i]] autorelease]];
				[pool releaseObjects];
			}
		} @finally {

			free(ivarList);
		}
#elif defined(OF_OLD_GNU_RUNTIME)
		for (methodList = class->class_pointer->methods;
		    methodList != NULL; methodList = methodList->method_next) {
			int i;

			for (i = 0; i < methodList->method_count; i++)
				[classMethods addObject: [[[OFMethod alloc]
				    _initWithMethod:
167
168
169
170
171
172
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
195
196
197
198
199
200





201
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}
#endif

		[classMethods makeImmutable];
		[instanceMethods makeImmutable];


		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[classMethods release];
	[instanceMethods release];


	[super dealloc];
}

- (OFArray*)classMethods
{
	OF_GETTER(classMethods, YES)
}

- (OFArray*)instanceMethods
{
	OF_GETTER(instanceMethods, YES)
}





@end







>














>













>
>
>
>
>

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
				    _initWithMethod:
				    &methodList->method_list[i]] autorelease]];
		}
#endif

		[classMethods makeImmutable];
		[instanceMethods makeImmutable];
		[instanceVariables makeImmutable];

		[pool release];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[classMethods release];
	[instanceMethods release];
	[instanceVariables release];

	[super dealloc];
}

- (OFArray*)classMethods
{
	OF_GETTER(classMethods, YES)
}

- (OFArray*)instanceMethods
{
	OF_GETTER(instanceMethods, YES)
}

- (OFArray*)instanceVariables
{
	OF_GETTER(instanceVariables, YES)
}
@end