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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright 2010 University of Helsinki.
*
* This file is part of libgu.
*
* Libgu is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Libgu is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libgu. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file
*
* Lightweight tagged data.
*/
#ifndef GU_VARIANT_H_
#define GU_VARIANT_H_
#include <gu/defs.h>
#include <gu/mem.h>
#include <gu/type.h>
/** @name Variants
* @{
*/
typedef struct GuVariant GuVariant;
void* gu_alloc_variant(uint8_t tag,
size_t size, size_t align,
GuVariant* variant_out, GuPool* pool);
GuVariant gu_make_variant(uint8_t tag,
size_t size, size_t align,
const void* init, GuPool* pool);
#define gu_new_variant(tag, type, variant_out, pool) \
((type*)gu_alloc_variant(tag, sizeof(type), \
gu_alignof(type), variant_out, pool))
/**<
* @hideinitializer */
#define gu_new_variant_i(POOL, TAG, T, ...) \
gu_make_variant(TAG, sizeof(T), gu_alignof(T), \
&(T){ __VA_ARGS__ }, POOL)
#define gu_new_flex_variant(tag, type, flex_mem, n_elems, variant_out, pool) \
((type*)gu_alloc_variant(tag, \
GU_FLEX_SIZE(type, flex_mem, n_elems), \
gu_flex_alignof(type), \
variant_out, pool))
/**<
* @hideinitializer */
enum {
GU_VARIANT_NULL = -1
};
int gu_variant_tag(GuVariant variant);
void* gu_variant_data(GuVariant variant);
typedef struct GuVariantInfo GuVariantInfo;
struct GuVariantInfo {
int tag;
void* data;
};
GuVariantInfo gu_variant_open(GuVariant variant);
/** @privatesection */
struct GuVariant {
uintptr_t p;
/**< @private */
};
/** @} */
static inline void*
gu_variant_to_ptr(GuVariant variant)
{
return (void*)variant.p;
}
static inline GuVariant
gu_variant_from_ptr(const void* p)
{
GuVariant v = { (uintptr_t)p };
return v;
}
extern const GuVariant gu_null_variant;
static inline bool
gu_variant_is_null(GuVariant v) {
return ((void*)v.p == NULL);
}
// variant
typedef const struct GuConstructor GuConstructor;
struct GuConstructor {
int c_tag;
const char* c_name;
const GuType* type;
};
#define GU_CONSTRUCTOR_V(ctag, c_type) { \
.c_tag = ctag, \
.c_name = #ctag, \
.type = c_type \
}
#define GU_CONSTRUCTOR(ctag, t_) \
GU_CONSTRUCTOR_V(ctag, gu_type(t_))
#define GU_CONSTRUCTOR_P(ctag, t_) \
GU_CONSTRUCTOR_V(ctag, gu_ptr_type(t_))
#define GU_CONSTRUCTOR_S(ctag, t_, ...) \
GU_CONSTRUCTOR_V(ctag, GU_TYPE_LIT(struct, t_, __VA_ARGS__))
#define GU_CONSTRUCTOR_S1(ctag, t_, mem1_, type1_) \
GU_CONSTRUCTOR_S(ctag, t_, \
GU_MEMBER(t_, mem1_, type1_))
#define GU_CONSTRUCTOR_S2(ctag, t_, mem1_, type1_, mem2_, type2_) \
GU_CONSTRUCTOR_S(ctag, t_, \
GU_MEMBER(t_, mem1_, type1_), \
GU_MEMBER(t_, mem2_, type2_))
typedef struct {
int len;
GuConstructor* elems;
} GuConstructors;
typedef const struct GuVariantType GuVariantType, GuType_GuVariant;
struct GuVariantType {
GuType_repr repr_base;
GuConstructors ctors;
};
#define GU_TYPE_INIT_GuVariant(k_, t_, ...) { \
.repr_base = GU_TYPE_INIT_repr(k_, GuVariant, _), \
.ctors = { \
.len = GU_ARRAY_LEN(GuConstructor,GU_ID({__VA_ARGS__})), \
.elems = ((GuConstructor[]){__VA_ARGS__}) \
} \
}
extern GU_DECLARE_KIND(GuVariant);
#endif // GU_VARIANT_H_
|