libcdsb/src/__internal/vnode.h

62 lines
2.3 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 void* vnode_t;
extern vnode_t libcdsb_builtin_vnode_create ( var_t) wur__;
extern vnode_t libcdsb_builtin_vnode_create_ex(vtype xt, var_t) wur__;
extern var_t libcdsb_builtin_vnode_peek(const vnode_t* x, vtype type) pure__ wur__ Nonnull__(1);
extern void libcdsb_builtin_vnode_free( vnode_t* x, vtype type) Nonnull__(1);
ainline(void libcdsb_builtin_vnode_attach(vnode_t* node, var_t value)) {
2023-03-24 21:25:42 +03:00
if (value.type < VTYPE_STRING) {
*node = libcdsb_builtin_vnode_create(value);
} else if (sizeof(str_t) == sizeof(void*) && value.type == VTYPE_STRING) {
*node = *(char**)value.pointer;
2022-08-19 18:46:51 +03:00
} else {
2023-03-24 21:25:42 +03:00
*node = malloc(vtype_size(value.type));
memcpy(*node, value.pointer, vtype_size(value.type));
2022-08-19 18:46:51 +03:00
}
}
ainline(void libcdsb_builtin_vnode_attach_ex(vnode_t* node, vtype target, var_t value)) {
2023-03-24 21:25:42 +03:00
if (value.type < VTYPE_STRING) {
*node = libcdsb_builtin_vnode_create_ex(target, value);
2022-08-19 18:46:51 +03:00
} else {
2023-03-24 21:25:42 +03:00
type_assert(target, value.type);
2022-08-19 18:46:51 +03:00
2023-03-24 21:25:42 +03:00
if (sizeof(str_t) == sizeof(void*) && value.type == VTYPE_STRING) {
*node = *(char**)value.pointer;
2022-08-19 18:46:51 +03:00
} else {
2023-03-24 21:25:42 +03:00
*node = malloc(vtype_size(value.type));
memcpy(*node, value.pointer, vtype_size(value.type));
2022-08-19 18:46:51 +03:00
}
}
}
2023-03-24 21:25:42 +03:00
#define vnode_create libcdsb_builtin_vnode_create
#define vnode_attach libcdsb_builtin_vnode_attach
#define vnode_create_ex libcdsb_builtin_vnode_create_ex
#define vnode_attach_ex libcdsb_builtin_vnode_attach_ex
#define vnode_peek libcdsb_builtin_vnode_peek
#define vnode_free libcdsb_builtin_vnode_free
2022-06-02 14:20:55 +03:00
2023-03-24 21:25:42 +03:00
#define vnode_hash(n, t) variable_hash(vnode_peek(n, t))
#define vnode_compare(s0, t0, s1, t1) variable_compare(vnode_peek(s0, t0), vnode_peek(s1, t1))
#define vnode_stringify(n, t) variable_stringify(vnode_peek(n, t))
2022-06-02 14:20:55 +03:00
2023-03-24 21:25:42 +03:00
#define vnode_duplicate(n, t) vnode_create(vnode_peek(n, t))
#define vnode_duplicate_ex(xt, n, t) vnode_create_ex(xt, vnode_peek(n, t))
2022-06-08 10:54:50 +03:00
2022-06-02 14:20:55 +03:00
#endif /* LIBCDSB_SRC_INTERNAL_VNODE_H */