summaryrefslogtreecommitdiff
path: root/src/runtime/c/gu/variant.h
blob: df2ea214f72f2031cf5ac674fd10772cea06ad3d (plain)
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
/* 
 * 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>

/** @name Variants
 * @{
 */

typedef uintptr_t GuVariant;


GU_API_DECL
void* gu_alloc_variant(uint8_t tag, 
		       size_t size, size_t align, 
		       GuVariant* variant_out, GuPool* pool);

GU_API_DECL
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
};

GU_API_DECL
int gu_variant_tag(GuVariant variant);

GU_API_DECL
void* gu_variant_data(GuVariant variant);


typedef struct GuVariantInfo GuVariantInfo;

struct GuVariantInfo {
	int tag;
	void* data;
};

GU_API_DECL GuVariantInfo gu_variant_open(GuVariant variant);
GU_API_DECL GuVariant gu_variant_close(GuVariantInfo info);

/** @} */

static inline void*
gu_variant_to_ptr(GuVariant variant)
{
	return (void*) variant;
}

static inline GuVariant
gu_variant_from_ptr(const void* p)
{
	return (uintptr_t) p;
}

GU_API_DECL extern const GuVariant gu_null_variant;

static inline bool
gu_variant_is_null(GuVariant v) {
	return ((void*)v == NULL);
}

#endif // GU_VARIANT_H_