[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Check for NULL struct
From: |
Felix |
Subject: |
Re: [Chicken-users] Check for NULL struct |
Date: |
Wed, 30 Mar 2011 09:47:25 +0200 (CEST) |
From: Cosme Enmanuel Zamudio Salazar <address@hidden>
Subject: [Chicken-users] Check for NULL struct
Date: Tue, 29 Mar 2011 09:46:53 -0700
> Is there a way to check for a NULL structure returned from a C function?
>
> this is the structure
>
> (define-record sdl-surface pointer)
>
> (define-record-printer (sdl-surface s out)
> (for-each (lambda (x) (display x out))
> (list "#<sdl-surface "(sdl-surface-pointer s)">")))
>
> (define-foreign-type SDL_Surface (c-pointer "SDL_Surface")
> sdl-surface-pointer
> make-sdl-surface)
>
>
> the NULL can be returned when i try to load an image that doesnt exists:
>
> #;12> (img-load "test.png")
> #<sdl-surface #f>
>
> but how do i check if its NULL ?
"c-pointer" usually maps to "#f" (as Kon pointed out in his reply).
But since you are using automatic argument/result conversion by wrapping
the pointer into a struct, you will get an "#f" in the pointer field.
You ccould use a different result wrapper in your foreign type definition:
(define (make-sdl-surface-wrapper ptr)
(and ptr (make-sdl-surface ptr)))
Or check explicitly with "(not (sdl-surface-pointer SURFACE))".
Raw pointer objects that contain a NULL pointer are usually mapped to
"#f" by the FFI, but in some cases are not, so you can check a pointer
object for a NULL-address with "(null-pointer? PTR)" (I think this is
not necessary here, I just mention it for completeness).
cheers,
felix