From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.devel Subject: FFI support for disjoint types Date: Thu, 11 Nov 2010 17:24:09 +0100 Message-ID: <87bp5vx1zq.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1289492683 7225 80.91.229.12 (11 Nov 2010 16:24:43 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 11 Nov 2010 16:24:43 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Thu Nov 11 17:24:38 2010 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PGZx0-0002eY-Ch for guile-devel@m.gmane.org; Thu, 11 Nov 2010 17:24:38 +0100 Original-Received: from localhost ([127.0.0.1]:49983 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PGZwz-0007fU-Ll for guile-devel@m.gmane.org; Thu, 11 Nov 2010 11:24:37 -0500 Original-Received: from [140.186.70.92] (port=42299 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PGZwt-0007fP-F2 for guile-devel@gnu.org; Thu, 11 Nov 2010 11:24:32 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PGZws-000625-9l for guile-devel@gnu.org; Thu, 11 Nov 2010 11:24:31 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:41303) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PGZwr-00061i-TO for guile-devel@gnu.org; Thu, 11 Nov 2010 11:24:30 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PGZwo-0002YO-HB for guile-devel@gnu.org; Thu, 11 Nov 2010 17:24:26 +0100 Original-Received: from yoda.fdn.fr ([80.67.169.18]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Nov 2010 17:24:26 +0100 Original-Received: from ludo by yoda.fdn.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Nov 2010 17:24:26 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 84 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: yoda.fdn.fr X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 21 Brumaire an 219 de la =?iso-8859-1?Q?R=E9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Cancel-Lock: sha1:AygoNu4x2gEoSjTVVgVz/h+ipb0= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:11127 Archived-At: Hello! I’ve used the macro below in a couple of projects. It allows the creation of disjoint Scheme types for disjoint C pointer types, and takes care of preserving eq?-ness for equal C pointers. Example: --8<---------------cut here---------------start------------->8--- ;; Create a wrapped pointer type `class?'. (define-wrapped-pointer-type class? wrap-class unwrap-class print-class) (define lookup-class (let ((f (libchop-function '* "class_lookup" ('*)))) (lambda (name) (let ((ptr (f (string->pointer name)))) (if (null-pointer? ptr) #f ;; Wrap the object pointer so that it appears as an object ;; that matches `class?' at the Scheme level. (wrap-class ptr)))))) (define (class-name c) ;; C is a `class?' object, so unwrap it to get the underlying ;; pointer. (let ((ptr (make-pointer (+ (pointer-address (unwrap-class c)) %offset-of-name)))) (pointer->string (dereference-pointer ptr)))) --8<---------------cut here---------------end--------------->8--- Code: --8<---------------cut here---------------start------------->8--- (define-syntax define-wrapped-pointer-type (lambda (stx) (syntax-case stx () ((_ pred wrap unwrap print) ;; hygiene (with-syntax ((type-name (datum->syntax #'pred (gensym))) (%wrap (datum->syntax #'wrap (gensym)))) #'(begin (define-record-type type-name (%wrap pointer) pred (pointer unwrap)) (define wrap ;; Use a weak hash table to preserve pointer identity, i.e., ;; PTR1 == PTR2 <-> (eq? (wrap PTR1) (wrap PTR2)). (let ((ptr->obj (make-weak-value-hash-table))) (lambda (ptr) (or (hash-ref ptr->obj ptr) (let ((o (%wrap ptr))) (hash-set! ptr->obj ptr o) o))))) (set-record-type-printer! type-name print)))) ((_ type-name print) ;; lazyness (let* ((type-name* (syntax->datum #'type-name)) (pred-name (datum->syntax #'type-name (symbol-append type-name* '?))) (wrap-name (datum->syntax #'type-name (symbol-append 'wrap- type-name*))) (%wrap-name (datum->syntax #'type-name (symbol-append '%wrap- type-name*))) (unwrap-name (datum->syntax #'type-name (symbol-append 'unwrap- type-name*)))) (with-syntax ((pred pred-name) (wrap wrap-name) (%wrap %wrap-name) (unwrap unwrap-name)) #'(define-wrapped-pointer-type pred wrap unwrap print))))))) --8<---------------cut here---------------end--------------->8--- The second pattern in the macro is convenient but unhygienic, so I’m inclined to remove it. Thoughts? What about adding it to (system foreign), along with documentation? Thanks, Ludo’.