LispBM
lbm_custom_type.h
Go to the documentation of this file.
1 /*
2  Copyright 2022 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 
45 typedef bool (*custom_type_destructor)(lbm_uint);
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 const char *lbm_get_custom_descriptor(lbm_value value) {
67  if (lbm_type_of(value) == LBM_TYPE_CUSTOM) {
68  lbm_uint *m = (lbm_uint*)lbm_dec_custom(value);
69  return (const char*)m[CUSTOM_TYPE_DESCRIPTOR];
70  }
71  return NULL;
72 }
73 
74 static inline lbm_uint lbm_get_custom_value(lbm_value value) {
75  lbm_uint *m = (lbm_uint*)lbm_dec_custom(value);
76  return m[CUSTOM_TYPE_VALUE];
77 }
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 #endif
lbm_uint lbm_dec_custom(lbm_value val)
static lbm_type lbm_type_of(lbm_value x)
Definition: heap.h:657
bool lbm_custom_type_destroy(lbm_uint *lbm_mem_ptr)
bool lbm_custom_type_create(lbm_uint value, custom_type_destructor fptr, const char *desc, lbm_value *result)
uint32_t lbm_value
Definition: lbm_types.h:43