1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: UFFI -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: aggregates.cl
6 ;;;; Purpose: UFFI source to handle aggregate types
7 ;;;; Programmers: Kevin M. Rosenberg and John DeSoi
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: aggregates.cl,v 1.4 2002/04/23 01:06:56 desoi Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; UFFI users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
25 (defmacro def-enum (enum-name args &key (separator-string "#"))
26 "Creates a constants for a C type enum list, symbols are created
27 in the created in the current package. The symbol is the concatenation
28 of the enum-name name, separator-string, and field-name"
32 (declare (fixnum counter))
34 (let ((name (if (listp arg) (car arg) arg))
35 (value (if (listp arg)
37 (setq counter (cadr arg))
42 (setq name (intern (concatenate 'string
43 (symbol-name enum-name)
46 (push `(uffi:def-constant ,name ,value) constants)))
47 (setf cmds (append '(progn)
48 #+allegro `((ff:def-foreign-type ,enum-name :int))
49 #+lispworks `((fli:define-c-typedef ,enum-name :int))
50 #+cmu `((alien:def-alien-type ,enum-name alien:signed))
51 #+mcl `((def-mcl-type ,enum-name :integer))
52 (nreverse constants)))
57 (defmacro def-array-pointer (name-array type)
58 `(def-mcl-type ,name-array '(:array ,type)))
61 ; this is how rref expands array slot access (minus adding the struct offset)
62 (defmacro deref-array (obj type i)
63 "Returns a field from a row"
64 `(,(accessor-symbol type :get) ,obj (* (the fixnum ,i) ,(foreign-object-size type))))
66 (defmacro deref-array-set (obj type i value)
67 `(,(accessor-symbol type :set) ,obj ,value (* (the fixnum ,i) ,(foreign-object-size type))))
69 (defsetf deref-array deref-array-set)
72 (defun process-struct-fields (name fields variant)
74 (dolist (field fields)
75 (let* ((field-name (car field))
77 (def (append (list field-name)
78 (if (eq type :pointer-self)
79 #+cmu `((* (alien:struct ,name)))
81 `(,(convert-from-uffi-type type :struct))))))
83 (push (list def) processed)
84 (push def processed))))
85 (nreverse processed)))
88 (defmacro def-struct (name &rest fields)
89 `(ccl:defrecord ,name ,@(process-struct-fields name fields nil)))
92 (defmacro def-union (name &rest fields)
93 `(ccl:defrecord ,name (:variant ,@(process-struct-fields name fields t))))
96 ; Assuming everything is pointer based - no support for Mac handles
97 (defmacro get-slot-value (obj type slot) ;use setf to set values
98 `(ccl:pref ,obj ,(read-from-string (format nil "~a.~a" type slot))))
101 (defmacro get-slot-pointer (obj type slot)
102 `(ccl:%int-to-ptr (+ (ccl:%ptr-to-int ,obj) (the fixnum (ccl:field-info ,type ,slot)))))
106 #| a few simple tests
116 (defvar s (allocate-foreign-object :struct))
117 (setf (get-slot-value s :struct :s1) 3)
118 (get-slot-value s :struct :s1)
119 (setf (get-slot-value s :struct :u1.s1) 5)
120 (get-slot-value s :struct :u1.s1)