-
Notifications
You must be signed in to change notification settings - Fork 2
/
typeclass.h
50 lines (46 loc) · 2.08 KB
/
typeclass.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
/**
* @file
* Utilities to define a typeclass and its instance.
*/
#ifndef IT_TYPECLASS_H
#define IT_TYPECLASS_H
/**
* @def typeclass(funcs)
* @brief Define a typeclass with the given functions.
*
* # Example
*
* @code
* typedef typeclass(char* (*show)(void* self)) Show; // Defines a typeclass and names it `Show`
* @endcode
*
* @param funcs A semicolon separated list of typeclass functions.
*
* @note The functions usually take the `self` from the typeclass instance (and possibly more arguments).
*/
#define typeclass(funcs) \
struct \
{ \
funcs; \
}
/**
* @def typeclass_instance(Typeclass)
* @brief Define a typeclass instance for the given typeclass.
*
* This just contains a `void* self` member and the typeclass itself.
* # Example
*
* @code
* typedef typeclass(char* (*show)(void* self)) Show;
* typedef typeclass_instance(Show) Showable; // Defines the typeclass instance for `Show` typeclass
* @endcode
*
* @param Typeclass The semantic type (C type) of the typeclass defined with #typeclass(funcs).
*/
#define typeclass_instance(Typeclass) \
struct \
{ \
void* self; \
Typeclass const* tc; \
}
#endif /* !IT_TYPECLASS_H */