wanted: option to translate structs recursively

Bug #1473799 reported by 3b
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
CFFI
New
Undecided
Unassigned

Bug Description

it would be nice to have the option to completely translate a struct and all its contents to lisp data structures at once, for quick/simple bindings.

Attached patch is a partial implementation, but it doesn't handle shared structure/circularity which should probably be included in a full solution.

example (using attached patch):

(defcstruct ivec2.1
  (:x :int)
  (:y :int))

(defcstruct (nested.1 :recursive t)
  (ivec (:struct ivec2.1))
  (ivecs (:struct ivec2.1) :count 2)
  (ints :int :count 2))

(with-foreign-object (nested '(:struct nested.1) 2)
  (setf (mem-ref nested '(:struct nested.1))
        '(ivec (:x 1 :y 2)
          ivecs ((:x 11 :y 12) (:x 21 :y 22))
          ints (100 101)))
  (setf (mem-aref nested '(:struct nested.1) 1)
        '(ivec (:x 1001 :y 1002)
          ivecs ((:x 1011 :y 1012) (:x 1021 :y 1022))
          ints (1100 1101)))
  (list (mem-ref nested '(:struct nested.1))
        (mem-aref nested '(:struct nested.1) 1)))
;; =>
'((INTS #(100 101) IVECS #((:Y 12 :X 11) (:Y 22 :X 21)) IVEC (:Y 2 :X 1))
 (INTS #(1100 1101) IVECS #((:Y 1012 :X 1011) (:Y 1022 :X 1021))
  IVEC (:Y 1002 :X 1001)))

Revision history for this message
3b (00003b) wrote :
Revision history for this message
3b (00003b) wrote :

Actually, I guess if it is only handling translation of the actual contents of the structure, it doesn't need circularity/sharing checks.

Sharing would imply pointers, and it can't recursively translate to pointers without some protocol to allow freeing them properly (particularly difficult with sharing in the general case with separate alloc/free).

Revision history for this message
3b (00003b) wrote :

Possibly instead of adding an option to struct definitions, it would be better to decide whether to decode recursively on access, either with an extra option to MEM-REF/MEM-AREF or a separate function?

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.