ObjFW  Check-in [c5d65603d8]

Overview
Comment:Initial support for blocks.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c5d65603d82a18fd6ee5ce1329f6e2d7a0dcd5c2eb958db8412985b2c46020b9
User & Date: js on 2010-08-28 18:54:35
Other Links: manifest | tags
Context
2010-08-29
17:53
More methods using blocks. check-in: e84073d0f9 user: js tags: trunk
2010-08-28
18:54
Initial support for blocks. check-in: c5d65603d8 user: js tags: trunk
2010-08-25
22:12
Remove @ATOMIC_OBJCFLAGS@. check-in: 8ba0c30a03 user: js tags: trunk
Changes

Modified configure.ac from [7b44bd66cf] to [4a2a23a23f].

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62








63

64




65
66
67
68
69
70
71
	], [
	id n = nil;
	for (id i in n);
	], [
	AC_DEFINE(OF_HAVE_FAST_ENUMERATION, 1,
		[Compiler support for Fast Enumeration])
	AC_MSG_RESULT(yes)
	], [
	AC_MSG_RESULT(no)])

AC_MSG_CHECKING(whether Objective C compiler supports properties)
AC_TRY_COMPILE([
	#import <objc/objc.h>

	@interface Foo
	{
		id bar;
	}

	@property (retain, nonatomic) id bar;
	@end
	], [
	Foo *foo = nil;
	[foo setBar: nil];
	[foo bar];
	], [
	AC_DEFINE(OF_HAVE_PROPERTIES, 1, [Compiler support for properties])
	AC_SUBST(PROPERTIESTESTS_M, "PropertiesTests.m")
	AC_MSG_RESULT(yes)








	], [

	AC_MSG_RESULT(no)])





AC_CHECK_HEADERS([objfw-rt.h objc/objc.h])

test x"$ac_cv_header_objfw_rt_h" = x"yes" && objc_runtime="ObjFW-RT"

if test x"$ac_cv_header_objc_objc_h" = x"yes"; then
	dnl TODO: This is ugly. Let's think of a better check.







<
|




















>
>
>
>
>
>
>
>

>
|
>
>
>
>







34
35
36
37
38
39
40

41
42
43
44
45
46
47
48
49
50
51
52
53
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
	], [
	id n = nil;
	for (id i in n);
	], [
	AC_DEFINE(OF_HAVE_FAST_ENUMERATION, 1,
		[Compiler support for Fast Enumeration])
	AC_MSG_RESULT(yes)

	], [AC_MSG_RESULT(no)])

AC_MSG_CHECKING(whether Objective C compiler supports properties)
AC_TRY_COMPILE([
	#import <objc/objc.h>

	@interface Foo
	{
		id bar;
	}

	@property (retain, nonatomic) id bar;
	@end
	], [
	Foo *foo = nil;
	[foo setBar: nil];
	[foo bar];
	], [
	AC_DEFINE(OF_HAVE_PROPERTIES, 1, [Compiler support for properties])
	AC_SUBST(PROPERTIESTESTS_M, "PropertiesTests.m")
	AC_MSG_RESULT(yes)
	], [AC_MSG_RESULT(no)])

AC_MSG_CHECKING(whether Objective C compiler supports blocks)
old_OBJCFLAGS="$OBJCFLAGS"
OBJCFLAGS="$OBJCFLAGS -fblocks"
AC_TRY_COMPILE([], [
	int (^foo)(int bar);
	foo = ^(int bar) { return 0; }
	], [
	AC_DEFINE(OF_HAVE_BLOCKS, 1, [Compiler support for blocks])
	AC_MSG_RESULT(yes)
	], [
	AC_MSG_RESULT(no)
	OBJCFLAGS="$old_OBJCFLAGS"
	])

AC_CHECK_HEADERS([objfw-rt.h objc/objc.h])

test x"$ac_cv_header_objfw_rt_h" = x"yes" && objc_runtime="ObjFW-RT"

if test x"$ac_cv_header_objc_objc_h" = x"yes"; then
	dnl TODO: This is ugly. Let's think of a better check.

Modified src/OFArray.h from [8a7d786fed] to [695e6c2270].

13
14
15
16
17
18
19




20
21
22
23
24
25
26

#import "OFObject.h"
#import "OFEnumerator.h"

@class OFDataArray;
@class OFString;





/**
 * \brief A class for storing objects in an array.
 */
@interface OFArray: OFObject <OFCopying, OFMutableCopying, OFFastEnumeration>
{
	OFDataArray *array;
}







>
>
>
>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#import "OFObject.h"
#import "OFEnumerator.h"

@class OFDataArray;
@class OFString;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_array_enumeration_block_t)(id obj, size_t idx, BOOL *stop);
#endif

/**
 * \brief A class for storing objects in an array.
 */
@interface OFArray: OFObject <OFCopying, OFMutableCopying, OFFastEnumeration>
{
	OFDataArray *array;
}
178
179
180
181
182
183
184









185
186
187
188
189
190
191
 */
- (OFString*)componentsJoinedByString: (OFString*)separator;

/**
 * \return An OFEnumerator to enumarate through the array's objects
 */
- (OFEnumerator*)enumerator;









@end

/// \cond internal
@interface OFArrayEnumerator: OFEnumerator
{
	OFDataArray   *array;
	size_t	      count;







>
>
>
>
>
>
>
>
>







182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 */
- (OFString*)componentsJoinedByString: (OFString*)separator;

/**
 * \return An OFEnumerator to enumarate through the array's objects
 */
- (OFEnumerator*)enumerator;

#ifdef OF_HAVE_BLOCKS
/**
 * Executes a block for each object.
 *
 * \param block The block to execute for each object
 */
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block;
#endif
@end

/// \cond internal
@interface OFArrayEnumerator: OFEnumerator
{
	OFDataArray   *array;
	size_t	      count;

Modified src/OFArray.m from [3b7a88e4d4] to [9bd99c7ed0].

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
		[objs[i] retain];

	return new;
}

- (id)objectAtIndex: (size_t)index
{
	return *((OFObject**)[array itemAtIndex: index]);
}

- (size_t)indexOfObject: (OFObject*)obj
{
	id *objs = [array cArray];
	size_t i, count = [array count];








|







207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
		[objs[i] retain];

	return new;
}

- (id)objectAtIndex: (size_t)index
{
	return *((id*)[array itemAtIndex: index]);
}

- (size_t)indexOfObject: (OFObject*)obj
{
	id *objs = [array cArray];
	size_t i, count = [array count];

346
347
348
349
350
351
352












353
354
355
356
357
358
359

- (OFEnumerator*)enumerator
{
	return [[[OFArrayEnumerator alloc]
	    initWithDataArray: array
	     mutationsPointer: NULL] autorelease];
}













- (void)dealloc
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)







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







346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

- (OFEnumerator*)enumerator
{
	return [[[OFArrayEnumerator alloc]
	    initWithDataArray: array
	     mutationsPointer: NULL] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;

	for (i = 0; i < count && !stop; i++)
		block(objs[i], i, &stop);
}
#endif

- (void)dealloc
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)

Modified src/OFDictionary.h from [43cc867a38] to [49adfd1a99].

11
12
13
14
15
16
17




18
19
20
21
22
23
24

#include <stdarg.h>

#import "OFObject.h"
#import "OFEnumerator.h"

@class OFArray;





/// \cond internal
struct of_dictionary_bucket
{
	OFObject <OFCopying> *key;
	OFObject *object;
	uint32_t hash;







>
>
>
>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <stdarg.h>

#import "OFObject.h"
#import "OFEnumerator.h"

@class OFArray;

#ifdef OF_HAVE_BLOCKS
typedef void (^of_dictionary_enumeration_block_t)(id key, id obj, BOOL *stop);
#endif

/// \cond internal
struct of_dictionary_bucket
{
	OFObject <OFCopying> *key;
	OFObject *object;
	uint32_t hash;
162
163
164
165
166
167
168










169
170
171
172
173
174
175
 */
- (OFEnumerator*)objectEnumerator;

/**
 * \return An OFEnumerator to enumerate through the dictionary's keys
 */
- (OFEnumerator*)keyEnumerator;










@end

/// \cond internal
@interface OFDictionaryEnumerator: OFEnumerator
{
	struct of_dictionary_bucket **data;
	uint32_t size;







>
>
>
>
>
>
>
>
>
>







166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
 */
- (OFEnumerator*)objectEnumerator;

/**
 * \return An OFEnumerator to enumerate through the dictionary's keys
 */
- (OFEnumerator*)keyEnumerator;

#ifdef OF_HAVE_BLOCKS
/**
 * Executes a block for each key / object pair.
 *
 * \param block The block to execute for each key / object pair.
 */
- (void)enumerateKeysAndObjectsUsingBlock:
    (of_dictionary_enumeration_block_t)block;
#endif
@end

/// \cond internal
@interface OFDictionaryEnumerator: OFEnumerator
{
	struct of_dictionary_bucket **data;
	uint32_t size;

Modified src/OFDictionary.m from [3d989f9035] to [bf50d5443a].

632
633
634
635
636
637
638













639
640
641
642
643
644
645
- (OFEnumerator*)keyEnumerator
{
	return [[[OFDictionaryKeyEnumerator alloc]
		initWithData: data
			size: size
	    mutationsPointer: NULL] autorelease];
}














- (void)dealloc
{
	uint32_t i;

	for (i = 0; i < size; i++) {
		if (data[i] != NULL && data[i] != DELETED) {







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







632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
- (OFEnumerator*)keyEnumerator
{
	return [[[OFDictionaryKeyEnumerator alloc]
		initWithData: data
			size: size
	    mutationsPointer: NULL] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateKeysAndObjectsUsingBlock:
    (of_dictionary_enumeration_block_t)block
{
	size_t i;
	BOOL stop = NO;

	for (i = 0; i < size && !stop; i++)
		if (data[i] != NULL && data[i] != DELETED)
			block(data[i]->key, data[i]->object, &stop);
}
#endif

- (void)dealloc
{
	uint32_t i;

	for (i = 0; i < size; i++) {
		if (data[i] != NULL && data[i] != DELETED) {

Modified src/OFMutableArray.m from [bad05b4309] to [ad03860f72].

231
232
233
234
235
236
237


















238

- (OFEnumerator*)enumerator
{
	return [[[OFArrayEnumerator alloc]
	    initWithDataArray: array
	     mutationsPointer: &mutations] autorelease];
}


















@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

- (OFEnumerator*)enumerator
{
	return [[[OFArrayEnumerator alloc]
	    initWithDataArray: array
	     mutationsPointer: &mutations] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	OFObject **objs = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	for (i = 0; i < count && !stop; i++) {
		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa];

		block(objs[i], i, &stop);
	}
}
#endif
@end

Modified src/OFMutableDictionary.m from [0791cedf2b] to [5c1939bedc].

260
261
262
263
264
265
266



















267
- (OFEnumerator*)keyEnumerator
{
	return [[[OFDictionaryKeyEnumerator alloc]
		initWithData: data
			size: size
	    mutationsPointer: &mutations] autorelease];
}



















@end







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

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
- (OFEnumerator*)keyEnumerator
{
	return [[[OFDictionaryKeyEnumerator alloc]
		initWithData: data
			size: size
	    mutationsPointer: &mutations] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateKeysAndObjectsUsingBlock:
    (of_dictionary_enumeration_block_t)block
{
	size_t i;
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	for (i = 0; i < size && !stop; i++) {
		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa];

		if (data[i] != NULL && data[i] != DELETED)
			block(data[i]->key, data[i]->object, &stop);
	}
}
#endif
@end

Modified src/objfw-defs.h.in from [b3381e8211] to [40a7e9074f].

1
2
3
4
5
6

7
8
9
10
11
12
13
14
15
16
17
18
19
#undef OF_APPLE_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_ATOMIC_OPS
#undef OF_BIG_ENDIAN
#undef OF_GNU_RUNTIME
#undef OF_HAVE_ASPRINTF

#undef OF_HAVE_FAST_ENUMERATION
#undef OF_HAVE_GCC_ATOMIC_OPS
#undef OF_HAVE_LIBKERN_OSATOMIC_H
#undef OF_HAVE_POLL
#undef OF_HAVE_PROPERTIES
#undef OF_HAVE_PTHREADS
#undef OF_HAVE_PTHREAD_SPINLOCKS
#undef OF_HAVE_SCHED_YIELD
#undef OF_HAVE_SYS_SELECT_H
#undef OF_OBJFW_RUNTIME
#undef OF_PLUGINS
#undef OF_THREADS
#undef SIZE_MAX






>













1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#undef OF_APPLE_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_ATOMIC_OPS
#undef OF_BIG_ENDIAN
#undef OF_GNU_RUNTIME
#undef OF_HAVE_ASPRINTF
#undef OF_HAVE_BLOCKS
#undef OF_HAVE_FAST_ENUMERATION
#undef OF_HAVE_GCC_ATOMIC_OPS
#undef OF_HAVE_LIBKERN_OSATOMIC_H
#undef OF_HAVE_POLL
#undef OF_HAVE_PROPERTIES
#undef OF_HAVE_PTHREADS
#undef OF_HAVE_PTHREAD_SPINLOCKS
#undef OF_HAVE_SCHED_YIELD
#undef OF_HAVE_SYS_SELECT_H
#undef OF_OBJFW_RUNTIME
#undef OF_PLUGINS
#undef OF_THREADS
#undef SIZE_MAX

Modified tests/OFArrayTests.m from [0fbf78cb38] to [ff3793f72c].

182
183
184
185
186
187
188
189





































190
191
192
		[e dealloc];
	}

	TEST(@"Detection of mutation during Fast Enumeration", ok)

	[m[0] removeNObjects: 1];
#endif






































	[pool drain];
}
@end








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



182
183
184
185
186
187
188
189
190
191
192
193
194
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
222
223
224
225
226
227
228
229
		[e dealloc];
	}

	TEST(@"Detection of mutation during Fast Enumeration", ok)

	[m[0] removeNObjects: 1];
#endif

#ifdef OF_HAVE_BLOCKS
	{
		__block BOOL ok = YES;
		__block size_t count = 0;
		OFArray *cmp = a[0];
		OFMutableArray *a2;

		m[0] = [[a[0] mutableCopy] autorelease];
		[m[0] enumerateObjectsUsingBlock:
		    ^ (id obj, size_t idx, BOOL *stop) {
			    count++;
			    if (![obj isEqual: [cmp objectAtIndex: idx]])
				    ok = NO;
		}];

		if (count != [cmp count])
			ok = NO;

		TEST(@"Enumeration using blocks", ok)

		ok = NO;
		a2 = m[0];
		@try {
			[a2 enumerateObjectsUsingBlock:
			    ^ (id obj, size_t idx, BOOL *stop) {
				[a2 removeObjectAtIndex: idx];
			}];
		} @catch (OFEnumerationMutationException *e) {
			ok = YES;
			[e dealloc];
		}

		TEST(@"Detection of mutation during enumeration using blocks",
		    ok)
	}
#endif

	[pool drain];
}
@end

Modified tests/OFDictionaryTests.m from [c3d8891ce2] to [16f274bf48].

90
91
92
93
94
95
96



































97
98
99
100
101
102
103
		[e dealloc];
	}

	TEST(@"Detection of mutation during Fast Enumeration", ok)

	[dict removeObjectForKey: @""];
#endif




































	TEST(@"-[count]", [dict count] == 2)

	TEST(@"+[dictionaryWithKeysAndObjects:]",
	    (dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar",
								@"baz", @"qux",
								nil]) &&







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







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
		[e dealloc];
	}

	TEST(@"Detection of mutation during Fast Enumeration", ok)

	[dict removeObjectForKey: @""];
#endif

#ifdef OF_HAVE_BLOCKS
	{
		__block size_t i = 0;
		__block BOOL ok = YES;

		[dict enumerateKeysAndObjectsUsingBlock:
		    ^ (id key, id obj, BOOL *stop) {
			if (![key isEqual: keys[i]])
				ok = NO;
			[dict setObject: [dict objectForKey: key]
				 forKey: key];
			i++;
		}];

		TEST(@"Enumeration using blocks", ok)

		ok = NO;
		@try {
			[dict enumerateKeysAndObjectsUsingBlock:
			    ^ (id key, id obj, BOOL *stop) {
				[dict setObject: @""
					 forKey: @""];
			}];
		} @catch (OFEnumerationMutationException *e) {
			ok = YES;
			[e dealloc];
		}

		TEST(@"Detection of mutation during enumeration using blocks",
		    ok)

		[dict removeObjectForKey: @""];
	}
#endif

	TEST(@"-[count]", [dict count] == 2)

	TEST(@"+[dictionaryWithKeysAndObjects:]",
	    (dict = [OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar",
								@"baz", @"qux",
								nil]) &&