2012-12-20 59 views
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))) 

這樣就不會分配額外的內存,並且可以直接設置值。

那麼這是我的小白日夢,有誰知道它是否可以工作?

回答

0

閱讀了CFFI源代碼,似乎這不是通過內置機制支持的。那裏有處理數組類型的代碼,但它沒有暴露或導出,因此顯然不能使用。還有一個建議的塊存儲器接口,但尚未編寫。

所以這次沒有骰子,但CFFI太棒了,所以我只是用不同的方式做出來。

p.s.還有WAAF-CFFI,這是值得研究,但我沒有具體的這個,但