Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing an element at an index from an array of structs through c-ref #76

Open
ghost opened this issue Jun 16, 2017 · 2 comments
Open

Comments

@ghost
Copy link

ghost commented Jun 16, 2017

From the c-plus examples:

(c-ref object foo-t :a)              ;; => object.a
(c-ref object foo-t :c 2)            ;; => object.c[2]
(c-ref object foo-t :x 2 :b0)        ;; => object.x[2].b0
(c-ref object foo-t :x :b0)          ;; => object.x[0].b0
(c-ref object foo-t :c *)            ;; => *(object.c)
(c-ref object foo-t :x 1 :s :x &)    ;; => &(object.x[1].s.x)

Is there a simpler way to access an element at an index from an array of structs?

(...)              ;; => object[1].a

Currently, I get a pointer first using c-aptr:

(c-with ((iovec (:struct (c:iovec)) :count 2))
      (c-ref (c-aptr iovec 0 '(:struct (c:iovec))) ;; pointer to struct at index 0
             (:struct (c:iovec)) ;; pointer type
             :iov-len)) ;; member

A cleaner way would be something like:

(c-with ((iovec (:struct (c:iovec)) :count 2))
      (iovec :index 0 :iov-len))
@rpav
Copy link
Owner

rpav commented Jun 16, 2017

Does a number in the first position not work? I.e., (iovec 0 :iov-len)?

@ghost
Copy link
Author

ghost commented Jun 17, 2017

The c-with macro has the keyword count, however the c-ref macro does not. The c-ref macro can only access an index of a member array of a struct, but not an array of structs.

(plus-c:c-with ((iovec (:struct (c:iovec)) :count 2))
      (iovec 0 :iov-len))
; in: PLUS-C:C-WITH ((IOVEC (:STRUCT (C:IOVEC)) :COUNT 2))
;     (IOVEC 0 :IOV-LEN)
; ==>
;   (PLUS-C:C-REF #:TMP951 (:STRUCT (C:IOVEC)) 0 :IOV-LEN)
; 
; caught ERROR:
;   during macroexpansion of (PLUS-C:C-REF #:TMP951 (:STRUCT #) ...). Use
;   *BREAK-ON-SIGNALS* to intercept.
;   
;    Error parsing ref: 0 on type #<AUTOWRAP:FOREIGN-RECORD IOVEC {1002173723}>
; 
; compilation unit finished
;   caught 1 ERROR condition

debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread
#<THREAD "main thread" RUNNING {100301E883}>:
  Execution of a form compiled with errors.
Form:
  (PLUS-C:C-REF #:TMP951 (:STRUCT (C:IOVEC)) 0 :IOV-LEN)
Compile-time error:
  during macroexpansion of (PLUS-C:C-REF #:TMP951 (:STRUCT #) ...). Use
*BREAK-ON-SIGNALS* to intercept.

 Error parsing ref: 0 on type #<AUTOWRAP:FOREIGN-RECORD IOVEC {1002173723}>

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

((LAMBDA ()))
   source: (IOVEC 0 :IOV-LEN)
0] 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant