-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.h
58 lines (52 loc) · 1.04 KB
/
obj.h
1
2
3
4
5
6
7
8
9
10
11
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
#ifndef __EXCEPT_H__
#define __EXCEPT_H__
#include <stdlib.h>
/* obj.h: objecty and exceptiony stuff
*
* Some macros to make C a bit more like C++, but without bringing in
* all of C++'s crapola.
*/
/* Here's an example:
*
* int
* foo()
* {
* struct bar *b = NULL;
* FILE *f = NULL;
*
* attempt {
* b = new(struct bar);
* if (! b) fail;
*
* f = fopen("foo", "r");
* if (! f) fail;
*
* (void)fgets(b->baz, 10, f);
* }
*
* if (f) {
* (void)fclose(f);
* }
*
* recover {
* if (b) {
* free(b);
* }
* return -1;
* }
*
* return 0;
* }
*/
/** Exception-type things
*
* These allow you to have pseudo-exceptions. It looks kludgy and it
* is, but it's only that way so you can have nice pretty code.
*/
static int __obj_passed = 0;
#define attempt for (__obj_passed = 0; !__obj_passed; __obj_passed = 1)
#define fail break
#define succeed continue
#define recover if (__obj_passed ? (__obj_passed = 0) : 1)
#define new(type) (type *)calloc(1, sizeof(type))
#endif