C Tool Box 0.1.0
Loading...
Searching...
No Matches
ctb.h
Go to the documentation of this file.
1/**
2 * @file
3 */
4#ifndef CTB_H
5#define CTB_H
6
7/**
8 * @brief Creates an array of the given type and length
9 *
10 * @param itemType Type of each item in the array
11 * @param length Number of items in the array
12 */
13#define ctb_arr(itemType, length) \
14 ((itemType[length]){ 0 })
15
16/**
17 * @brief Creates an object of the given type and initializes it with zeros
18 *
19 * @param type Type of the object
20 */
21#define ctb_obj(type) \
22 ((type){ 0 })
23
24/**
25 * @brief Calculates the length of the given array
26 *
27 * @param arr Array to calculate the length from
28 */
29#define ctb_lengthOf(arr) \
30 (sizeof(arr) / sizeof(arr[0]))
31
32/**
33 * @brief Calculates the address of the containing object
34 *
35 * @param obj Object to calculate the containing object from
36 * @param type Type of the containing object
37 * @param member Member of the containing object
38 */
39#define ctb_containerOf(obj, type, member) \
40 ((type *)((char *)(obj) - offsetof(type, member)))
41
42/**
43 * @brief Generates a compile-time assertion
44 *
45 * @param expression Expression to assert
46 * @param identifier Identifier to use in the assertion
47 */
48#define ctb_staticAssert(expression, identifier) \
49 typedef char identifier[(!!(expression)) * 2 - 1]
50
51#endif // CTB_H