/[cffi-objects]/struct.lisp
ViewVC logotype

Diff of /struct.lisp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by rklochkov, Thu Feb 9 15:53:55 2012 UTC revision 9 by rklochkov, Fri Aug 24 19:26:53 2012 UTC
# Line 21  Struct may be used in OBJECT cffi-type o Line 21  Struct may be used in OBJECT cffi-type o
21  (defgeneric free-struct (class value)  (defgeneric free-struct (class value)
22    (:method (class value)    (:method (class value)
23      (declare (ignore class))      (declare (ignore class))
24     ;   (break)
25        ;(format t "Free ~a ~a~%" class value)
26      (foreign-free value)))      (foreign-free value)))
27    
28  (defmethod gconstructor ((struct struct) &key new-struct &allow-other-keys)  (defmethod gconstructor ((struct struct) &key new-struct &allow-other-keys)
# Line 28  Struct may be used in OBJECT cffi-type o Line 30  Struct may be used in OBJECT cffi-type o
30        (new-struct (class-name (class-of struct)))        (new-struct (class-name (class-of struct)))
31        (progn        (progn
32          (setf (slot-value struct 'value) nil)          (setf (slot-value struct 'value) nil)
33            (setf (slot-value struct 'free-after) nil)
34          (null-pointer))))          (null-pointer))))
35    
36  (defun pair (maybe-pair)  (defun pair (maybe-pair)
# Line 48  or may be cons (class-name . struct-name Line 51  or may be cons (class-name . struct-name
51                  (if (slot-boundp ,class-name 'value)                  (if (slot-boundp ,class-name 'value)
52                      (getf (slot-value ,class-name 'value) ',x)                      (getf (slot-value ,class-name 'value) ',x)
53                      (foreign-slot-value (pointer ,class-name)                      (foreign-slot-value (pointer ,class-name)
54                                          ',struct-name ',x)))                                          '(:struct ,struct-name) ',x)))
55                (unless (fboundp '(setf ,x))                (unless (fboundp '(setf ,x))
56                  (defgeneric (setf ,x) (val ,class-name)))                  (defgeneric (setf ,x) (val ,class-name)))
57                (defmethod (setf ,x) (val (,class-name ,class-name))                (defmethod (setf ,x) (val (,class-name ,class-name))
58                  (if (slot-boundp ,class-name 'value)                  (if (slot-boundp ,class-name 'value)
59                      (setf (getf (slot-value ,class-name 'value) ',x) val)                      (setf (getf (slot-value ,class-name 'value) ',x) val)
60                      (setf (foreign-slot-value (pointer ,class-name)                      (setf (foreign-slot-value (pointer ,class-name)
61                                                ',struct-name ',x)                                                '(:struct ,struct-name) ',x)
62                            val)))                            val)))
63                (save-setter ,class-name ,x)))                (save-setter ,class-name ,x)))
64            (foreign-slot-names struct-name)))))            (foreign-slot-names `(:struct ,struct-name))))))
65    
66  (defmacro defbitaccessors (class slot &rest fields)  (defmacro defbitaccessors (class slot &rest fields)
67    (let ((pos 0))    (let ((pos 0))
# Line 99  or may be cons (class-name . struct-name Line 102  or may be cons (class-name . struct-name
102                      (setf (foreign-slot-value res class slot) val))))                      (setf (foreign-slot-value res class slot) val))))
103                (foreign-slot-names class))                (foreign-slot-names class))
104          res)          res)
105        (slot-value object 'pointer)))        (pointer object)))
106    
107  (defun struct->clos (class struct &optional object)  (defun struct->clos (class struct &optional object)
108    (let ((res (or object (make-instance class))))    "Translates pointer STRUCT to object OBJECT (if not supplied, then to new
109      (setf (slot-value res 'value) nil)  object).
110      (mapc (lambda (slot)    I suppose, that by default it should convert data from pointer to struct.
111              (setf (getf (slot-value res 'value) slot)  Only exception is the presence of OBJECT with not boundp value"
112                    (foreign-slot-value struct class slot)))    (let ((%object (or object
113            (foreign-slot-names class))                       (unless (null-pointer-p struct)
114      res))                         (make-instance class :pointer struct)))))
115        (when %object
116          (if (slot-boundp %object 'value)
117              (progn
118                (setf (slot-value %object 'value) nil)
119                (unless (null-pointer-p struct)
120                  (dolist (slot (foreign-slot-names class))
121                    (setf (getf (slot-value %object 'value) slot)
122                          (foreign-slot-value struct class slot)))))
123              (setf (pointer %object) struct))
124          %object)))
125    
126  (define-foreign-type cffi-struct (cffi-object freeable-out)  (define-foreign-type cffi-struct (cffi-object freeable-out)
127    ()    ()
128    (:actual-type :pointer))    (:actual-type :pointer))
129    
130  (defmethod free-ptr ((type cffi-struct) ptr)  (defmethod free-sent-ptr ((type cffi-struct) ptr place)
131    (free-struct (object-class type) ptr))    (when (and (slot-boundp place 'value) (not (null-pointer-p ptr)))
132        (free-struct (object-class type) ptr)))
133    
134    (defmethod free-returned-ptr ((type cffi-struct) ptr)
135      (unless (null-pointer-p ptr)
136        (free-struct (object-class type) ptr)))
137    
138    
139  (defmethod foreign-type-size ((type cffi-struct))  (defmethod foreign-type-size ((type cffi-struct))
140    "Return the size in bytes of a foreign typedef."    "Return the size in bytes of a foreign typedef."
141    (foreign-type-size (object-class type)))    (foreign-type-size (object-class type)))
142    
143  (define-parse-method struct (class &key (free :no-transfer) out)  (define-parse-method struct (class &rest rest)
144    (make-instance 'cffi-struct    (apply #'make-instance 'cffi-struct :class class rest))
                  :class class :free free :out out))  
145    
146  (defun %class (type value)  (defun %class (type value)
147    (or (object-class type) (class-name (class-of value))))    (or (object-class type) (class-name (class-of value))))
148    
149  (defmethod copy-from-foreign ((type cffi-object) ptr place)  (defmethod copy-from-foreign ((type cffi-object) ptr place)
150    (when (or (slot-boundp place 'value)    (when (slot-boundp place 'value)
             (member (object-free type) '(:all :transfer)))  
151      (struct->clos (%class type place) ptr place)))      (struct->clos (%class type place) ptr place)))
152    
153    ;; cffi-object is not tyoo. It is for use struct with object designator
154  (defmethod translate-to-foreign ((value struct) (type cffi-object))  (defmethod translate-to-foreign ((value struct) (type cffi-object))
155    (values (clos->new-struct (%class type value) value) value))    (values (clos->new-struct (%class type value) value) value))
156    
# Line 142  or may be cons (class-name . struct-name Line 158  or may be cons (class-name . struct-name
158    (struct->clos (object-class type) value))    (struct->clos (object-class type) value))
159    
160  ;;; Allowed use with object designator  ;;; Allowed use with object designator
161  ;; object == (struct nil :out t :free t)  ;; object == (struct nil)
162    
163    
164  ;; to allow using array of structs  ;; to allow using array of structs

Legend:
Removed from v.1  
changed lines
  Added in v.9

  ViewVC Help
Powered by ViewVC 1.1.5