70 lines
2.8 KiB
C
70 lines
2.8 KiB
C
/* This software is licensed by the Apache License 2.0, see LICENSE file */
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
#include "include.h"
|
|
#include "assert.h"
|
|
|
|
#ifndef LIBCDSB_SRC_INTERNAL_VNODE_H
|
|
#define LIBCDSB_SRC_INTERNAL_VNODE_H
|
|
|
|
#define is_permissible(T) (sizeof(void*) >= sizeof(T) && _Alignof(void*) >= _Alignof(T))
|
|
|
|
typedef union {
|
|
void* ptr; bool b;
|
|
str_t s; arr_t a; list_t l;
|
|
map_t m; set_t vs; dict_t vd;
|
|
u8_t u8; u16_t u16; u32_t u32; u64_t u64;
|
|
fl_t f; dbl_t d; ldbl_t ld;
|
|
} var_t;
|
|
|
|
typedef void* vnode_t;
|
|
|
|
extern vnode_t libcdsb_builtin_vnode_create (const void* value, vtype type) wur__;
|
|
extern vnode_t libcdsb_builtin_vnode_create_target(vtype target_type, const void* value, vtype type) wur__;
|
|
|
|
extern void libcdsb_builtin_vnode_free(vnode_t* x, vtype type) Nonnull__(1);
|
|
extern void* libcdsb_builtin_vnode_peek(const vnode_t* x, vtype type) pure__ wur__ Nonnull__(1);
|
|
|
|
ainline(void libcdsb_builtin_vnode_attach(vnode_t* node, const void* value, vtype type)) {
|
|
if (type < VTYPE_STRING) {
|
|
*node = libcdsb_builtin_vnode_create(value, type);
|
|
} else if (sizeof(str_t) == sizeof(void*) && type == VTYPE_STRING) {
|
|
*node = *(char**)value;
|
|
} else {
|
|
*node = malloc(vtype_size(type));
|
|
memcpy(*node, value, vtype_size(type));
|
|
}
|
|
}
|
|
|
|
ainline(void libcdsb_builtin_vnode_tattach(vnode_t* node, vtype target_type, const void* value, vtype type)) {
|
|
if (type < VTYPE_STRING) {
|
|
*node = libcdsb_builtin_vnode_create_target(target_type, value, type);
|
|
} else {
|
|
type_assert(target_type, type);
|
|
|
|
if (sizeof(str_t) == sizeof(void*) && type == VTYPE_STRING) {
|
|
*node = *(char**)value;
|
|
} else {
|
|
*node = malloc(vtype_size(type));
|
|
memcpy(*node, value, vtype_size(type));
|
|
}
|
|
}
|
|
}
|
|
|
|
#define vnode_create libcdsb_builtin_vnode_create
|
|
#define vnode_tcreate libcdsb_builtin_vnode_create_target
|
|
#define vnode_attach libcdsb_builtin_vnode_attach
|
|
#define vnode_tattach libcdsb_builtin_vnode_tattach
|
|
#define vnode_peek libcdsb_builtin_vnode_peek
|
|
#define vnode_free libcdsb_builtin_vnode_free
|
|
|
|
#define vnode_hash(vnode, type) vtype_hash(vnode_peek(vnode, type), type)
|
|
#define vnode_compare(s0, t0, s1, t1) vtype_compare(vnode_peek(s0, t0), t0, vnode_peek(s1, t1), t1)
|
|
#define vnode_compare_eq(s0, s1, t) vtype_compare_eq(vnode_peek(s0, t), vnode_peek(s1, t), t)
|
|
#define vnode_duplicate(vnode, type) vnode_create(vnode_peek(vnode, type), type)
|
|
#define vnode_tduplicate(target_type, vnode, type) vnode_tcreate(target_type, vnode_peek(vnode, type), type)
|
|
|
|
#define vnode_stringify(n, t) vtype_stringify(vnode_peek(n, t), t)
|
|
|
|
#endif /* LIBCDSB_SRC_INTERNAL_VNODE_H */
|