Code: Select all
(defmacro vec-int-to-lisp-vec (&rest args)
(let ((x (gensym))
(y (gensym))
(z (gensym)))
`(let* ((,x (first (list ,@args)))
(,y (vec-int-length ,x))
(,z (make-array ,y :element-type t :fill-pointer 0
:initial-element
nil)))
(dotimes (i ,y)
(vector-push (mem-aref (vec-int-to-c-arr ,x) :int i) ,z))
,z)))
I have this function c-arr-to-vec-int which converts a c array of integers created by foreign-alloc to a C++ vector<int>. It is used like this:
Code: Select all
(defparameter a (c-arr-to-vec-int (foreign-alloc :int :initial-contents '(1 2 3 4 5)) 5))
Code: Select all
CV> (vec-int-to-c-arr a)
#.(SB-SYS:INT-SAP #X7FFF965C3040) <--This is analagous to (foreign-alloc :int :initial-contents '(1 2 3 4 5))
Code: Select all
(foreign-alloc :int :initial-contents '(1 2 3 4 5))
Code: Select all
CV> (mem-aref (vec-int-to-c-arr a) :int)
1
CV> (mem-aref (vec-int-to-c-arr a) :int 1)
2
The vec-int-length function just retrieve a C++ vector length.