ObjFW  Check-in [dbd145b124]

Overview
Comment:Check for mutations during fast enumeration in OFMutableSet.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dbd145b1243c3a08ab4a0c0d97a781b8600dda7bcc5d43f20a0448398d916cb3
User & Date: js on 2011-07-20 16:50:19
Other Links: manifest | tags
Context
2011-07-20
18:23
Remove code duplication for fast enumeration of mutable collections. check-in: 63fc941b66 user: js tags: trunk
16:50
Check for mutations during fast enumeration in OFMutableSet. check-in: dbd145b124 user: js tags: trunk
2011-07-19
23:28
Make OFFastEnumeration part of OFCollection. check-in: ab71981cda user: js tags: trunk
Changes

Modified src/OFMutableSet.h from [7c8ba2472f] to [927b683a17].

16
17
18
19
20
21
22




23
24
25
26
27
28
29
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33







+
+
+
+








#import "OFSet.h"

/**
 * \brief An mutable unordered set of unique objects.
 */
@interface OFMutableSet: OFSet
{
	unsigned long mutations;
}

/**
 * \brief Adds the specified object to the set.
 *
 * \param object The object to add to the set
 */
- (void)addObject: (id)object;

Modified src/OFMutableSet.m from [b2021bf947] to [44aa66377a].

17
18
19
20
21
22
23

24
25
26
27
28
29
30
31
32




33
34
35
36










































37
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


32
33
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







+







-
-
+
+
+
+



-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

#include "config.h"

#define OF_MUTABLE_SET_M

#import "OFMutableSet.h"
#import "OFDictionary.h"
#import "OFNull.h"
#import "OFAutoreleasePool.h"

@implementation OFMutableSet
- (void)addObject: (id)object
{
	[dictionary _setObject: [OFNull null]
			forKey: object
		       copyKey: NO];
}


	mutations++;
}

- (void)removeObject: (id)object
{
	[dictionary removeObjectForKey: object];
}

	mutations++;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count
{
	OFAutoreleasePool *pool = state->extra.pointers[0];
	OFEnumerator *enumerator = state->extra.pointers[1];
	int i;

	state->itemsPtr = objects;
	state->mutationsPtr = &mutations;

	if (state->state == -1)
		return 0;

	if (state->state == 0) {
		pool = [[OFAutoreleasePool alloc] init];
		enumerator = [dictionary keyEnumerator];

		state->extra.pointers[0] = pool;
		state->extra.pointers[1] = enumerator;

		state->state = 1;
	}

	for (i = 0; i < count; i++) {
		id object = [enumerator nextObject];

		if (object == nil) {
			[pool release];
			state->state = -1;
			return i;
		}

		objects[i] = object;
	}

	return count;
}
@end

Modified tests/OFSet.m from [6401139555] to [daab62c011].

13
14
15
16
17
18
19

20

21

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

23
24
25
26
27
28
29
30







+

+
-
+







 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFSet.h"
#import "OFArray.h"
#import "OFAutoreleasePool.h"

#import "OFArray.h"
#import "OFEnumerationMutationException.h"

#import "TestsAppDelegate.h"

static OFString *module = @"OFSet";

@implementation TestsAppDelegate (OFSetTests)
- (void)setTests
102
103
104
105
106
107
108











109
110
111
112
113
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126







+
+
+
+
+
+
+
+
+
+
+





		i++;
	}

	if (i != 4)
		ok = NO;

	TEST(@"Fast enumeration", ok)

	ok = NO;
	@try {
		for (OFString *s in mutableSet)
			[mutableSet removeObject: s];
	} @catch (OFEnumerationMutationException *e) {
		ok = YES;
		[e dealloc];
	}

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

	[pool drain];
}
@end