summaryrefslogtreecommitdiff
path: root/libguile/foreign.h
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2010-01-04 12:11:33 +0100
committerAndy Wingo <wingo@pobox.com>2010-01-04 12:39:21 +0100
commite2c2a6994d05124760ea7f18caf5d28fb47e453c (patch)
treebe4c3ae11905d89a8f9519bcc6fbebab4a003beb /libguile/foreign.h
parent208fae8a0ebaa56b69105c89d69e337aef6a3e62 (diff)
downloadguile-e2c2a6994d05124760ea7f18caf5d28fb47e453c.tar.gz
add foreign value wrapper
* libguile/foreign.h: * libguile/foreign.c: New files, implementing simple wrappers around foreign values, such as those that one might link in dynamically from a library. * libguile/tags.h (scm_tc7_foreign): Take a tc7 for foreign values. * libguile.h: * libguile/init.c: Add foreign.h to headers and init. * libguile/print.c (iprin1): Add printer for foreign values. * libguile/gc.c (scm_i_tag_name): Case for foreign values. * libguile/goops.c (scm_class_of, create_standard_classes): Add a class for foreign values. * libguile/evalext.c (scm_self_evaluating_p): Add case for foreign values. * libguile/Makefile.am: Add foreign.[ch] to the build.
Diffstat (limited to 'libguile/foreign.h')
-rw-r--r--libguile/foreign.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/libguile/foreign.h b/libguile/foreign.h
new file mode 100644
index 000000000..954c1c5b3
--- /dev/null
+++ b/libguile/foreign.h
@@ -0,0 +1,87 @@
+/* Copyright (C) 2010 Free Software Foundation, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef SCM_FOREIGN_H
+#define SCM_FOREIGN_H
+
+
+
+/* A subset of libffi's types. */
+typedef enum
+ {
+ SCM_FOREIGN_TYPE_VOID,
+ SCM_FOREIGN_TYPE_FLOAT,
+ SCM_FOREIGN_TYPE_DOUBLE,
+ SCM_FOREIGN_TYPE_UINT8,
+ SCM_FOREIGN_TYPE_INT8,
+ SCM_FOREIGN_TYPE_UINT16,
+ SCM_FOREIGN_TYPE_INT16,
+ SCM_FOREIGN_TYPE_UINT32,
+ SCM_FOREIGN_TYPE_INT32,
+ SCM_FOREIGN_TYPE_UINT64,
+ SCM_FOREIGN_TYPE_INT64,
+ SCM_FOREIGN_TYPE_STRUCT,
+ SCM_FOREIGN_TYPE_POINTER
+ } scm_t_foreign_type;
+
+
+typedef void (*scm_t_foreign_finalizer) (void *);
+
+#define SCM_FOREIGN_P(x) \
+ (!SCM_IMP (x) && SCM_TYP7(x) == scm_tc7_foreign)
+#define SCM_VALIDATE_FOREIGN(pos, x) \
+ SCM_MAKE_VALIDATE (pos, x, FOREIGN_P)
+#define SCM_FOREIGN_TYPE(x) \
+ ((scm_t_foreign_type)((SCM_CELL_WORD_0 (x) >> 8)&0xff))
+#define SCM_FOREIGN_OBJECT(x, ctype) \
+ ((ctype*)SCM_CELL_OBJECT_1 (x))
+#define SCM_FOREIGN_OBJECT_REF(x, ctype) \
+ (*SCM_FOREIGN_OBJECT (x, ctype))
+#define SCM_FOREIGN_OBJECT_SET(x, ctype, val) \
+ (*SCM_FOREIGN_OBJECT (x, ctype) = (val))
+
+#define SCM_FOREIGN_TYPED_P(x, type) \
+ (SCM_FOREIGN_P (x) && SCM_FOREIGN_TYPE (x) == SCM_FOREIGN_TYPE_##type)
+#define SCM_VALIDATE_FOREIGN_TYPED(pos, x, type) \
+ do { \
+ SCM_ASSERT_TYPE (SCM_FOREIGN_TYPED_P (x, type), x, pos, FUNC_NAME, \
+ "FOREIGN_"#type"_P"); \
+ } while (0)
+
+#define SCM_FOREIGN_SIMPLE_P(x) \
+ (SCM_FOREIGN_P (x) \
+ && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_VOID \
+ && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_STRUCT \
+ && SCM_FOREIGN_TYPE (x) != SCM_FOREIGN_TYPE_POINTER)
+#define SCM_VALIDATE_FOREIGN_SIMPLE(pos, x) \
+ SCM_MAKE_VALIDATE (pos, x, FOREIGN_SIMPLE_P)
+
+SCM_API SCM scm_c_from_foreign (scm_t_foreign_type type, void *val, size_t size,
+ scm_t_foreign_finalizer finalizer);
+SCM_API SCM scm_c_take_foreign (scm_t_foreign_type type, void *val,
+ scm_t_foreign_finalizer finalizer);
+
+SCM_API SCM scm_foreign_ref (SCM foreign);
+SCM_API SCM scm_foreign_set_x (SCM foreign, SCM val);
+
+SCM_INTERNAL void scm_i_foreign_print (SCM foreign, SCM port,
+ scm_print_state *pstate);
+SCM_INTERNAL void scm_init_foreign (void);
+
+
+#endif /* SCM_FOREIGN_H */