www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Pass _arguments to next method?

reply Benjamin Schulte <Aldoric gmx.de> writes:
Hi!

For example, we have the following methods: (Just example - might not be useful
;P)
void calc_sum( ... ) { /* do something */ }
void calc_dif( ... ) { /* do something */ }
void calc( int what, ... ) {
  switch( what ) {
    case 0: calc_sum( /* here is the problem */ ); break;
    case 1: calc_dif( /* here is the problem */ ); break;
  }
}

How can I pass the _arguments to the next called methods?

Thanks in advance :)
Jul 04 2008
next sibling parent reply TomD <t_demmer.nospam web.de> writes:
Benjamin Schulte Wrote:
[...]

http://www.digitalmars.com/d/1.0/function.html
see the section about variadic functions.

Ciao
Tom
Jul 04 2008
next sibling parent Benjamin Schulte <Aldoric gmx.de> writes:
I already did so, and I did again as you said. But I can't find the solution
for my problem. I can not do something like:

switch( _arguments.length ) {
case 0: call_sum( ); break;
case 1: call_sum( va_arg!(int)(_argptr)) ); break;
case 2: call_sum( va_arg!(int)(_argptr)), va_arg!(int)(_argptr)) ); break;
case 3: call_sum( va_arg!(int)(_argptr)), va_arg!(int)(_argptr)),
va_arg!(int)(_argptr)) ); break;
}

That would be too crazy as far that I don't even know the type of the arguments
that are passed. I just want to copy the current argument list of the current
called method to the next one.

call_sum( _arguments ); // would only pass ONE argument
call_sum( _argptr ); // would also only pass ONE argument
call_sum( ... ); // does not work


One solution surely would be to redesign the call_sum method to something like

void call_sum( void* argptr, uint argcnt ) {
  /* ... */
}

But isn't it possible a diffent way?

Maybe this is a better example:

void debugln( ... ) {
  // save argument data to file
  // ...
  // display message
  writefln( ~arguments~ );
}


TomD Wrote:

 Benjamin Schulte Wrote:
 [...]
 
 http://www.digitalmars.com/d/1.0/function.html
 see the section about variadic functions.
 
 Ciao
 Tom
 

Jul 04 2008
prev sibling parent "Koroskin Denis" <2korden gmail.com> writes:
On Fri, 04 Jul 2008 14:56:44 +0400, Benjamin Schulte <Aldoric gmx.de>  
wrote:

 nd I did again as you said. But I can't find the solution for my  
 problem. I can not do something like:

Try this: import std.stdio; void debugln(T...)(T t) { writefln(t); } int main() { debugln("hello", ", ", "world!"); return 0; }
Jul 04 2008
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Benjamin Schulte" <Aldoric gmx.de> wrote in message 
news:g4kpm3$16km$1 digitalmars.com...
 Hi!

 For example, we have the following methods: (Just example - might not be 
 useful ;P)
 void calc_sum( ... ) { /* do something */ }
 void calc_dif( ... ) { /* do something */ }
 void calc( int what, ... ) {
  switch( what ) {
    case 0: calc_sum( /* here is the problem */ ); break;
    case 1: calc_dif( /* here is the problem */ ); break;
  }
 }

 How can I pass the _arguments to the next called methods?

 Thanks in advance :)

The only way I have seen it done (in Tango), is to have a helper function that does the real work which takes the vararg information as parameters. For instance: void real_calc_sum(TypeInfo[] arguments, ArgList args) { do the real calc sum here} void calc_sum(...) { real_calc_sum(_arguments, _argptr); } Then you can call real_calc_sum from your calc function, and calc_sum is callable outside calc as a variadic function. -Steve
Jul 04 2008