LispBM
Loading...
Searching...
No Matches
lbm_custom_type.h
Go to the documentation of this file.
1/*
2 Copyright 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
19#ifndef LBM_CUSTOM_TYPE_H_
20#define LBM_CUSTOM_TYPE_H_
21
22#include <stdbool.h>
23#include <stddef.h>
24#include <lbm_types.h>
25#include <lbm_defines.h>
26#include <heap.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32// Custom type lbm_memory footprint
33#define CUSTOM_TYPE_VALUE 0
34#define CUSTOM_TYPE_DESCRIPTOR 1
35#define CUSTOM_TYPE_DESTRUCTOR 2
36#define CUSTOM_TYPE_LBM_MEM_SIZE 3
37
38// encoding
39//
40// ( lbm-mem-ptr . LBM_TYPE_CUSTOM_SYM )
41// |
42// pointer to lbm_memory
43//
44
46
57bool lbm_custom_type_create(lbm_uint value, custom_type_destructor fptr, const char *desc, lbm_value *result);
58
64bool lbm_custom_type_destroy(lbm_uint *lbm_mem_ptr);
65
66static inline bool lbm_is_custom(lbm_value value) {
67 lbm_value v = lbm_dec_custom(value);
68 // SYM_NIL has bit pattern 0
69 // dec_custom returns zero when type is not custom.
70 return (v != 0); // lbm_type_of(value) == LBM_TYPE_CUSTOM && !lbm_is_symbol_nil(lbm_car(value)));
71}
72
73// Must check is_custom before calling get_custom_descriptor
74static inline const char *lbm_get_custom_descriptor(lbm_value value) {
75 lbm_uint *m = (lbm_uint*)(lbm_ref_cell(value)->car);
76 return (const char*)m[CUSTOM_TYPE_DESCRIPTOR];
77}
78
79// Must check is_custom before calling get_custom_descriptor
81 lbm_uint *m = (lbm_uint*)lbm_dec_custom(value);
82 if (m) {
83 return m[CUSTOM_TYPE_VALUE];
84 }
85 return 0;
86}
87
88#ifdef __cplusplus
89}
90#endif
91#endif
lbm_uint lbm_dec_custom(lbm_value val)
Definition heap.c:290
static lbm_cons_t * lbm_ref_cell(lbm_value addr)
Definition heap.h:994
bool lbm_custom_type_destroy(lbm_uint *lbm_mem_ptr)
Definition lbm_custom_type.c:47
static bool lbm_is_custom(lbm_value value)
Definition lbm_custom_type.h:66
bool lbm_custom_type_create(lbm_uint value, custom_type_destructor fptr, const char *desc, lbm_value *result)
Definition lbm_custom_type.c:23
#define CUSTOM_TYPE_DESCRIPTOR
Definition lbm_custom_type.h:34
#define CUSTOM_TYPE_VALUE
Definition lbm_custom_type.h:33
static lbm_uint lbm_get_custom_value(lbm_value value)
Definition lbm_custom_type.h:80
bool(* custom_type_destructor)(lbm_uint)
Definition lbm_custom_type.h:45
static const char * lbm_get_custom_descriptor(lbm_value value)
Definition lbm_custom_type.h:74
uint32_t lbm_uint
Definition lbm_types.h:48
uint32_t lbm_value
Definition lbm_types.h:44
lbm_value car
Definition heap.h:204