www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Array to variadic function?

reply bobef <bobef lessequal.com> writes:
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...
Nov 28 2005
next sibling parent reply pragma <pragma_member pathlink.com> writes:
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
Nov 28 2005
parent bobef <bobef lessequal.com> writes:
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)
Dec 01 2005
prev sibling parent reply "Walter Bright" <newshound digitalmars.com> writes:
"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 ...)
Dec 01 2005
next sibling parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
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.
Dec 01 2005
prev sibling parent reply bobef <bobef lessequal.com> writes:
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"
Dec 02 2005
parent reply pragma <pragma_member pathlink.com> writes:
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
Dec 02 2005
parent bobef <bobef lessequal.com> writes:
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())); //............ }
Dec 03 2005