[gambit-list] is this a bug in ffi?

Mikael More mikael.more at gmail.com
Sun Feb 5 16:01:58 EST 2012


Ouch. Well then what we addressed in previous email makes the most sense, I
suppose - defining constant variables with integer contents (define const1
0) (define const2 1) etc. and then manually converting in the C code:
(define p (c-lambda (int) .. #<<c  enumtype v;   switch where based on
___arg1 you set v    case 0: v = CONST1; break; case 1: v = CONST2; and so
on.

2012/2/5 Álvaro Castro-Castilla <alvaro.castro.castilla at gmail.com>

> Mikael,
>
> I think I don't follow you. The problem is that C++ doesn't allow casting
> from int to enum, while I think C does. There is a link about this in
> stackoverflow:
> http://stackoverflow.com/questions/4165439/generic-way-to-cast-int-to-enum-in-c
>
> The cairo type is defined as an enum, so if compiling with the C compiler
> it works seamlessly with scheme's integers, however, if using C++ compiler,
> it doesn't cast it implicitly. Thus the need for this -more complex-
> machiner.
>
> That's my understanding, but I might be wrong.
>
>
>
>
>
> On Sun, Feb 5, 2012 at 8:10 PM, Mikael <mikael.rcv at gmail.com> wrote:
>
>> Are you sure that, now that each constant has a fixed integer value, the
>> compiler is allowed to represent this internally as anything else than an
>> int?
>> Check it out. If not, you know it's an int, so you could just typecast
>>
>>
>> 2012/2/5 Álvaro Castro-Castilla <alvaro.castro.castilla at gmail.com>
>>
>>> This is the definition  in C:
>>>
>>> typedef enum _cairo_format {
>>>     CAIRO_FORMAT_INVALID   = -1,
>>>     CAIRO_FORMAT_ARGB32    = 0,
>>>     CAIRO_FORMAT_RGB24     = 1,
>>>     CAIRO_FORMAT_A8        = 2,
>>>     CAIRO_FORMAT_A1        = 3,
>>>     CAIRO_FORMAT_RGB16_565 = 4
>>> } cairo_format_t;
>>>
>>>
>>> AFAIK, bindings in Gambit usually deal with this as an integer, but with
>>> C++ enabled, conversion is needed.
>>>
>>>
>>>
>>>
>>> 2012/2/5 Mikael <mikael.rcv at gmail.com>
>>>
>>>>
>>>>
>>>> 2012/2/5 Álvaro Castro-Castilla <alvaro.castro.castilla at gmail.com>
>>>>
>>>>> Hi!
>>>>>
>>>>> I open a new thread, because this is a deeper issue than the C++ enums
>>>>> conversion problem.
>>>>>
>>>>> When I declare this:
>>>>>
>>>>> (define CAIRO_FORMAT_RGB24 ((c-lambda () cairo-format-t "___result =
>>>>> CAIRO_FORMAT_RGB24;")))
>>>>>
>>>>> or:
>>>>> (define CAIRO_FORMAT_RGB24 (c-lambda () cairo-format-t "___result =
>>>>> CAIRO_FORMAT_RGB24;"))
>>>>> and then call
>>>>> (CAIRO_FORMAT_RGB24)
>>>>>
>>>>> my program segfaults. It seems that just executing that c-lambda,
>>>>> returning an enum, fails. The enum is defined as:  (c-define-type
>>>>> cairo-format-t "cairo_format_t")
>>>>>
>>>>
>>>> Ouch - your declaration makes Gambit identify, I think, cairo_format_t
>>>> as a C structure, and tries to copy its contents fully. Hmm. If this is not
>>>> how it really is, do (c-define-type cairo-format-t integer) or sth?
>>>>
>>>>>
>>>>> I don't understand why this segfaults: 1) I'm declaring the type 2)
>>>>> I'm returning the proper type. What's the problem?
>>>>>
>>>>>
>>>>> Best regards,
>>>>>
>>>>> Álvaro
>>>>>
>>>>> _______________________________________________
>>>>> Gambit-list mailing list
>>>>> Gambit-list at iro.umontreal.ca
>>>>> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>>>>>
>>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20120205/662af437/attachment.htm>


More information about the Gambit-list mailing list