ObjFW  Diff

Differences From Artifact [fb38385e6c]:

To Artifact [cacb4ce363]:

  • File src/OFMutableArray_adjacent.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 5759) [annotate] [blame] [check-ins using]


72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
72
73
74
75
76
77
78

79
80
81
82
83
84
85
86







-
+







- (void)replaceObjectAtIndex: (size_t)index
		  withObject: (id)object
{
	id *cArray = [array cArray];
	id oldObject;

	if (index >= [array count])
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	oldObject = cArray[index];
	cArray[index] = [object retain];
	[oldObject release];
}

- (void)replaceObjectIdenticalTo: (id)oldObject
147
148
149
150
151
152
153
154

155
156
157
158
159
160
161
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161







-
+








- (void)removeNObjects: (size_t)nObjects
{
	id *cArray = [array cArray], *copy;
	size_t i, count = [array count];

	if (nObjects > count)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	copy = [self allocMemoryForNItems: nObjects
				   ofSize: sizeof(id)];
	memcpy(copy, cArray + (count - nObjects), nObjects * sizeof(id));

	@try {
		[array removeNItems: nObjects];
170
171
172
173
174
175
176
177

178
179
180
181
182
183
184
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184







-
+








- (void)removeObjectsInRange: (of_range_t)range
{
	id *cArray = [array cArray], *copy;
	size_t i, count = [array count];

	if (range.length > count - range.start)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	copy = [self allocMemoryForNItems: range.length
				   ofSize: sizeof(id)];
	memcpy(copy, cArray + range.start, range.length * sizeof(id));

	@try {
		[array removeNItems: range.length
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
273
274
275
276
277
278
279
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
273
274
275
276
277
278
279
280







-
-
+
+

















-
-
+
+




-
-
+
+
+













	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
				  object: self];
			    exceptionWithClass: isa
					object: self];

		block(cArray[i], i, &stop);
	}
}

- (void)replaceObjectsUsingBlock: (of_array_replace_block_t)block
{
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;
	unsigned long mutations2 = mutations;

	for (i = 0; i < count && !stop; i++) {
		id newObject;

		if (mutations != mutations2)
			@throw [OFEnumerationMutationException
			    newWithClass: isa
				  object: self];
			    exceptionWithClass: isa
					object: self];

		newObject = block(cArray[i], i, &stop);

		if (newObject == nil)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		[newObject retain];
		[cArray[i] release];
		cArray[i] = newObject;
	}
}
#endif

- (void)makeImmutable
{
	isa = [OFArray_adjacent class];
}
@end