1#pragma once
 2
 3/*
 4 * Built-in interface ids
 5 */
 6#define PwInterfaceId_Basic         0
 7#define PwInterfaceId_RandomAccess  1
 8#define PwInterfaceId_Reader        2
 9#define PwInterfaceId_Writer        3
10#define PwInterfaceId_LineReader    4
11#define PwInterfaceId_Append        5
12#define PwInterfaceId_Fd            6
13
14#define PW_NUM_BUILTIN_INTERFACES   7
15
16/*
17 * Method declaration macros
18 */
19
20#define PW_METHOD_BEGIN(interface_name, method_name)  \
21    struct _PwInterface_##interface_name;  \
22    struct _PwMethod_##interface_name##_##method_name;  \
23    typedef struct _PwMethod_##interface_name##_##method_name PwMethod_##interface_name##_##method_name;  \
24    typedef
25
26#define PW_METHOD_END(interface_name, method_name)  \
27    ;  \
28    struct _PwMethod_##interface_name##_##method_name {  \
29        [[ gnu::warn_unused_result ]] PwFunc_##interface_name##_##method_name func;  \
30        struct _PwMethod_##interface_name##_##method_name* super;  \
31        struct _PwInterface_##interface_name* self;  \
32        unsigned struct_offset;  \
33    };
34/* Note:
35 * struct_offset is a shorthand copy of self->type->struct_offsets[0]
36 */
37
38/*
39 * Interface declaration macros
40 */
41#define PW_INTERFACE_BEGIN(interface_name)  \
42    struct _PwInterface_##interface_name {  \
43        uint16_t id;           \
44        uint16_t num_methods;  \
45        char* name;            \
46        char** method_names;   \
47        struct _PwType* type;
48
49#define PW_INTERFACE_END(interface_name)  \
50    };  \
51    typedef struct _PwInterface_##interface_name PwInterface_##interface_name;
52
53
54/*
55 * Generic interface.
56 *
57 * This structure is used in PwType and by basic interface-agnostic functions.
58 */
59
60struct _PwType;  // forward declaration
61
62PW_METHOD_BEGIN(Generic, Generic)
63    bool (*PwFunc_Generic_Generic)(PwMethod_Generic_Generic* mthis, ...)
64PW_METHOD_END(Generic, Generic)
65
66PW_INTERFACE_BEGIN(Generic)
67    PwMethod_Generic_Generic methods[];  // for real interfaces this is multiple fields, not an array
68PW_INTERFACE_END(Generic)
69
70// shortcuts
71typedef PwMethod_Generic_Generic PwMethod_Generic;
72typedef PwFunc_Generic_Generic   PwFunc_Generic;
73
74
75#ifdef __cplusplus
76}
77#endif