ObjFW  Check-in [ea3b6ef066]

Overview
Comment:Store initialization parameters of exceptions.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ea3b6ef06609b0587d1933a80e92fbf63cb590e033b81cc0411728bae43310da
User & Date: js on 2008-10-30 00:49:41
Other Links: manifest | tags
Context
2008-10-30
00:51
Forgot to change 2 inits. check-in: 6b0fefe13c user: js tags: trunk
00:49
Store initialization parameters of exceptions. check-in: ea3b6ef066 user: js tags: trunk
2008-10-29
17:29
Add getMemForNItems:withSize: & resizeMem:toNItems:withSize: in OFObject check-in: 49859c2bcc user: js tags: trunk
Changes

Modified src/OFExceptions.h from [039a0e8d07] to [a8e30d777d].

12
13
14
15
16
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
#import <stddef.h>
#import "OFObject.h"

// FIXME: Exceptions should include which type of error occoured (fopen etc.)

@interface OFException: OFObject
{

	char *errstr;
}

+ newWithObject: (id)obj;
- initWithObject: (id)obj;
- free;
- raise;
- (char*)string;
@end

@interface OFNoMemException: OFException




+ newWithObject: (id)obj
	andSize: (size_t)size;
- initWithObject: (id)obj
	 andSize: (size_t)size;


@end

@interface OFNotImplementedException: OFException




+ newWithObject: (id)obj
    andSelector: (SEL)sel;
- initWithObject: (id)obj
     andSelector: (SEL)sel;


@end

@interface OFMemNotPartOfObjException: OFException




+ newWithObject: (id)obj
     andPointer: (void*)ptr;
- initWithObject: (id)obj
      andPointer: (void*)ptr;


@end

@interface OFOverflowException: OFException
+ newWithObject: (id)obj;
- initWithObject: (id)obj;
@end

@interface OFOpenFileFailedException: OFException





+ newWithObject: (id)obj
	andPath: (const char*)path
	andMode: (const char*)mode;
- initWithObject: (id)obj
	 andPath: (const char*)path
	 andMode: (const char*)mode;




@end

@interface OFReadOrWriteFailedException: OFException





+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems;
@end

@interface OFReadFailedException: OFReadOrWriteFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems;






@end

@interface OFWriteFailedException: OFReadOrWriteFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems;
@end







>
|






|



>
>
>
>




>
>



>
>
>
>




>
>



>
>
>
>




>
>








>
>
>
>
>

|
|

|
|
>
>
>
>



>
>
>
>
>



<
<
<



>
>
>
>
>
>



|
<
<

12
13
14
15
16
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
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 <stddef.h>
#import "OFObject.h"

// FIXME: Exceptions should include which type of error occoured (fopen etc.)

@interface OFException: OFObject
{
	id   object;
	char *string;
}

+ newWithObject: (id)obj;
- initWithObject: (id)obj;
- free;
- raise;
- (char*)cString;
@end

@interface OFNoMemException: OFException
{
	size_t req_size;
}

+ newWithObject: (id)obj
	andSize: (size_t)size;
- initWithObject: (id)obj
	 andSize: (size_t)size;
- (char*)cString;
- (size_t)requestedSize;
@end

@interface OFNotImplementedException: OFException
{
	SEL selector;
}

+ newWithObject: (id)obj
    andSelector: (SEL)sel;
- initWithObject: (id)obj
     andSelector: (SEL)sel;
- (char*)cString;
- (SEL)selector;
@end

@interface OFMemNotPartOfObjException: OFException
{
	void *pointer;
}

+ newWithObject: (id)obj
     andPointer: (void*)ptr;
- initWithObject: (id)obj
      andPointer: (void*)ptr;
- (char*)cString;
- (void*)pointer;
@end

@interface OFOverflowException: OFException
+ newWithObject: (id)obj;
- initWithObject: (id)obj;
@end

@interface OFOpenFileFailedException: OFException
{
	char *path;
	char *mode;
}

+ newWithObject: (id)obj
	andPath: (const char*)p
	andMode: (const char*)m;
- initWithObject: (id)obj
	 andPath: (const char*)p
	 andMode: (const char*)m;
- free;
- (char*)cString;
- (char*)path;
- (char*)mode;
@end

@interface OFReadOrWriteFailedException: OFException
{
	size_t req_size;
	size_t req_items;
}

+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems;



- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems;
- (size_t)requestedSize;
- (size_t)requestedItems;
@end

@interface OFReadFailedException: OFReadOrWriteFailedException
- (char*)cString;
@end

@interface OFWriteFailedException: OFReadOrWriteFailedException
- (char*)cString;


@end

Modified src/OFExceptions.m from [c11f760527] to [2f901d9233].

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










172
173
174
175
176
177
178
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
+ newWithObject: (id)obj
{
	return [[OFException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	if ((self = [super init]))

		errstr = NULL;


	return self;
}

- free
{
	if (errstr != NULL)
		free(errstr);

	return [super free];
}

- raise
{
	@throw self;
	return self;
}

- (char*)string
{
	return errstr;
}
@end

@implementation OFNoMemException
+ newWithObject: (id)obj
	andSize: (size_t)size
{
	return [[OFNoMemException alloc] initWithObject: obj
						andSize: size];
}

- initWithObject: (id)obj
	 andSize: (size_t)size
{
	if ((self = [super init])) {
		if (obj != nil)
			asprintf(&errstr, "ERROR: Could not allocate %zu bytes "


			    "for object of class %s!\n", size, [obj name]);




		else
			asprintf(&errstr, "ERROR: Could not allocate %zu bytes "

			    "for object of class (null)!\n", size);

	}



	return self;
}
@end

@implementation OFNotImplementedException
+ newWithObject: (id)obj
    andSelector: (SEL)sel
{
	return [[OFNotImplementedException alloc] initWithObject: obj
						     andSelector: sel];
}

- initWithObject: (id)obj
     andSelector: (SEL)sel
{
	if ((self = [super init]))
		asprintf(&errstr, "ERROR: Requested selector %s not "
		    "implemented in %s!\n", SEL_NAME(sel), [obj name]);

	return self;
}
















@end

@implementation OFMemNotPartOfObjException
+ newWithObject: (id)obj
     andPointer: (void*)ptr
{
	return [[OFMemNotPartOfObjException alloc] initWithObject: obj
						       andPointer: ptr];
}

- initWithObject: (id)obj
      andPointer: (void*)ptr
{
	if ((self = [super init]))










		asprintf(&errstr, "ERROR: Memory at %p was not allocated as "
		    "part of object of class\n"
		    "ERROR: %s!\n"
		    "ERROR: -> Not changing memory allocation!\n"
		    "ERROR: (Hint: It is possible that you tried to free the "
		    "same memory twice!)\n", ptr, [obj name]);

	return self;
}





@end

@implementation OFOverflowException
+ newWithObject: (id)obj
{
	return [[OFOverflowException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	if ((self = [super init])) {

		if (obj != nil)
			asprintf(&errstr, "ERROR: Overflow in object of class "
			    "%s!\n", [obj name]);



		else
			errstr = strdup("ERROR: Overflow in object of class "
			    "(null)!\n");
	}

	return self;
}
@end

@implementation OFOpenFileFailedException
+ newWithObject: (id)obj
	andPath: (const char*)path
	andMode: (const char*)mode
{
	return [[OFOpenFileFailedException alloc] initWithObject: obj
							 andPath: path
							 andMode: mode];
}

- initWithObject: (id)obj
	 andPath: (const char*)path
	 andMode: (const char*)mode
{
	if ((self = [super init]))






















		asprintf(&errstr, "ERROR: Failed to open file %s with mode %s "
		    "in object of class %s!\n", path, mode, [self name]);

	return self;
}










@end

@implementation OFReadOrWriteFailedException
+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems
{
	return [[OFReadOrWriteFailedException alloc] initWithObject: obj
							    andSize: size
							  andNItems: nitems];
}
@end

@implementation OFReadFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems
{
	if ((self = [super init]))
























		asprintf(&errstr, "ERROR: Failed to read %zu items of size "
		    "%zu in object of class %s!\n", nitems, size, [obj name]);

	return self;
}
@end

@implementation OFWriteFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems
{


	if ((self = [super init]))
		asprintf(&errstr, "ERROR: Failed to write %zu items of size "
		    "%zu in object of class %s!\n", nitems, size, [obj name]);

	return self;
}
@end







|
>
|
>






|
|










|

|














|
|
|
>
>
|
>
>
>
>
|
|
>
|
>
|

>
>
|














|
|
<



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













|
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|

|

>
>
>
>
>










|
>
|
<
|
>
>
>
|
|
|
|
<
|





|
|


|
|



|
|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|

|

>
>
>
>
>
>
>
>
>
>











<

<




|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|

|




<
|
<

>
>
|
|
|

|


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
170
171
172
173
174
175
176
177
178
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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

297

298
299
300
301
302
303
304
305
306
307
+ newWithObject: (id)obj
{
	return [[OFException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	if ((self = [super init])) {
		object = obj;
		string = NULL;
	}

	return self;
}

- free
{
	if (string != NULL)
		free(string);

	return [super free];
}

- raise
{
	@throw self;
	return self;
}

- (char*)cString
{
	return string;
}
@end

@implementation OFNoMemException
+ newWithObject: (id)obj
	andSize: (size_t)size
{
	return [[OFNoMemException alloc] initWithObject: obj
						andSize: size];
}

- initWithObject: (id)obj
	 andSize: (size_t)size
{
	if ((self = [super initWithObject: obj]))
		req_size = size;

	return self;
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Could not allocate %zu bytes for object of"
	    "class %s!\n", req_size, object != nil ? [object name] : "(null)");

	return string;
}

- (size_t)requestedSize
{
	return req_size;
}
@end

@implementation OFNotImplementedException
+ newWithObject: (id)obj
    andSelector: (SEL)sel
{
	return [[OFNotImplementedException alloc] initWithObject: obj
						     andSelector: sel];
}

- initWithObject: (id)obj
     andSelector: (SEL)sel
{
	if ((self = [super initWithObject: obj]))
		selector = sel;


	return self;
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Requested selector %s not implemented in "
	    "%s!\n", SEL_NAME(selector), [object name]);

	return string;
}

- (SEL)selector
{
	return selector;
}
@end

@implementation OFMemNotPartOfObjException
+ newWithObject: (id)obj
     andPointer: (void*)ptr
{
	return [[OFMemNotPartOfObjException alloc] initWithObject: obj
						       andPointer: ptr];
}

- initWithObject: (id)obj
      andPointer: (void*)ptr
{
	if ((self = [super initWithObject: obj]))
		pointer = ptr;

	return self;
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Memory at %p was not allocated as part of "
	    "object of class\n"
	    "ERROR: %s!\n"
	    "ERROR: -> Not changing memory allocation!\n"
	    "ERROR: (Hint: It is also possible that you tried to free the same "
	    "memory twice!)\n", pointer, [object name]);

	return string;
}

- (void*)pointer
{
	return pointer;
}
@end

@implementation OFOverflowException
+ newWithObject: (id)obj
{
	return [[OFOverflowException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	return (self = [super initWithObject: obj]);
}


- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Overflow in object of class %s!\n",
	    object != nil ? [object name] : "(null)");


	return string;
}
@end

@implementation OFOpenFileFailedException
+ newWithObject: (id)obj
	andPath: (const char*)p
	andMode: (const char*)m
{
	return [[OFOpenFileFailedException alloc] initWithObject: obj
							 andPath: p
							 andMode: m];
}

- initWithObject: (id)obj
	 andPath: (const char*)p
	 andMode: (const char*)m
{
	if ((self = [super init])) {
		path = p != NULL ? strdup(p) : NULL;
		mode = m != NULL ? strdup(m) : NULL;
	}

	return self;
}

- free
{
	if (path != NULL)
		free(path);
	if (mode != NULL)
		free(mode);

	return [super free];
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Failed to open file %s with mode %s "
	    "in object of class %s!\n", path, mode, [self name]);

	return string;
}

- (char*)path
{
	return path;
}

- (char*)mode
{
	return mode;
}
@end

@implementation OFReadOrWriteFailedException
+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems
{
	return [[OFReadOrWriteFailedException alloc] initWithObject: obj
							    andSize: size
							  andNItems: nitems];
}



- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems
{
	if ((self = [super init])) {
		req_size = size;
		req_items = nitems;
	}

	return self;
}

- (size_t)requestedSize
{
	return req_size;
}

- (size_t)requestedItems
{
	return req_items;
}
@end

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

	asprintf(&string, "ERROR: Failed to read %zu items of size %zu in "
	    "object of class %s!\n", req_items, req_size, [object name]);

	return string;
}
@end

@implementation OFWriteFailedException

- (char*)cString

{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Failed to write %zu items of size %zu in "
	    "object of class %s!\n", req_items, req_size, [object name]);

	return string;
}
@end

Modified tests/OFObject/OFObject.m from [15b4656509] to [ad58ee962e].

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define CATCH_EXCEPTION(code, exception)		\
	caught = false;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = true;				\
		puts("CAUGHT! Error string was:");	\
		fputs([e string], stdout);		\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}








|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define CATCH_EXCEPTION(code, exception)		\
	caught = false;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = true;				\
		puts("CAUGHT! Error string was:");	\
		fputs([e cString], stdout);		\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}