r10100: move function so that deref-array macro is defined
[uffi.git] / src / aggregates.lisp
index a233d1d4462d44bc30c9366e67ea10258c4dfb9b..5fdc9081d430acc5e83ec570f01d3d99764d00f2 100644 (file)
@@ -2,12 +2,12 @@
 ;;;; *************************************************************************
 ;;;; FILE IDENTIFICATION
 ;;;;
-;;;; Name:          aggregates.cl
+;;;; Name:          aggregates.lisp
 ;;;; Purpose:       UFFI source to handle aggregate types
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: aggregates.lisp,v 1.3 2002/10/14 04:15:02 kevin Exp $
+;;;; $Id$
 ;;;;
 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;;
@@ -16,8 +16,7 @@
 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
 ;;;; *************************************************************************
 
-(declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
-(in-package :uffi)
+(in-package #:uffi)
 
 (defmacro def-enum (enum-name args &key (separator-string "#"))
   "Creates a constants for a C type enum list, symbols are created
@@ -44,7 +43,7 @@ of the enum-name name, separator-string, and field-name"
     (setf cmds (append '(progn)
                       #+allegro `((ff:def-foreign-type ,enum-name :int))
                       #+lispworks `((fli:define-c-typedef ,enum-name :int))
-                      #+cmu `((alien:def-alien-type ,enum-name alien:signed))
+                      #+(or cmu scl) `((alien:def-alien-type ,enum-name alien:signed))
                       #+sbcl `((sb-alien:define-alien-type ,enum-name sb-alien:signed))
                        #+(and mcl (not openmcl)) `((def-mcl-type ,enum-name :integer))
                        #+openmcl `((ccl::def-foreign-type ,enum-name :int))
@@ -59,7 +58,7 @@ of the enum-name name, separator-string, and field-name"
   #+lispworks
   `(fli:define-c-typedef ,name-array
     (:c-array ,(convert-from-uffi-type type :array)))
-  #+cmu
+  #+(or cmu scl)
   `(alien:def-alien-type ,name-array 
     (* ,(convert-from-uffi-type type :array)))
   #+sbcl
@@ -78,10 +77,11 @@ of the enum-name name, separator-string, and field-name"
             (type (cadr field))
             (def (append (list field-name)
                          (if (eq type :pointer-self)
-                             #+cmu `((* (alien:struct ,name)))
+                             #+(or cmu scl) `((* (alien:struct ,name)))
                              #+sbcl `((* (sb-alien:struct ,name)))
                              #+mcl `((:* (:struct ,name)))
-                             #-(or cmu sbcl mcl) `((* ,name))
+                             #+lispworks `((:pointer ,name))
+                             #-(or cmu sbcl scl mcl lispworks) `((* ,name))
                              `(,(convert-from-uffi-type type :struct))))))
        (if variant
            (push (list def) processed)
@@ -90,7 +90,7 @@ of the enum-name name, separator-string, and field-name"
        
            
 (defmacro def-struct (name &rest fields)
-  #+cmu
+  #+(or cmu scl)
   `(alien:def-alien-type ,name (alien:struct ,name ,@(process-struct-fields name fields)))
   #+sbcl
   `(sb-alien:define-alien-type ,name (sb-alien:struct ,name ,@(process-struct-fields name fields)))
@@ -108,17 +108,18 @@ of the enum-name name, separator-string, and field-name"
 
 
 (defmacro get-slot-value (obj type slot)
-  #+(or lispworks cmu sbcl) (declare (ignore type))
+  #+(or lispworks cmu sbcl scl) (declare (ignore type))
   #+allegro
   `(ff:fslot-value-typed ,type :c ,obj ,slot)
   #+lispworks
   `(fli:foreign-slot-value ,obj ,slot)
-  #+cmu
+  #+(or cmu scl)
   `(alien:slot ,obj ,slot)
   #+sbcl
   `(sb-alien:slot ,obj ,slot)
   #+mcl
-  `(ccl:pref ,obj ,(read-from-string (format nil ":~a.~a" (keyword type) (keyword slot))))
+  `(ccl:pref ,obj ,(intern (concatenate 'string (symbol-name type) "." (symbol-name slot))
+                         :keyword))
   )
 
 #+mcl
@@ -130,12 +131,12 @@ of the enum-name name, separator-string, and field-name"
 
 
 (defmacro get-slot-pointer (obj type slot)
-  #+(or lispworks cmu sbcl) (declare (ignore type))
+  #+(or lispworks cmu sbcl scl) (declare (ignore type))
   #+allegro
   `(ff:fslot-value-typed ,type :c ,obj ,slot)
   #+lispworks
   `(fli:foreign-slot-pointer ,obj ,slot)
-  #+cmu
+  #+(or cmu scl)
   `(alien:slot ,obj ,slot)
   #+sbcl
   `(sb-alien:slot ,obj ,slot)
@@ -159,22 +160,31 @@ of the enum-name name, separator-string, and field-name"
 
 (defmacro deref-array (obj type i)
   "Returns a field from a row"
-  #+(or lispworks cmu sbcl) (declare (ignore type))
-  #+cmu  `(alien:deref ,obj ,i)
-  #+sbcl  `(sb-alien:deref ,obj ,i)
-  #+lispworks `(fli:dereference ,obj :index ,i)
+  #+(or lispworks cmu sbcl scl) (declare (ignore type))
+  #+(or cmu scl)  `(alien:deref ,obj ,i)
+  #+sbcl `(sb-alien:deref ,obj ,i)
+  #+lispworks `(fli:dereference ,obj :index ,i :copy-foreign-object nil)
   #+allegro `(ff:fslot-value-typed (quote ,(convert-from-uffi-type type :type)) :c ,obj ,i)
-  #+mcl
+  #+openmcl
   (let* ((array-type (array-type type))
          (local-type (convert-from-uffi-type array-type :allocation))
-         (accessor (first (macroexpand `(ccl:pref obj ,local-type)))))
-    `(,accessor 
+        (element-size-in-bits (ccl::%foreign-type-or-record-size local-type :bits)))
+    (ccl::%foreign-access-form
+     obj
+     (ccl::%foreign-type-or-record local-type)
+     `(* ,i ,element-size-in-bits)
+     nil))
+  #+(and mcl (not openmcl))
+  (let* ((array-type (array-type type))
+        (local-type (convert-from-uffi-type array-type :allocation))
+        (accessor (first (macroexpand `(ccl:pref obj ,local-type)))))
+    `(,accessor
       ,obj
       (* (the fixnum ,i) ,(size-of-foreign-type local-type))))
   )
 
 ; this expands to the %set-xx functions which has different params than %put-xx
-#+mcl
+#+(and mcl (not openmcl))
 (defmacro deref-array-set (obj type i value)
   (let* ((array-type (array-type type))
          (local-type (convert-from-uffi-type array-type :allocation))
@@ -185,7 +195,7 @@ of the enum-name name, separator-string, and field-name"
       (* (the fixnum ,i) ,(size-of-foreign-type local-type)) 
       ,value)))
 
-#+mcl
+#+(and mcl (not openmcl))
 (defsetf deref-array deref-array-set)
 
 (defmacro def-union (name &rest fields)
@@ -193,7 +203,7 @@ of the enum-name name, separator-string, and field-name"
   `(ff:def-foreign-type ,name (:union ,@(process-struct-fields name fields)))
   #+lispworks
   `(fli:define-c-union ,name ,@(process-struct-fields name fields))
-  #+cmu
+  #+(or cmu scl)
   `(alien:def-alien-type ,name (alien:union ,name ,@(process-struct-fields name fields)))
   #+sbcl
   `(sb-alien:define-alien-type ,name (sb-alien:union ,name ,@(process-struct-fields name fields)))
@@ -203,3 +213,37 @@ of the enum-name name, separator-string, and field-name"
   `(ccl::def-foreign-type nil 
                          (:union ,name ,@(process-struct-fields name fields)))
 )
+
+
+#-(or sbcl cmu)
+(defun convert-from-foreign-usb8 (s len)
+  (declare (optimize (speed 3) (space 0) (safety 0) (compilation-speed 0))
+          (fixnum len))
+  (let ((a (make-array len :element-type '(unsigned-byte 8))))
+    (dotimes (i len a)
+      (declare (fixnum i))
+      (setf (aref a i) (uffi:deref-array s '(:array :unsigned-byte) i)))))
+
+#+sbcl
+(defun convert-from-foreign-usb8 (s len)
+  (declare (type sb-sys:system-area-pointer sap))
+  (locally
+      (declare (optimize (speed 3) (safety 0)))
+    (let ((result (make-array len :element-type '(unsiged-byte 8))))
+      (sb-kernel:copy-from-system-area s 0
+                                      result (* sb-vm:vector-data-offset
+                                                sb-vm:n-word-bits)
+                                      (* len sb-vm:n-byte-bits))
+      result)))
+
+#+cmu
+(defun convert-from-foreign-usb8 (s le)
+  (declare (type system:system-area-pointer sap))
+  (locally
+      (declare (optimize (speed 3) (safety 0)))
+    (let ((result (make-array len :element-type '(unsiged-byte 8))))
+      (kernel:copy-from-system-area s 0
+                                   result (* vm:vector-data-offset
+                                             vm:word-bits)
+                                   (* len vm:byte-bits))
+      result)))