ObjFW  Check-in [bdc9b3219f]

Overview
Comment:Get rid of OFNotInSetException.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bdc9b3219f92d3ff28c3010c8f4690f7d1e65087dc93ebe8a7a9e41d470a190c
User & Date: js on 2009-05-14 14:24:02
Other Links: manifest | tags
Context
2009-05-15
20:01
Use OFString as much as possible. check-in: 67066be692 user: js tags: trunk
2009-05-14
14:24
Get rid of OFNotInSetException. check-in: bdc9b3219f user: js tags: trunk
13:46
Two small fixes for OFAutoreleasePool. check-in: af41efe079 user: js tags: trunk
Changes

Modified src/OFAutoreleasePool.m from [1d59e36e29] to [3f9b8ad9ca].

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
+ (void)initialize
{
	pool_list_key = [[OFTLSKey alloc] initWithDestructor: release_list];
}

+ (void)addToPool: (OFObject*)obj
{
	OFList *pool_list;

	@try {
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	} @catch (OFNotInSetException *e) {
		[e dealloc];
		[[self alloc] init];
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	}

	if ([pool_list last] == NULL)
		[[self alloc] init];

	if ([pool_list last] == NULL)
		@throw [OFInitializationFailedException newWithClass: self];

	[[pool_list last]->object addToPool: obj];
}

- init
{
	OFList *pool_list;

	self = [super init];

	objects = nil;

	@try {
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	} @catch (OFNotInSetException *e) {
		[e dealloc];
		pool_list = [[OFList alloc] initWithoutRetainAndRelease];
		[OFThread setObject: pool_list
			  forTLSKey: pool_list_key];
		[pool_list release];
	}

	listobj = [pool_list append: self];







|

<
|
<
<




|
<
<
<













<
|
<
<







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
+ (void)initialize
{
	pool_list_key = [[OFTLSKey alloc] initWithDestructor: release_list];
}

+ (void)addToPool: (OFObject*)obj
{
	OFList *pool_list = [OFThread objectForTLSKey: pool_list_key];


	if (pool_list == nil || [pool_list last] == NULL) {


		[[self alloc] init];
		pool_list = [OFThread objectForTLSKey: pool_list_key];
	}

	if (pool_list == nil || [pool_list last] == NULL)



		@throw [OFInitializationFailedException newWithClass: self];

	[[pool_list last]->object addToPool: obj];
}

- init
{
	OFList *pool_list;

	self = [super init];

	objects = nil;


	if ((pool_list = [OFThread objectForTLSKey: pool_list_key]) == nil) {


		pool_list = [[OFList alloc] initWithoutRetainAndRelease];
		[OFThread setObject: pool_list
			  forTLSKey: pool_list_key];
		[pool_list release];
	}

	listobj = [pool_list append: self];

Modified src/OFDataArray.m from [f9593924bb] to [cbd581ad22].

39
40
41
42
43
44
45
46

47
48
49
50
51
52
53
	Class c;

	self = [super init];

	if (is == 0) {
		c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c];

	}

	data = NULL;
	itemsize = is;
	count = 0;

	return self;







|
>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	Class c;

	self = [super init];

	if (is == 0) {
		c = isa;
		[super dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						    andSelector: _cmd];
	}

	data = NULL;
	itemsize = is;
	count = 0;

	return self;

Modified src/OFDictionary.h from [7c028d029d] to [3f6c3d3146].

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 * \param obj The object to set the key to
 */
- set: (OFObject*)key
   to: (OFObject*)obj;

/**
 * \param key The key whose object should be returned
 * \return The object for the given key
 */
- (id)get: (OFObject*)key;

/**
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed







|







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 * \param obj The object to set the key to
 */
- set: (OFObject*)key
   to: (OFObject*)obj;

/**
 * \param key The key whose object should be returned
 * \return The object for the given key or nil if the key was not found
 */
- (id)get: (OFObject*)key;

/**
 * Remove the object with the given key from the dictionary.
 *
 * \param key The key whose object should be removed

Modified src/OFDictionary.m from [967868d132] to [ab1ae07d78].

99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
- set: (OFObject*)key
   to: (OFObject*)obj
{
	uint32_t hash;
	of_list_object_t *iter;

	if (key == nil || obj == nil)
		@throw [OFInvalidArgumentException newWithClass: isa];


	hash = [key hash] & (size - 1);

	if (data[hash] == nil)
		data[hash] = [[OFList alloc] init];

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) {







|
>







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
- set: (OFObject*)key
   to: (OFObject*)obj
{
	uint32_t hash;
	of_list_object_t *iter;

	if (key == nil || obj == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	hash = [key hash] & (size - 1);

	if (data[hash] == nil)
		data[hash] = [[OFList alloc] init];

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) {
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183

- (id)get: (OFObject*)key
{
	uint32_t hash;
	of_list_object_t *iter;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa];


	hash = [key hash] & (size - 1);

	if (data[hash] == nil)
		@throw [OFNotInSetException newWithClass: isa];

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next)
		if ([iter->object isEqual: key])
			return iter->next->object;

	@throw [OFNotInSetException newWithClass: isa];
}

- remove: (OFObject*)key
{
	uint32_t hash;
	of_list_object_t *iter;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa];


	hash = [key hash] & (size - 1);

	if (data[hash] == nil)
		@throw [OFNotInSetException newWithClass: isa];

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) {
		if ([iter->object isEqual: key]) {
			[data[hash] remove: iter->next];
			[data[hash] remove: iter];

			if ([data[hash] first] == NULL) {
				[data[hash] release];
				data[hash] = nil;
			}

			return self;
		}
	}

	@throw [OFNotInSetException newWithClass: isa];
}

- (float)averageItemsPerBucket
{
	size_t items, buckets, i;

	items = 0;







|
>




|





|








|
>




|















|







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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

- (id)get: (OFObject*)key
{
	uint32_t hash;
	of_list_object_t *iter;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	hash = [key hash] & (size - 1);

	if (data[hash] == nil)
		return nil;

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next)
		if ([iter->object isEqual: key])
			return iter->next->object;

	return nil;
}

- remove: (OFObject*)key
{
	uint32_t hash;
	of_list_object_t *iter;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	hash = [key hash] & (size - 1);

	if (data[hash] == nil)
		return self;

	for (iter = [data[hash] first]; iter != NULL; iter = iter->next->next) {
		if ([iter->object isEqual: key]) {
			[data[hash] remove: iter->next];
			[data[hash] remove: iter];

			if ([data[hash] first] == NULL) {
				[data[hash] release];
				data[hash] = nil;
			}

			return self;
		}
	}

	return self;
}

- (float)averageItemsPerBucket
{
	size_t items, buckets, i;

	items = 0;

Modified src/OFExceptions.h from [df20b2266d] to [046ef80792].

212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

/**
 * An OFException indicating that initializing something failed.
 */
@interface OFInitializationFailedException: OFException {}
@end

/**
 * An OFException indicating that the requested key is not in the set.
 */
@interface OFNotInSetException: OFException {}
@end

/**
 * An OFException indicating the file couldn't be opened.
 */
@interface OFOpenFileFailedException: OFException
{
	char *path;
	char *mode;







<
<
<
<
<
<







212
213
214
215
216
217
218






219
220
221
222
223
224
225

/**
 * An OFException indicating that initializing something failed.
 */
@interface OFInitializationFailedException: OFException {}
@end







/**
 * An OFException indicating the file couldn't be opened.
 */
@interface OFOpenFileFailedException: OFException
{
	char *path;
	char *mode;

Modified src/OFExceptions.m from [0cbb296450] to [89c27be317].

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Initialization failed for class %s!", [class name]);

	return string;
}
@end

@implementation OFNotInSetException
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "The requested key is not in the set of type %s!",
	    [class name]);

	return string;
}
@end

@implementation OFOpenFileFailedException
+ newWithClass: (Class)class_
       andPath: (const char*)path_







<
<
<
<
<
<
<
<
<
<
<
<
<







265
266
267
268
269
270
271













272
273
274
275
276
277
278
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Initialization failed for class %s!", [class name]);














	return string;
}
@end

@implementation OFOpenFileFailedException
+ newWithClass: (Class)class_
       andPath: (const char*)path_

Modified src/OFIterator.m from [e57d8be90e] to [7c4dd9ded2].

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

- (id)nextObject
{
	if (last == NULL) {
		for (; pos < size && data[pos] == nil; pos++);
		if (pos == size)
			@throw [OFNotInSetException
			    newWithClass: [OFDictionary class]];

		return (last = [data[pos++] first])->object;
	}

	if ((last = last->next) != NULL)
		return last->object;








|
<







31
32
33
34
35
36
37
38

39
40
41
42
43
44
45
}

- (id)nextObject
{
	if (last == NULL) {
		for (; pos < size && data[pos] == nil; pos++);
		if (pos == size)
			return nil;


		return (last = [data[pos++] first])->object;
	}

	if ((last = last->next) != NULL)
		return last->object;

Modified src/OFString.m from [7666d17f06] to [5fed7eca62].

86
87
88
89
90
91
92
93

94
95
96
97
98
99
100

	return YES;
}

- (int)compare: (id)obj
{
	if (![obj isKindOf: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa];


	return strcmp(string, [obj cString]);
}

- (uint32_t)hash
{
	uint32_t hash;







|
>







86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

	return YES;
}

- (int)compare: (id)obj
{
	if (![obj isKindOf: [OFString class]])
		@throw [OFInvalidArgumentException newWithClass: isa
						    andSelector: _cmd];

	return strcmp(string, [obj cString]);
}

- (uint32_t)hash
{
	uint32_t hash;

Modified src/OFThread.m from [e453479303] to [f142cdc00e].

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
	 */
	return [[[self alloc] initWithObject: obj] autorelease];
}

+ setObject: (id)obj
  forTLSKey: (OFTLSKey*)key
{
	id old;

	@try {
		old = [self objectForTLSKey: key];
	} @catch (OFNotInSetException *e) {
		[e dealloc];
		old = nil;
	}

#ifndef _WIN32
	if (pthread_setspecific(key->key, obj))
#else
	if (!TlsSetValue(key->key, obj))
#endif
		/* FIXME: Maybe another exception would be better */
		@throw [OFNotInSetException newWithClass: self];


	if (obj != nil)
		[obj retain];
	if (old != nil)
		[old release];

	return self;







<
<
<
|
<
<
<
<







|
>







44
45
46
47
48
49
50



51




52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	 */
	return [[[self alloc] initWithObject: obj] autorelease];
}

+ setObject: (id)obj
  forTLSKey: (OFTLSKey*)key
{



	id old = [self objectForTLSKey: key];





#ifndef _WIN32
	if (pthread_setspecific(key->key, obj))
#else
	if (!TlsSetValue(key->key, obj))
#endif
		/* FIXME: Maybe another exception would be better */
		@throw [OFInvalidArgumentException newWithClass: self
						    andSelector: _cmd];

	if (obj != nil)
		[obj retain];
	if (old != nil)
		[old release];

	return self;
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#endif

	/*
	 * NULL and nil might be different on some platforms. NULL is returned
	 * if the key is missing, nil can be returned if it was explicitly set
	 * to nil to release the old object.
	 */
	if (ret == NULL || (id)ret == nil)
		@throw [OFNotInSetException newWithClass: self];

	return (id)ret;
}

- initWithObject: (id)obj
{
	Class c;







|
|







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#endif

	/*
	 * NULL and nil might be different on some platforms. NULL is returned
	 * if the key is missing, nil can be returned if it was explicitly set
	 * to nil to release the old object.
	 */
	if (ret == NULL)
		return nil;

	return (id)ret;
}

- initWithObject: (id)obj
{
	Class c;

Modified tests/OFAutoreleasePool/OFAutoreleasePool.m from [ca3ccd4ed1] to [0efd8e9184].

94
95
96
97
98
99
100


101
102
	o2 = [[[OFObject alloc] init] autorelease];

	pool2 = [[OFAutoreleasePool alloc] init];
	o3 = [[[OFObject alloc] init] autorelease];

	[pool1 release];



	return (inits == 20 && retains == 5 && releases == 16 ? 0 : 1);
}







>
>
|

94
95
96
97
98
99
100
101
102
103
104
	o2 = [[[OFObject alloc] init] autorelease];

	pool2 = [[OFAutoreleasePool alloc] init];
	o3 = [[[OFObject alloc] init] autorelease];

	[pool1 release];

	printf("inits:    %02d\nretains:  %02d\nreleases: %02d\n",
	   inits, retains, releases);
	return (inits == 17 && retains == 5 && releases == 16 ? 0 : 1);
}

Modified tests/OFDictionary/OFDictionary.m from [47966a2668] to [3731d42c29].

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
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
#import "OFConstString.h"
#import "OFString.h"
#import "OFExceptions.h"

int
main()
{
	BOOL caught;

	OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16];
	OFIterator *iter = [dict iterator];

	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *key1 = [OFString stringWithCString: "key1"];
	OFString *key2 = [OFString stringWithCString: "key2"];
	OFString *value1 = [OFString stringWithCString: "value1"];
	OFString *value2 = [OFString stringWithCString: "value2"];

	[dict set: key1
	       to: value1];
	[dict set: key2
	       to: value2];
	[pool release];

	if (strcmp([[dict get: @"key1"] cString], "value1")) {
		puts("\033[K\033[1;31mTest 1/8 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/8 failed!\033[m");
		return 1;
	}

	if (![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"] ||
	    ![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"]) {
		puts("\033[K\033[1;31mTest 3/8 failed!\033[m");
		return 1;
	}

	[dict changeHashSize: 8];
	iter = [dict iterator];
	if (![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"] ||
	    ![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"]) {
		puts("\033[K\033[1;31mTest 4/8 failed!\033[m");
		return 1;
	}

	if ([dict averageItemsPerBucket] != 1.0) {
		puts("\033[K\033[1;31mTest 5/8 failed!\033[m");
		return 1;
	}

	caught = NO;
	@try {
		[iter nextObject];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 6/8 failed!\033[m");
		return 1;
	}

	caught = NO;
	@try {
		[dict get: @"key3"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 7/8 failed!\033[m");
		return 1;
	}

	[dict remove: @"key2"];
	caught = NO;
	@try {
		[dict remove: @"key2"];
	} @catch (OFNotInSetException *e) {
		caught = YES;
	}
	if (!caught) {
		puts("\033[K\033[1;31mTest 8/8 failed!\033[m");
		return 1;
	}

	puts("\033[1;32mTests successful: 8/8\033[0m");

	return 0;
}







<
<
















|




|







|









|




|



<
<
|
<
<
<
<
|



<
<
|
<
<
<
<
|



<
<
<
<
<
<
<
<
<
<
<
<
|



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
82
83
84












85
86
87
88
#import "OFConstString.h"
#import "OFString.h"
#import "OFExceptions.h"

int
main()
{


	OFDictionary *dict = [OFDictionary dictionaryWithHashSize: 16];
	OFIterator *iter = [dict iterator];

	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *key1 = [OFString stringWithCString: "key1"];
	OFString *key2 = [OFString stringWithCString: "key2"];
	OFString *value1 = [OFString stringWithCString: "value1"];
	OFString *value2 = [OFString stringWithCString: "value2"];

	[dict set: key1
	       to: value1];
	[dict set: key2
	       to: value2];
	[pool release];

	if (strcmp([[dict get: @"key1"] cString], "value1")) {
		puts("\033[K\033[1;31mTest 1/7 failed!\033[m");
		return 1;
	}

	if (strcmp([[dict get: key2] cString], "value2")) {
		puts("\033[K\033[1;31mTest 2/7 failed!\033[m");
		return 1;
	}

	if (![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"] ||
	    ![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"]) {
		puts("\033[K\033[1;31mTest 3/7 failed!\033[m");
		return 1;
	}

	[dict changeHashSize: 8];
	iter = [dict iterator];
	if (![[iter nextObject] isEqual: @"key1"] ||
	    ![[iter nextObject] isEqual: @"value1"] ||
	    ![[iter nextObject] isEqual: @"key2"] ||
	    ![[iter nextObject] isEqual: @"value2"]) {
		puts("\033[K\033[1;31mTest 4/7 failed!\033[m");
		return 1;
	}

	if ([dict averageItemsPerBucket] != 1.0) {
		puts("\033[K\033[1;31mTest 5/7 failed!\033[m");
		return 1;
	}



	if ([iter nextObject] != nil) {




		puts("\033[K\033[1;31mTest 6/7 failed!\033[m");
		return 1;
	}



	if ([dict get: @"key3"] != nil) {




		puts("\033[K\033[1;31mTest 7/7 failed!\033[m");
		return 1;
	}













	puts("\033[1;32mTests successful: 7/7\033[0m");

	return 0;
}