Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 28, 2005 Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Is there some way to pass something like array to variadic function. I mean often there is no variant where function take valist instead of ... and when you need to wrap this function what do you do? Write many ifs? I will illustrate my question with example void someCfunction(int a,...) { ... do stuff here } void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values) { if(values.length==1) someCfunction(345,values[0].toString()); else if(values.length==2) someCfunction(543,values[0].toString(),values[1].toString()); //etc... } There must be a better way to do this... |
November 28, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | In article <dmfcgr$2hpa$1@digitaldaemon.com>, bobef says... > >Is there some way to pass something like array to variadic function. I mean often there is no variant where function take valist instead of ... and when you need to wrap this function what do you do? Write many ifs? I will illustrate my question with example > >void someCfunction(int a,...) >{ > ... do stuff here >} > > >void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values) >{ > if(values.length==1) someCfunction(345,values[0].toString()); > else if(values.length==2) >someCfunction(543,values[0].toString(),values[1].toString()); > //etc... >} > >There must be a better way to do this... I'm shooting from the hip here, but have you tried this? void foobar(int a,void*[]...){ /* stuff */ } - EricAnderton at yahoo |
December 01, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | pragma wrote:
> In article <dmfcgr$2hpa$1@digitaldaemon.com>, bobef says...
>
>>Is there some way to pass something like array to variadic function. I mean often there is no variant where function take valist instead of ... and when you need to wrap this function what do you do? Write many ifs?
>>I will illustrate my question with example
>>
>>void someCfunction(int a,...)
>>{
>> ... do stuff here
>>}
>>
>>
>>void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values)
>>{
>> if(values.length==1) someCfunction(345,values[0].toString());
>> else if(values.length==2) someCfunction(543,values[0].toString(),values[1].toString());
>> //etc...
>>}
>>
>>There must be a better way to do this...
>
>
> I'm shooting from the hip here, but have you tried this?
>
> void foobar(int a,void*[]...){ /* stuff */ }
>
> - EricAnderton at yahoo
I can't. I am not allowed to change any of the two functions. The one is defined by dmdscript and the other by the (guy_who_wrote_it !is me)
|
December 02, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | "bobef" <bobef@lessequal.com> wrote in message news:dmfcgr$2hpa$1@digitaldaemon.com... > Is there some way to pass something like array to variadic function. I mean often there is no variant where function take valist instead of ... and when you need to wrap this function what do you do? Write many ifs? I will illustrate my question with example > > void someCfunction(int a,...) > { > ... do stuff here > } > > > void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values) > { > if(values.length==1) someCfunction(345,values[0].toString()); > else if(values.length==2) > someCfunction(543,values[0].toString(),values[1].toString()); > //etc... > } > > There must be a better way to do this... Redeclare someCfunction as: void someCfunction(int[] a ...) |
December 02, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
> "bobef" <bobef@lessequal.com> wrote in message
> news:dmfcgr$2hpa$1@digitaldaemon.com...
>
>>Is there some way to pass something like array to variadic function. I
>>mean often there is no variant where function take valist instead of ...
>>and when you need to wrap this function what do you do? Write many ifs?
>>I will illustrate my question with example
>>
>>void someCfunction(int a,...)
>>{
>>... do stuff here
>>}
>>
>>
>>void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values)
>>{
>>if(values.length==1) someCfunction(345,values[0].toString());
>>else if(values.length==2)
>>someCfunction(543,values[0].toString(),values[1].toString());
>>//etc...
>>}
>>
>>There must be a better way to do this...
>
>
> Redeclare someCfunction as:
>
> void someCfunction(int[] a ...)
>
I think the problem is you can't allways redeclare a function.
What would be nice is to be able to construct arguments and pass them to another function.
For example to be able to (with ...) take n arguments, and pass to another variadic function some of them, for example a slice.
|
December 02, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright wrote:
> "bobef" <bobef@lessequal.com> wrote in message
> news:dmfcgr$2hpa$1@digitaldaemon.com...
>
>>Is there some way to pass something like array to variadic function. I
>>mean often there is no variant where function take valist instead of ...
>>and when you need to wrap this function what do you do? Write many ifs?
>>I will illustrate my question with example
>>
>>void someCfunction(int a,...)
>>{
>>... do stuff here
>>}
>>
>>
>>void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values)
>>{
>>if(values.length==1) someCfunction(345,values[0].toString());
>>else if(values.length==2)
>>someCfunction(543,values[0].toString(),values[1].toString());
>>//etc...
>>}
>>
>>There must be a better way to do this...
>
>
> Redeclare someCfunction as:
>
> void someCfunction(int[] a ...)
>
>
extern(C) void someCfunction(int[] a...)
{
printf("as %d %d\0",a);
}
int main(char[][] argv)
{
int[] a;
a~=5;a~=6;
someCfunction(a);
return 1;
}
outputs "as 2 9243472"
|
December 02, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bobef | In article <dmpvro$2b3f$1@digitaldaemon.com>, bobef says... > >Walter Bright wrote: >> "bobef" <bobef@lessequal.com> wrote in message news:dmfcgr$2hpa$1@digitaldaemon.com... >> >>>Is there some way to pass something like array to variadic function. I mean often there is no variant where function take valist instead of ... and when you need to wrap this function what do you do? Write many ifs? I will illustrate my question with example >>> >>>void someCfunction(int a,...) >>>{ >>>... do stuff here >>>} >>> >>> >>>void dmdscript_for_example_function(/*all_the_stuff*/,Value[] values) >>>{ >>>if(values.length==1) someCfunction(345,values[0].toString()); >>>else if(values.length==2) >>>someCfunction(543,values[0].toString(),values[1].toString()); >>>//etc... >>>} >>> >>>There must be a better way to do this... >> >> >> Redeclare someCfunction as: >> >> void someCfunction(int[] a ...) >> >> > >extern(C) void someCfunction(int[] a...) >{ > printf("as %d %d\0",a); >} > >int main(char[][] argv) >{ > int[] a; > a~=5;a~=6; > someCfunction(a); > return 1; >} > >outputs "as 2 9243472" Try this: >extern(C) void someCfunction(int[] a...) >{ > writefln("as %s",a); >} - EricAnderton at yahoo |
December 03, 2005 Re: Array to variadic function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | pragma wrote: >>outputs "as 2 9243472" > > > Try this: > > >>extern(C) void someCfunction(int[] a...) >>{ >> writefln("as %s",a); >>} > > > - EricAnderton at yahoo I got confused....... This is the real code I am asking the question for. There is no xchat_emit_writefln... And xchat_emit_print is what I meant with someCfunction... //this is part of struct actually extern(C) int (*xchat_emit_print) (xchat_plugin *ph,char *event_name, ...); void* g_xchat_emit_print(Dobject pthis,CallContext* cc,Dobject othis,Value* ret,Value[] arglist) { //todo: the whole arglist should be passed to the variadic function Value *v=getErrorReturn(arglist,1,"xchat_emit_print"); if(!v) { if(arglist.length==1) this_ph.xchat_emit_print(this_ph,toStringz(arglist[0].toString())); else if(arglist.length==2) this_ph.xchat_emit_print(this_ph,toStringz(arglist[0].toString()),toStringz(arglist[1].toString())); //............ } |
Copyright © 1999-2021 by the D Language Foundation