Neale Pickett
·
2008-04-14
obj.h
1/* obj.h: objecty and exceptiony stuff
2 *
3 * Some macros to make C a bit more like C++, but without bringing in
4 * all of C++'s crapola.
5 */
6
7#ifndef __OBJ_H__
8#define __OBJ_H__
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <errno.h>
14
15/** Exception-type things. Don't nest them.
16 *
17 * These allow you to have pseudo-exceptions. It looks kludgy and it
18 * is, but I think it makes the actual code easier to understand.
19 */
20static char *exception;
21#define try for (exception = "Failure"; exception; exception = NULL)
22#define fail break
23#define pfail {exception = strerror(errno); break;}
24#define succeed continue
25#define raise(x) {exception = x; break;}
26#define except if (exception)
27
28/** Allocate something */
29#define new(type) (type *)calloc(1, sizeof(type))
30
31/** Clear something out */
32#define zero(x) (void)memset(&x, 0, sizeof(x))
33
34/** Printf debugging macros */
35#ifdef NODUMP
36# define DUMPf(fmt, args...)
37#else
38# define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
39#endif
40#define DUMP() DUMPf("")
41#define DUMP_d(v) DUMPf("%s = %d", #v, v)
42#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
43#define DUMP_s(v) DUMPf("%s = %s", #v, v)
44#define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v)
45#define DUMP_p(v) DUMPf("%s = %p", #v, v)
46
47#endif