4 * @brief General purpose definitions and macros.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 * Copyright (C) 1998, 1999 D. Hugh Redelmeier. (Endian stuff)
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #ifndef DEFINITIONS_H_
26 #define DEFINITIONS_H_
30 #define BITS_PER_BYTE 8
33 * Default length for various auxiliary text buffers
38 * Macro compares two strings for equality
40 #define streq(x,y) (strcmp(x, y) == 0)
43 * Macro compares two binary blobs for equality
45 #define memeq(x,y,len) (memcmp(x, y, len) == 0)
48 * Macro gives back larger of two values.
50 #define max(x,y) ((x) > (y) ? (x):(y))
53 * Macro gives back smaller of two values.
55 #define min(x,y) ((x) < (y) ? (x):(y))
58 * Call destructor of a object if object != NULL
60 #define DESTROY_IF(obj) if (obj) obj->destroy(obj)
63 * Debug macro to follow control flow
65 #define POS printf("%s, line %d\n", __FILE__, __LINE__)
68 * Macro to allocate a sized type.
70 #define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
73 * Assign a function as a class method
75 #define ASSIGN(method, function) (method = (typeof(method))function)
78 * printf() specifier to resolf enum names, see enum_names
80 #define ENUM_PRINTF_SPEC 'N'
82 typedef struct enum_name_t enum_name_t
;
85 * Struct to store names for enums. Use the convenience macros
87 * For a single range, use:
88 * ENUM(name, first, last, string1, string2, ...)
90 * For multiple ranges, use:
91 * ENUM_BEGIN(name, first, last, string1, string2, ...)
92 * ENUM_NEXT(name, first, last, last_from_previous, string3, ...)
93 * ENUM_NEXT(name, first, last, last_from_previous, string4, ...)
94 * ENUM_END(name, last_from_previous)
103 #define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }}
104 #define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
105 #define ENUM_END(name, prev) enum_name_t *name = &name##prev;
106 #define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
108 #endif /*DEFINITIONS_H_*/