libcdsb/src/__internal/vnode.h

70 lines
2.8 KiB
C
Raw Normal View History

2022-06-02 14:20:55 +03:00
/* This software is licensed by the Apache License 2.0, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "include.h"
2022-08-19 18:46:51 +03:00
#include "assert.h"
2022-06-02 14:20:55 +03:00
#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 {
2022-06-09 16:29:10 +03:00
void* ptr; bool b;
2022-06-02 14:20:55 +03:00
str_t s; arr_t a; list_t l;
2022-08-16 18:39:55 +03:00
map_t m; set_t vs; dict_t vd;
2022-06-02 14:20:55 +03:00
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;
2022-08-22 14:18:38 +03:00
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__;
2022-06-02 14:20:55 +03:00
2022-08-22 14:18:38 +03:00
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);
2022-06-02 14:20:55 +03:00
2022-08-22 14:18:38 +03:00
ainline(void libcdsb_builtin_vnode_attach(vnode_t* node, const void* value, vtype type)) {
2022-08-19 18:46:51 +03:00
if (type < VTYPE_STRING) {
2022-08-22 14:18:38 +03:00
*node = libcdsb_builtin_vnode_create(value, type);
2022-08-19 18:46:51 +03:00
} 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));
}
}
2022-08-22 14:18:38 +03:00
ainline(void libcdsb_builtin_vnode_tattach(vnode_t* node, vtype target_type, const void* value, vtype type)) {
2022-08-19 18:46:51 +03:00
if (type < VTYPE_STRING) {
2022-08-22 14:18:38 +03:00
*node = libcdsb_builtin_vnode_create_target(target_type, value, type);
2022-08-19 18:46:51 +03:00
} 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));
}
}
}
2022-08-22 14:18:38 +03:00
#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
2022-06-02 14:20:55 +03:00
2022-08-14 18:18:34 +03:00
#define vnode_hash(vnode, type) vtype_hash(vnode_peek(vnode, type), type)
2022-06-02 14:20:55 +03:00
#define vnode_compare(s0, t0, s1, t1) vtype_compare(vnode_peek(s0, t0), t0, vnode_peek(s1, t1), t1)
2022-08-14 18:18:34 +03:00
#define vnode_compare_eq(s0, s1, t) vtype_compare_eq(vnode_peek(s0, t), vnode_peek(s1, t), t)
2022-08-22 14:18:38 +03:00
#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)
2022-06-02 14:20:55 +03:00
2022-06-08 10:54:50 +03:00
#define vnode_stringify(n, t) vtype_stringify(vnode_peek(n, t), t)
2022-06-02 14:20:55 +03:00
#endif /* LIBCDSB_SRC_INTERNAL_VNODE_H */