Commit | Line | Data |
---|---|---|
e2c2a699 AW |
1 | /* Copyright (C) 2010 Free Software Foundation, Inc. |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public License | |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301 USA | |
17 | */ | |
18 | ||
19 | #ifndef SCM_FOREIGN_H | |
20 | #define SCM_FOREIGN_H | |
21 | ||
22 | ||
23 | ||
24 | /* A subset of libffi's types. */ | |
25 | typedef enum | |
26 | { | |
27 | SCM_FOREIGN_TYPE_VOID, | |
28 | SCM_FOREIGN_TYPE_FLOAT, | |
29 | SCM_FOREIGN_TYPE_DOUBLE, | |
30 | SCM_FOREIGN_TYPE_UINT8, | |
31 | SCM_FOREIGN_TYPE_INT8, | |
32 | SCM_FOREIGN_TYPE_UINT16, | |
33 | SCM_FOREIGN_TYPE_INT16, | |
34 | SCM_FOREIGN_TYPE_UINT32, | |
35 | SCM_FOREIGN_TYPE_INT32, | |
36 | SCM_FOREIGN_TYPE_UINT64, | |
37 | SCM_FOREIGN_TYPE_INT64, | |
38 | SCM_FOREIGN_TYPE_STRUCT, | |
39 | SCM_FOREIGN_TYPE_POINTER | |
40 | } scm_t_foreign_type; | |
41 | ||
42 | ||
43 | typedef void (*scm_t_foreign_finalizer) (void *); | |
44 | ||
45 | #define SCM_FOREIGN_P(x) \ | |
46 | (!SCM_IMP (x) && SCM_TYP7(x) == scm_tc7_foreign) | |
47 | #define SCM_VALIDATE_FOREIGN(pos, x) \ | |
48 | SCM_MAKE_VALIDATE (pos, x, FOREIGN_P) | |
49 | #define SCM_FOREIGN_TYPE(x) \ | |
50 | ((scm_t_foreign_type)((SCM_CELL_WORD_0 (x) >> 8)&0xff)) | |
51 | #define SCM_FOREIGN_OBJECT(x, ctype) \ | |
52 | ((ctype*)SCM_CELL_OBJECT_1 (x)) | |
53 | #define SCM_FOREIGN_OBJECT_REF(x, ctype) \ | |
54 | (*SCM_FOREIGN_OBJECT (x, ctype)) | |
55 | #define SCM_FOREIGN_OBJECT_SET(x, ctype, val) \ | |
56 | (*SCM_FOREIGN_OBJECT (x, ctype) = (val)) | |
57 | ||
58 | #define SCM_FOREIGN_TYPED_P(x, type) \ | |
59 | (SCM_FOREIGN_P (x) && SCM_FOREIGN_TYPE (x) == SCM_FOREIGN_TYPE_##type) | |
60 | #define SCM_VALIDATE_FOREIGN_TYPED(pos, x, type) \ | |
61 | do { \ | |
62 | SCM_ASSERT_TYPE (SCM_FOREIGN_TYPED_P (x, type), x, pos, FUNC_NAME, \ | |
63 | "FOREIGN_"#type"_P"); \ | |
64 | } while (0) | |
65 | ||
66 | #define SCM_FOREIGN_SIMPLE_P(x) \ | |
67 | (SCM_FOREIGN_P (x) \ | |
68 | && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_VOID \ | |
69 | && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_STRUCT \ | |
70 | && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_POINTER) | |
71 | #define SCM_VALIDATE_FOREIGN_SIMPLE(pos, x) \ | |
72 | SCM_MAKE_VALIDATE (pos, x, FOREIGN_SIMPLE_P) | |
73 | ||
74 | SCM_API SCM scm_c_from_foreign (scm_t_foreign_type type, void *val, size_t size, | |
75 | scm_t_foreign_finalizer finalizer); | |
76 | SCM_API SCM scm_c_take_foreign (scm_t_foreign_type type, void *val, | |
77 | scm_t_foreign_finalizer finalizer); | |
78 | ||
79 | SCM_API SCM scm_foreign_ref (SCM foreign); | |
80 | SCM_API SCM scm_foreign_set_x (SCM foreign, SCM val); | |
81 | ||
82 | SCM_INTERNAL void scm_i_foreign_print (SCM foreign, SCM port, | |
83 | scm_print_state *pstate); | |
84 | SCM_INTERNAL void scm_init_foreign (void); | |
85 | ||
86 | ||
87 | #endif /* SCM_FOREIGN_H */ |