3
最近我一直在玩奇妙的cffi,但是我遇到了一些問題,如果某些東西應該可以使用build機制。Common Lisp:Cffi:Setf'ing Foreign Type
我想創建一個向量的外部數組,並設置了外部類型轉換,這樣纔有可能。
> (defparameter a (foreign-alloc 'vec3 :count 10))
A
> (setf (mem-aref a 'vec3 4) (convert-to-foreign #(1.0 2.0 3.0) 'vec3))
#(1.0 2.0 3.0)
> (convert-from-foreign (mem-aref b 'vec3 4) 'vec3)
#(1.0 2.0 3.0
到目前爲止,我有以下的允許創建和獲得外國vec3但不是檢索。
(defcstruct %vec3
(components :float :count 3))
(define-foreign-type vec3-type()
()
(:actual-type %vec3)
(:simple-parser vec3))
(defmethod translate-from-foreign (value (type vec3-type))
(make-array
3
:element-type 'single-float
:initial-contents (list (mem-aref value :float 0)
(mem-aref value :float 1)
(mem-aref value :float 2))))
的問題是,雖然我可以很容易地建立一個函數來充當VEC3一個二傳手,我喜歡那裏有一些在做這個,我只是沒有看到的內置方式。我已經閱讀了cffi手冊,並且看到了翻譯到外國的方法以及擴展到外國的dyn,但是我還沒有找到一種方法來使用本質上擴展到如下的東西:
(let ((tmp (mem-aref a '%vec3 4)))
(setf (mem-aref tmp :float 0) (aref lisp-value 0))
(setf (mem-aref tmp :float 1) (aref lisp-value 1))
(setf (mem-aref tmp :float 2) (aref lisp-value 2)))
這樣就不會分配額外的內存,並且可以直接設置值。
那麼這是我的小白日夢,有誰知道它是否可以工作?