ObjFW  Check-in [5d190947a7]

Overview
Comment:Implement OFCopying protocol for OFList.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 5d190947a76fabc8a0662fbeabbe497b25fbb46695e646e315b7a2a54b32fbc6
User & Date: js on 2009-05-20 19:59:13
Other Links: manifest | tags
Context
2009-05-23
01:59
Prevent + initialize from being called twice. check-in: e0163c30a9 user: js tags: trunk
2009-05-20
19:59
Implement OFCopying protocol for OFList. check-in: 5d190947a7 user: js tags: trunk
2009-05-19
17:13
Rename + tcpSocket to + socket in OFTCPSocket. check-in: 0b8c5616cb user: js tags: trunk
Changes

Modified src/OFList.h from [ea241d3479] to [8727a6856e].

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
	struct __of_list_object *next;
	struct __of_list_object *prev;
} of_list_object_t;

/**
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject
{
	of_list_object_t *first;
	of_list_object_t *last;
	BOOL		 retain_and_release;
}
/**
 * \return A new autoreleased OFList







|







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
	struct __of_list_object *next;
	struct __of_list_object *prev;
} of_list_object_t;

/**
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject <OFCopying>
{
	of_list_object_t *first;
	of_list_object_t *last;
	BOOL		 retain_and_release;
}
/**
 * \return A new autoreleased OFList

Modified src/OFList.m from [c06f8c5d7d] to [a7621f2b8f].

8
9
10
11
12
13
14

15
16
17
18
19
20
21
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import "OFList.h"


@implementation OFList
+ list
{
	return [[[OFList alloc] init] autorelease];
}








>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "config.h"

#import "OFList.h"
#import "OFExceptions.h"

@implementation OFList
+ list
{
	return [[[OFList alloc] init] autorelease];
}

193
194
195
196
197
198
199







































200

	/* One has still items */
	if (iter != NULL || iter2 != NULL)
		return NO;

	return YES;
}







































@end







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

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
230
231
232
233
234
235
236
237
238
239
240

	/* One has still items */
	if (iter != NULL || iter2 != NULL)
		return NO;

	return YES;
}

- (id)copy
{
	OFList *new;
	of_list_object_t *iter, *o, *prev;

	if (retain_and_release)
		new = [[OFList alloc] init];
	else
		new = [[OFList alloc] initWithoutRetainAndRelease];

	o = NULL;
	prev = NULL;

	@try {
		for (iter = first; iter != NULL; iter = iter->next) {
			o = [new allocWithSize: sizeof(of_list_object_t)];
			o->object = iter->object;
			o->next = NULL;
			o->prev = prev;

			if (new->first == NULL)
				new->first = o;
			if (prev != NULL)
				prev->next = o;
			if (retain_and_release)
				[o->object retain];

			prev = o;
		}
	} @catch (OFException *e) {
		[new release];
		@throw e;
	}

	new->last = o;

	return new;
}
@end

Modified tests/OFList/OFList.m from [95b616ccee] to [7639c23356].

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

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 14
#define SUCCESS								\
{									\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);							\
}
#define FAIL								\







|







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

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

#define NUM_TESTS 23
#define SUCCESS								\
{									\
	printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m",	\
	    (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS);		\
	fflush(stdout);							\
}
#define FAIL								\
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	@"Third String Object"
};

int
main()
{
	size_t i, j;
	OFList *list, *list2;
	of_list_object_t *iter;

	list = [OFList list];

	[list append: strings[0]];
	[list append: strings[1]];
	[list append: strings[2]];








|
|







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	@"Third String Object"
};

int
main()
{
	size_t i, j;
	OFList *list, *list2, *list3;
	of_list_object_t *iter, *iter2;

	list = [OFList list];

	[list append: strings[0]];
	[list append: strings[1]];
	[list append: strings[2]];

95
96
97
98
99
100
101




102
103
104












105
106
107
108
	[list2 append: strings[1]];
	[list2 append: strings[2]];
	CHECK([list2 isEqual: list]);

	[list2 remove: [list2 last]];
	CHECK(![list2 isEqual: list]);





	[list2 append: @"foo"];
	CHECK(![list2 isEqual: list]);













	puts("");

	return 0;
}







>
>
>
>
|


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




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
	[list2 append: strings[1]];
	[list2 append: strings[2]];
	CHECK([list2 isEqual: list]);

	[list2 remove: [list2 last]];
	CHECK(![list2 isEqual: list]);

	/*
	 * Only mutableCopy is guaranteed to return a real copy instead of just
	 * increasing the reference counter.
	 */
	[list2 append: [@"foo" mutableCopy]];
	CHECK(![list2 isEqual: list]);

	list3 = [list2 copy];
	CHECK([list2 isEqual: list3]);

	for (iter = [list2 first], iter2 = [list3 first];
	    iter != NULL && iter2 != NULL;
	    iter = iter->next, iter2 = iter2->next) {
		CHECK(iter != iter2)
		CHECK(iter->object == iter2->object)
	}
	CHECK(iter == NULL && iter2 == NULL)
	CHECK([[list2 last]->object retainCount] == 3)

	puts("");

	return 0;
}