LispBM
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
29 extern "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 
57 bool lbm_custom_type_create(lbm_uint value, custom_type_destructor fptr, const char *desc, lbm_value *result);
58 
64 bool lbm_custom_type_destroy(lbm_uint *lbm_mem_ptr);
65 
66 static inline bool lbm_is_custom(lbm_value value) {
67  return (lbm_type_of(value) == LBM_TYPE_CUSTOM && !lbm_is_symbol_nil(lbm_car(value)));
68 }
69 
70 // Must check is_custom before calling get_custom_descriptor
71 static inline const char *lbm_get_custom_descriptor(lbm_value value) {
72  lbm_uint *m = (lbm_uint*)(lbm_ref_cell(value)->car);
73  return (const char*)m[CUSTOM_TYPE_DESCRIPTOR];
74 }
75 
76 // Must check is_custom before calling get_custom_descriptor
77 static inline lbm_uint lbm_get_custom_value(lbm_value value) {
78  lbm_uint *m = (lbm_uint*)lbm_dec_custom(value);
79  return m[CUSTOM_TYPE_VALUE];
80 }
81 
82 #ifdef __cplusplus
83 }
84 #endif
85 #endif
lbm_car
lbm_value lbm_car(lbm_value cons)
Definition: heap.c:902
lbm_custom_type_destroy
bool lbm_custom_type_destroy(lbm_uint *lbm_mem_ptr)
Definition: lbm_custom_type.c:47
lbm_defines.h
lbm_ref_cell
static lbm_cons_t * lbm_ref_cell(lbm_value addr)
Definition: heap.h:970
lbm_uint
uint32_t lbm_uint
Definition: lbm_types.h:48
lbm_get_custom_descriptor
static const char * lbm_get_custom_descriptor(lbm_value value)
Definition: lbm_custom_type.h:71
LBM_TYPE_CUSTOM
#define LBM_TYPE_CUSTOM
Definition: lbm_defines.h:55
lbm_is_symbol_nil
static bool lbm_is_symbol_nil(lbm_value exp)
Definition: heap.h:834
lbm_is_custom
static bool lbm_is_custom(lbm_value value)
Definition: lbm_custom_type.h:66
lbm_cons_t::car
lbm_value car
Definition: heap.h:204
lbm_custom_type_create
bool lbm_custom_type_create(lbm_uint value, custom_type_destructor fptr, const char *desc, lbm_value *result)
Definition: lbm_custom_type.c:23
CUSTOM_TYPE_VALUE
#define CUSTOM_TYPE_VALUE
Definition: lbm_custom_type.h:33
CUSTOM_TYPE_DESCRIPTOR
#define CUSTOM_TYPE_DESCRIPTOR
Definition: lbm_custom_type.h:34
lbm_types.h
lbm_get_custom_value
static lbm_uint lbm_get_custom_value(lbm_value value)
Definition: lbm_custom_type.h:77
custom_type_destructor
bool(* custom_type_destructor)(lbm_uint)
Definition: lbm_custom_type.h:45
lbm_type_of
static lbm_type lbm_type_of(lbm_value x)
Definition: heap.h:663
lbm_value
uint32_t lbm_value
Definition: lbm_types.h:44
lbm_dec_custom
lbm_uint lbm_dec_custom(lbm_value val)
Definition: heap.c:250
heap.h