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