ObjFW  Check-in [0ea8e1ef09]

Overview
Comment:Nicer OFList API.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 0ea8e1ef094e3a40310521f581920c6514de6afc41176cfd056a9700de18915c
User & Date: js on 2010-04-23 13:10:11
Other Links: manifest | tags
Context
2010-04-23
14:02
Add a warning to OFStream documentation. check-in: 83b2a5d5d7 user: js tags: trunk
13:10
Nicer OFList API. check-in: 0ea8e1ef09 user: js tags: trunk
12:50
Improve link, symlink and rename operations in OFFile. check-in: 34991b40af user: js tags: trunk
Changes

Modified src/OFList.h from [ed49261cf1] to [44738d18b3].

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







-
-
+
+




-
-
+
+











-
+




-
+









-
+









-
+










-
-
+
+










-
-
+
+






-
+






} of_list_object_t;

/**
 * \brief A class which provides easy to use double-linked lists.
 */
@interface OFList: OFObject <OFCopying>
{
	of_list_object_t *first;
	of_list_object_t *last;
	of_list_object_t *firstListObject;
	of_list_object_t *lastListObject;
	size_t		 count;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly) of_list_object_t *first;
@property (readonly) of_list_object_t *last;
@property (readonly) of_list_object_t *firstListObject;
@property (readonly) of_list_object_t *lastListObject;
@property (readonly) size_t count;
#endif

/**
 * \return A new autoreleased OFList
 */
+ list;

/**
 * \return The first list object in the list
 */
- (of_list_object_t*)first;
- (of_list_object_t*)firstListObject;

/**
 * \return The last list object in the list
 */
- (of_list_object_t*)last;
- (of_list_object_t*)lastListObject;

/**
 * Appends an object to the list.
 *
 * \param obj The object to append
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */
- (of_list_object_t*)append: (OFObject*)obj;
- (of_list_object_t*)appendObject: (OFObject*)obj;

/**
 * Prepends an object to the list.
 *
 * \param obj The object to prepend
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */
- (of_list_object_t*)prepend: (OFObject*)obj;
- (of_list_object_t*)prependObject: (OFObject*)obj;

/**
 * Inserts an object before another object.
 * \param obj The object to insert
 * \param listobj The of_list_object_t of the object before which it should be
 *	  inserted
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */
- (of_list_object_t*)insert: (OFObject*)obj
		     before: (of_list_object_t*)listobj;
- (of_list_object_t*)insertObject: (OFObject*)obj
		 beforeListObject: (of_list_object_t*)listobj;

/**
 * Inserts an object after another object.
 * \param obj The object to insert
 * \param listobj The of_list_object_t of the object after which it should be
 *	  inserted
 * \return An of_list_object_t, needed to identify the object inside the list.
 *	   For example, if you want to remove an object from the list, you need
 *	   its of_list_object_t.
 */
- (of_list_object_t*)insert: (OFObject*)obj
		      after: (of_list_object_t*)listobj;
- (of_list_object_t*)insertObject: (OFObject*)obj
		  afterListObject: (of_list_object_t*)listobj;

/**
 * Removes the object with the specified list object from the list.
 *
 * \param listobj The list object returned by append / prepend
 */
- (void)remove: (of_list_object_t*)listobj;
- (void)removeListObject: (of_list_object_t*)listobj;

/**
 * \return The number of items in the list.
 */
- (size_t)count;
@end

Modified src/OFList.m from [b9fb525575] to [4b2ea9b8f4].

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

153
154
155
156
157
158
159
160
161
162




163
164
165
166
167
168
169
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
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
153
154
155
156
157
158




159
160
161
162
163
164
165
166
167
168
169







-
-
+
+








-
+





-
+

-
+


-
+

-
+


-
+






-
+

-
-
+
+

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








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













-
-
+
+








-
-
+
+













-
-
+
+








-
+






-
-
-
-
+
+
+
+







	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	first = NULL;
	last = NULL;
	firstListObject = NULL;
	lastListObject = NULL;

	return self;
}

- (void)dealloc
{
	of_list_object_t *iter;

	for (iter = first; iter != NULL; iter = iter->next)
	for (iter = firstListObject; iter != NULL; iter = iter->next)
		[iter->object release];

	[super dealloc];
}

- (of_list_object_t*)first
- (of_list_object_t*)firstListObject;
{
	return first;
	return firstListObject;
}

- (of_list_object_t*)last
- (of_list_object_t*)lastListObject;
{
	return last;
	return lastListObject;
}

- (of_list_object_t*)append: (OFObject*)obj
- (of_list_object_t*)appendObject: (OFObject*)obj
{
	of_list_object_t *o;

	o = [self allocMemoryWithSize: sizeof(of_list_object_t)];
	o->object = [obj retain];
	o->next = NULL;
	o->prev = last;
	o->prev = lastListObject;

	if (last != NULL)
		last->next = o;
	if (lastListObject != NULL)
		lastListObject->next = o;

	lastListObject = o;
	if (firstListObject == NULL)
	last = o;
	if (first == NULL)
		first = o;
		firstListObject = o;

	count++;

	[obj retain];

	return o;
}

- (of_list_object_t*)prependObject: (OFObject*)obj
{
	of_list_object_t *o;

	o = [self allocMemoryWithSize: sizeof(of_list_object_t)];
	o->object = [obj retain];
	o->next = firstListObject;
	o->prev = NULL;

	if (firstListObject != NULL)
		firstListObject->prev = o;

	firstListObject = o;
	if (lastListObject == NULL)
		lastListObject = o;

	count++;

	[obj retain];

	return o;
}

- (of_list_object_t*)prepend: (OFObject*)obj
{
	of_list_object_t *o;

	o = [self allocMemoryWithSize: sizeof(of_list_object_t)];
	o->object = [obj retain];
	o->next = first;
	o->prev = NULL;

	if (first != NULL)
		first->prev = o;

	first = o;
	if (last == NULL)
		last = o;

	count++;

	[obj retain];

	return o;
}

- (of_list_object_t*)insert: (OFObject*)obj
		     before: (of_list_object_t*)listobj
- (of_list_object_t*)insertObject: (OFObject*)obj
		 beforeListObject: (of_list_object_t*)listobj
{
	of_list_object_t *o;

	o = [self allocMemoryWithSize: sizeof(of_list_object_t)];
	o->object = [obj retain];
	o->next = listobj;
	o->prev = listobj->prev;

	if (listobj->prev != NULL)
		listobj->prev->next = o;

	listobj->prev = o;

	if (listobj == first)
		first = o;
	if (listobj == firstListObject)
		firstListObject = o;

	count++;

	[obj retain];

	return o;
}

- (of_list_object_t*)insert: (OFObject*)obj
		      after: (of_list_object_t*)listobj
- (of_list_object_t*)insertObject: (OFObject*)obj
		  afterListObject: (of_list_object_t*)listobj
{
	of_list_object_t *o;

	o = [self allocMemoryWithSize: sizeof(of_list_object_t)];
	o->object = [obj retain];
	o->next = listobj->next;
	o->prev = listobj;

	if (listobj->next != NULL)
		listobj->next->prev = o;

	listobj->next = o;

	if (listobj == last)
		last = o;
	if (listobj == lastListObject)
		lastListObject = o;

	count++;

	[obj retain];

	return o;
}

- (void)remove: (of_list_object_t*)listobj
- (void)removeListObject: (of_list_object_t*)listobj
{
	if (listobj->prev != NULL)
		listobj->prev->next = listobj->next;
	if (listobj->next != NULL)
		listobj->next->prev = listobj->prev;

	if (first == listobj)
		first = listobj->next;
	if (last == listobj)
		last = listobj->prev;
	if (firstListObject == listobj)
		firstListObject = listobj->next;
	if (lastListObject == listobj)
		lastListObject = listobj->prev;

	count--;

	[listobj->object release];

	[self freeMemory: listobj];
}
179
180
181
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
230
231
232
233
234
235
236
237
238
239
240

241
242
243
244
245
246
247
248
249
250
251
252
253
179
180
181
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
230
231
232
233
234
235
236
237
238
239
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254







-
-
+
+
+


















-
+





-
-
+
+














-
+











-
+














	if (![obj isKindOfClass: [OFList class]])
		return NO;

	if ([(OFList*)obj count] != count)
		return NO;

	for (iter = first, iter2 = [(OFList*)obj first]; iter != NULL &&
	    iter2 != NULL; iter = iter->next, iter2 = iter2->next)
	for (iter = firstListObject, iter2 = [(OFList*)obj firstListObject];
	    iter != NULL && iter2 != NULL;
	    iter = iter->next, iter2 = iter2->next)
		if (![iter->object isEqual: iter2->object])
			return NO;

	/* One is bigger than the other although we checked the count */
	assert(iter == NULL && iter2 == NULL);

	return YES;
}

- copy
{
	OFList *new = [[OFList alloc] init];
	of_list_object_t *iter, *o, *prev;

	o = NULL;
	prev = NULL;

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

			if (new->first == NULL)
				new->first = o;
			if (new->firstListObject == NULL)
				new->firstListObject = o;
			if (prev != NULL)
				prev->next = o;

			new->count++;

			[o->object retain];

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

	new->last = o;
	new->lastListObject = o;

	return new;
}

- (uint32_t)hash
{
	of_list_object_t *iter;
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (iter = first; iter != NULL; iter = iter->next) {
	for (iter = firstListObject; iter != NULL; iter = iter->next) {
		uint32_t h = [iter->object hash];

		OF_HASH_ADD(hash, h >> 24);
		OF_HASH_ADD(hash, (h >> 16) & 0xFF);
		OF_HASH_ADD(hash, (h >> 8) & 0xFF);
		OF_HASH_ADD(hash, h & 0xFF);
	}

	OF_HASH_FINALIZE(hash);

	return hash;
}
@end

Modified src/OFThread.m from [e64db843c5] to [ddd40f7181].

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
241

242
243
244
245
246
247
248
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
241

242
243
244
245
246
247
248
249







-
+
+


















-
+







}

+ (void)callAllDestructors
{
	of_list_object_t *iter;

	@synchronized (tlskeys) {
		for (iter = [tlskeys first]; iter != NULL; iter = iter->next)
		for (iter = [tlskeys firstListObject]; iter != NULL;
		    iter = iter->next)
			((OFTLSKey*)iter->object)->destructor(iter->object);
	}
}

- init
{
	self = [super init];

	if (!of_tlskey_new(&key)) {
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	destructor = NULL;

	@synchronized (tlskeys) {
		@try {
			listobj = [tlskeys append: self];
			listobj = [tlskeys appendObject: self];
		} @catch (OFException *e) {
			/*
			 * We can't use [super dealloc] on OS X here.
			 * Compiler bug? Anyway, [self dealloc] will do here
			 * as we check listobj != NULL in dealloc.
			 */
			listobj = NULL;
269
270
271
272
273
274
275
276

277
278
279
280
281
282
283
270
271
272
273
274
275
276

277
278
279
280
281
282
283
284







-
+







		destructor(self);

	of_tlskey_free(key);

	@synchronized (tlskeys) {
		/* In case we called [self dealloc] in init */
		if (listobj != NULL)
			[tlskeys remove: listobj];
			[tlskeys removeListObject: listobj];
	}

	[super dealloc];
}
@end

@implementation OFMutex

Modified tests/OFListTests.m from [f36ba55454] to [9238e3734a].

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







-
-
+
+

+
-
+

-
-
+
+

+
-
+

+
-
+

-
-
-
-
+
+
+
+
+
+

-
-
-
+
+
+
+


-
-
-
+
+
+
+




-
-
-
+
+
+






- (void)listTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFList *list;

	TEST(@"+[list]", (list = [OFList list]))

	TEST(@"-[append:]", [list append: strings[0]] &&
	    [list append: strings[1]] && [list append: strings[2]])
	TEST(@"-[appendObject:]", [list appendObject: strings[0]] &&
	    [list appendObject: strings[1]] && [list appendObject: strings[2]])

	TEST(@"-[firstListObject]",
	TEST(@"-[first]", [[list first]->object isEqual: strings[0]])
	    [[list firstListObject]->object isEqual: strings[0]])

	TEST(@"-[first]->next",
	    [[list first]->next->object isEqual: strings[1]])
	TEST(@"-[firstListObject]->next",
	    [[list firstListObject]->next->object isEqual: strings[1]])

	TEST(@"-[lastListObject]",
	TEST(@"-[last]", [[list last]->object isEqual: strings[2]])
	    [[list lastListObject]->object isEqual: strings[2]])

	TEST(@"-[lastListObject]->prev",
	TEST(@"-[last]->prev", [[list last]->prev->object isEqual: strings[1]])
	    [[list lastListObject]->prev->object isEqual: strings[1]])

	TEST(@"-[remove:]", R([list remove: [list last]]) &&
	    [[list last]->object isEqual: strings[1]] &&
	    R([list remove: [list first]]) &&
	    [[list first]->object isEqual: [list last]->object])
	TEST(@"-[removeListObject:]",
	    R([list removeListObject: [list lastListObject]]) &&
	    [[list lastListObject]->object isEqual: strings[1]] &&
	    R([list removeListObject: [list firstListObject]]) &&
	    [[list firstListObject]->object isEqual:
	    [list lastListObject]->object])

	TEST(@"-[insert:before:]", [list insert: strings[0]
					 before: [list last]] &&
	    [[list last]->prev->object isEqual: strings[0]])
	TEST(@"-[insertObject:beforeListObject:]",
	    [list insertObject: strings[0]
	      beforeListObject: [list lastListObject]] &&
	    [[list lastListObject]->prev->object isEqual: strings[0]])


	TEST(@"-[insert:after:]", [list insert: strings[2]
					 after: [list first]->next] &&
	    [[list last]->object isEqual: strings[2]])
	TEST(@"-[insertObject:afterListObject:]",
	    [list insertObject: strings[2]
	       afterListObject: [list firstListObject]->next] &&
	    [[list lastListObject]->object isEqual: strings[2]])

	TEST(@"-[count]", [list count] == 3)

	TEST(@"-[copy]", (list = [[list copy] autorelease]) &&
	    [[list first]->object isEqual: strings[0]] &&
	    [[list first]->next->object isEqual: strings[1]] &&
	    [[list last]->object isEqual: strings[2]])
	    [[list firstListObject]->object isEqual: strings[0]] &&
	    [[list firstListObject]->next->object isEqual: strings[1]] &&
	    [[list lastListObject]->object isEqual: strings[2]])

	TEST(@"-[isEqual:]", [list isEqual: [[list copy] autorelease]])

	[pool drain];
}
@end