variable argument list Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
fantaz
I live to help wx-kind
I live to help wx-kind
Posts: 169
Joined: Mon Sep 13, 2004 10:07 am
Location: Croatia

variable argument list

Post by fantaz »

hi,
i know it's not strictly wx q, but i was wondering if someone could help me.
The problem is as follows:
I don't know how many arguments will be in a function. I am aware of putting the overall number of arguments in the argument list, or putting the terminator argument, but....
Is there a way to just don't know the overall number of arguments of a fn and still manage the code efficiently?
E.g.

Code: Select all

void fn(int id, ...)
{
    va_list argptr;
    int selection; 
    va_start(argptr,id);
    while( selection != -1 )
    {
               selection = va_arg( argptr, int);
		some_vector.push_back(selection);
     }
     va_end(argptr);
}
This example uses terminator argument. Is there a way to know the size of va_list in some way?

Thanx,
fantaz
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France

Post by Cursor »

A basic way is to pass the param number just before the ... argument with an integer parameter.

Code: Select all

int aFunction(int aParam, int anotherParam, unsigned nbExtraParam, ...)
This way allow you not to pass a null value at the end of extra parameters.
But without this, INMO, you cant predict the number of extra arguments.
What is little and green, witch go up and down ??
Yoda playing with the force.
fantaz
I live to help wx-kind
I live to help wx-kind
Posts: 169
Joined: Mon Sep 13, 2004 10:07 am
Location: Croatia

Post by fantaz »

Cursor wrote:A basic way is to pass the param number just before the ... argument with an integer parameter.

Code: Select all

int aFunction(int aParam, int anotherParam, unsigned nbExtraParam, ...)
This way allow you not to pass a null value at the end of extra parameters.
But without this, INMO, you cant predict the number of extra arguments.
Yeah, just as i suspected :( . It sucks, but, hey, what can one do?
Anyway thanx for the response.
Also, my appologies to forum moderators for posting this topic to C++ Development group. :oops:
General development is more adequate.
phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany

Post by phlox81 »

In general, you shouldn't use the eclipse if you don't really need to.

If you need to hand over a variabletype of arguments of the same type,
you can use a std::vector, or simply an array.
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands

Post by Jorg »

Also, my appologies to forum moderators for posting this topic to C++ Development group. Embarassed
General development is more adequate.
It's no big problem, so don't worry about it That's why we have moderators. ;-)

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
fantaz
I live to help wx-kind
I live to help wx-kind
Posts: 169
Joined: Mon Sep 13, 2004 10:07 am
Location: Croatia

Post by fantaz »

phlox81 wrote:In general, you shouldn't use the eclipse if you don't really need to.

If you need to hand over a variabletype of arguments of the same type,
you can use a std::vector, or simply an array.
Yeah, I know.
This is why I wanted to use this feature:
I read GUI from xrc. Then I have to build rules for various actions, like if checkbox is checked, the wxTextCtrl is enabled, if certain choice was made(in wxChoice) the wxTextCtrl is enabled.
So, in order to build the the most abstract rule, the easiest and the most appealing way (I am lazy and don't like typing too mutch)
I came up with :idea: was to build a funtion with variable argument list,
since if the action I am dealing with can be radio, radiobox, wxchoice, wxcombobox, wxcheck, and deal with it.
For example, if the check control is checked I don't need any arguments,
but if the choice control selection is changed, I have to know which choices are responsible for some action.
It wouldn't be a problem if there are only few controls, and I have many(almost 200 - don't ask why) :cry: