[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: one C function to many Scheme functions

From: Neil Jerram
Subject: Re: one C function to many Scheme functions
Date: 08 Jan 2003 20:45:43 +0000
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

>>>>> "bajcik" == bajcik  <address@hidden> writes:

    bajcik> But I want to declare many functions in a loop. I have a
    bajcik> table of names and a table of C-functions [ mytype_t
    bajcik> function(my_type arg) ] and only ONE C-function that
    bajcik> converts SCM to mytype_t. It is a wrapper.

Guile does not have anything like gh_new_procedure_data.  However your
general objective can easily be achieved like this ...

    bajcik> I would like to do it this way:

    bajcik> typedef struct
    bajcik> {
    bajcik>     mytype_t (*func)(mytype_t arg);
    bajcik>     char *name;
    bajcik> } scm_func_t;

    bajcik> scm_func_t FuncTable[50]; /* Initialized */

    bajcik> SCM wrapper(SCM arg, void *data) /* !!! watch "data" */

Change this to SCM wrapper(SCM arg, SCM index).

    bajcik> {
    bajcik>     scm_func_t *sf = (scm_func_t *)data;

Change this to:
                scm_func_t *sf = &(FuncTable[SCM_INUM(index)]);

    bajcik>     my_type_t my_result;
    bajcik>     ...
    bajcik>     my_arg = SCM2my(arg);
    bajcik>     ...
    bajcik>     my_result = sf->func(my_arg)
    bajcik>     ...

    bajcik>     return my2SCM(my_result);
    bajcik> }

    bajcik> void declare_all_functions()
    bajcik> {
    bajcik>     int i;
    bajcik>     for (i=0; i<50; i++)
    bajcik>             gh_new_procedure_data(FuncTable[i].func, &FuncTable[i]);

Change this to a gh_new_procedure for just one Scheme function, which
maps to wrapper.  (With no loop.)

Then in Scheme:

(define (name0 arg) (wrapper arg 0))
(define (name1 arg) (wrapper arg 1))
(define (name2 arg) (wrapper arg 2))

Does this help?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]