unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* one C function to many Scheme functions
@ 2003-01-06 20:30 bajcik
  2003-01-08 20:45 ` Neil Jerram
  0 siblings, 1 reply; 6+ messages in thread
From: bajcik @ 2003-01-06 20:30 UTC (permalink / raw)


hello,

I know how to use gh_new_procedure* functions to declare function
visible in scheme script.

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

I would like to do it this way:

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

scm_func_t FuncTable[50]; /* Initialized */

SCM wrapper(SCM arg, void *data) /* !!! watch "data" */
{
	scm_func_t *sf = (scm_func_t *)data;
	my_type_t my_result;
	
	...
	my_arg = SCM2my(arg);
	...
	my_result = sf->func(my_arg)
	...

	return my2SCM(my_result);
}


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


Do you feel the sense?

FuncTable can't be a table with "SCM (*)(SCM)" functions, because my
program uses few script languages in plugins.

bajcik
-- 
.----- Krzysztof Garus ----- http://kolos.math.uni.lodz.pl/~bajcik/ --.
| http://kolos.math.uni.lodz.pl/~bajcik/duskc/ - proszę o krytykę :)  |


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: one C function to many Scheme functions
  2003-01-06 20:30 one C function to many Scheme functions bajcik
@ 2003-01-08 20:45 ` Neil Jerram
  2003-01-09 10:26   ` Krzysztof Garus
  2003-01-09 11:40   ` Matthias Koeppe
  0 siblings, 2 replies; 6+ messages in thread
From: Neil Jerram @ 2003-01-08 20:45 UTC (permalink / raw)
  Cc: guile-user

>>>>> "bajcik" == bajcik  <bajcik@kolos.math.uni.lodz.pl> 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?

        Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: one C function to many Scheme functions
  2003-01-08 20:45 ` Neil Jerram
@ 2003-01-09 10:26   ` Krzysztof Garus
  2003-01-09 23:30     ` Neil Jerram
  2003-01-09 11:40   ` Matthias Koeppe
  1 sibling, 1 reply; 6+ messages in thread
From: Krzysztof Garus @ 2003-01-09 10:26 UTC (permalink / raw)


On Wed, Jan 08, 2003 at 08:45:43PM +0000, Neil Jerram wrote:
> >>>>> "bajcik" == bajcik  <bajcik@kolos.math.uni.lodz.pl> 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 ...

[ cut ]

> Then in Scheme:
> 
> (define (name0 arg) (wrapper arg 0))
> (define (name1 arg) (wrapper arg 1))
> (define (name2 arg) (wrapper arg 2))
> ...
> 
> Does this help?

I am using similar workaround on similar concept, also with definitions.

Last question: what is cost this definition? I suppose it is small, but
if I call Scheme callback very frequent?

bajcik (not English spoken :> )
-- 
Krzysztof Garus <bajcik.kolos.math.uni.lodz@pl>            Linux User 171721
  Stronka: http://kolos.math.uni.lodz.pl/~bajcik/
  Polecam: http://kolos.math.uni.lodz.pl/~bajcik/duskc/


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: one C function to many Scheme functions
  2003-01-08 20:45 ` Neil Jerram
  2003-01-09 10:26   ` Krzysztof Garus
@ 2003-01-09 11:40   ` Matthias Koeppe
  2003-01-09 23:19     ` Neil Jerram
  1 sibling, 1 reply; 6+ messages in thread
From: Matthias Koeppe @ 2003-01-09 11:40 UTC (permalink / raw)
  Cc: guile-user

Neil Jerram <neil@ossau.uklinux.net> writes:

>>>>>> "bajcik" == bajcik  <bajcik@kolos.math.uni.lodz.pl> 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.  

What about applicable smobs?  The OP could make a new smob type for
"1-argument C closures".  The smob's APPLY function would call the
"wrapper" function, where the "void *data" would be fed from
SCM_SMOB_DATA.  (I think this requires a recent Guile (1.6).)

-- 
Matthias Köppe -- http://www.math.uni-magdeburg.de/~mkoeppe


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: one C function to many Scheme functions
  2003-01-09 11:40   ` Matthias Koeppe
@ 2003-01-09 23:19     ` Neil Jerram
  0 siblings, 0 replies; 6+ messages in thread
From: Neil Jerram @ 2003-01-09 23:19 UTC (permalink / raw)
  Cc: guile-user

>>>>> "Matthias" == Matthias Koeppe <mkoeppe@saturn.math.uni-magdeburg.de> writes:

    Matthias> Neil Jerram <neil@ossau.uklinux.net> writes:
    >>>>>>> "bajcik" == bajcik  <bajcik@kolos.math.uni.lodz.pl> 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.  

    Matthias> What about applicable smobs?  The OP could make a new
    Matthias> smob type for "1-argument C closures".  The smob's APPLY
    Matthias> function would call the "wrapper" function, where the
    Matthias> "void *data" would be fed from SCM_SMOB_DATA.  (I think
    Matthias> this requires a recent Guile (1.6).)

I didn't know about those - so sorry for being over-categorical
before.

Does anyone have any docs or full examples on applicable smobs?

        Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: one C function to many Scheme functions
  2003-01-09 10:26   ` Krzysztof Garus
@ 2003-01-09 23:30     ` Neil Jerram
  0 siblings, 0 replies; 6+ messages in thread
From: Neil Jerram @ 2003-01-09 23:30 UTC (permalink / raw)
  Cc: guile-user

>>>>> "Krzysztof" == Krzysztof Garus <bajcik@kolos.math.uni.lodz.pl> writes:

    Krzysztof> I am using similar workaround on similar concept, also
    Krzysztof> with definitions.

    Krzysztof> Last question: what is cost this definition? I suppose
    Krzysztof> it is small, but if I call Scheme callback very
    Krzysztof> frequent?

I doubt it's significant; anyway performance questions normally make
sense only in the context of a particular application.  If you do find
it's a slow point, ask here again.

        Neil



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-01-09 23:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-06 20:30 one C function to many Scheme functions bajcik
2003-01-08 20:45 ` Neil Jerram
2003-01-09 10:26   ` Krzysztof Garus
2003-01-09 23:30     ` Neil Jerram
2003-01-09 11:40   ` Matthias Koeppe
2003-01-09 23:19     ` Neil Jerram

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).