Question about FFI and constants

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about FFI and constants

Mariano Martinez Peck
Hi folks: I am calling to the OpenDBX library and this library uses C constants. Suppose something like this:

enum odbxerr {
    ODBX_ERR_SUCCESS,
#define ODBX_ERR_SUCCESS   ODBX_ERR_SUCCESS
    ODBX_ERR_BACKEND,
#define ODBX_ERR_BACKEND   ODBX_ERR_BACKEND
    ODBX_ERR_NOCAP,
#define ODBX_ERR_NOCAP   ODBX_ERR_NOCAP
    ODBX_ERR_PARAM,
.........


Then I have a function like this:

char* odbx_error( odbx_t* handle, int error )
In that case the int error es the index of the array or I can use the constant.
Right now, I am using int from 0 to N. But I would like to invoke calls to that function using Smalltalk Strings that represent C constants.

For example, now I have this:

OpenDBXUnix>>apiError: handle number: err
    "long odbx_error(odbx_t*, int)"
    <cdecl: char* 'odbx_error' (ulong long) module: 'opendbx' >
    ^self externalCallFailed   

And I call them this way for example:

OpenDBXUnix apiError:  handle number: 1.

But, I would love to do:

OpenDBXUnix apiError:  handle number: 'ODBX_ERR_BACKEND'.

Of course this fails because it cannot coerce and String to a long.

So, that someone know how can I do this (if I can) ?

Thanks

Mariano



_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about FFI and constants

hernanmd
Another alternative to what Andreas suggested is to reify in
meaningful messages the constants, see senders of
YAZODRFFILibrary>>odrCreate: in the Z3950 project at SqueakSource.
Cheers,

Hernán

2009/9/30 Mariano Martinez Peck <[hidden email]>:

> Hi folks: I am calling to the OpenDBX library and this library uses C
> constants. Suppose something like this:
>
> enum odbxerr {
>     ODBX_ERR_SUCCESS,
> #define ODBX_ERR_SUCCESS   ODBX_ERR_SUCCESS
>     ODBX_ERR_BACKEND,
> #define ODBX_ERR_BACKEND   ODBX_ERR_BACKEND
>     ODBX_ERR_NOCAP,
> #define ODBX_ERR_NOCAP   ODBX_ERR_NOCAP
>     ODBX_ERR_PARAM,
> .........
>
>
> Then I have a function like this:
>
> char* odbx_error( odbx_t* handle, int error )
>
> In that case the int error es the index of the array or I can use the
> constant.
> Right now, I am using int from 0 to N. But I would like to invoke calls to
> that function using Smalltalk Strings that represent C constants.
>
> For example, now I have this:
>
> OpenDBXUnix>>apiError: handle number: err
>     "long odbx_error(odbx_t*, int)"
>     <cdecl: char* 'odbx_error' (ulong long) module: 'opendbx' >
>     ^self externalCallFailed
>
> And I call them this way for example:
>
> OpenDBXUnix apiError:  handle number: 1.
>
> But, I would love to do:
>
> OpenDBXUnix apiError:  handle number: 'ODBX_ERR_BACKEND'.
>
> Of course this fails because it cannot coerce and String to a long.
>
> So, that someone know how can I do this (if I can) ?
>
> Thanks
>
> Mariano
>
>
>
>
>
>

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Question about FFI and constants

Fernando olivero
Hi Mariano, what about the rule in Kent Beck Smalltalk best practices  
book, put magic numbers into a method.

OpenDBUnix>>odbxErrBackend.
        ^ 1

OpenDBUnix apiError: handle number:  OpenDBUnix odbxErrBackend.



Or i you prefer also can code a mapping method like this one  
( although the first solution is much more elegant )

OpenDBUnix>>errorFromString: 'ODBX_ERR_BACKEND'

OpenDBUnix apiError: handle number:  ( OpenDBUnix  errorFromString:  
'ODBX_ERR_BACKEND')


> 2009/9/30 Mariano Martinez Peck <[hidden email]>:
>> Hi folks: I am calling to the OpenDBX library and this library uses C
>> constants. Suppose something like this:
>>
>> enum odbxerr {
>>     ODBX_ERR_SUCCESS,
>> #define ODBX_ERR_SUCCESS   ODBX_ERR_SUCCESS
>>     ODBX_ERR_BACKEND,
>> #define ODBX_ERR_BACKEND   ODBX_ERR_BACKEND
>>     ODBX_ERR_NOCAP,
>> #define ODBX_ERR_NOCAP   ODBX_ERR_NOCAP
>>     ODBX_ERR_PARAM,
>> .........
>>
>>
>> Then I have a function like this:
>>
>> char* odbx_error( odbx_t* handle, int error )
>>
>> In that case the int error es the index of the array or I can use the
>> constant.
>> Right now, I am using int from 0 to N. But I would like to invoke  
>> calls to
>> that function using Smalltalk Strings that represent C constants.
>>
>> For example, now I have this:
>>
>> OpenDBXUnix>>apiError: handle number: err
>>     "long odbx_error(odbx_t*, int)"
>>     <cdecl: char* 'odbx_error' (ulong long) module: 'opendbx' >
>>     ^self externalCallFailed
>>
>> And I call them this way for example:
>>
>> OpenDBXUnix apiError:  handle number: 1.
>>
>> But, I would love to do:
>>
>> OpenDBXUnix apiError:  handle number: 'ODBX_ERR_BACKEND'.
>>
>> Of course this fails because it cannot coerce and String to a long.
>>
>> So, that someone know how can I do this (if I can) ?
>>
>> Thanks
>>
>> Mariano
>>
>>
>>
>>
>>
>>
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project