In message <l3i6e016knkrb7ncj123hkgm9j6l669mr0@4ax.com>, Bob Hairgrove
<wouldnt_you_like@to_know.com> writes[color=blue]
>On Wed, 30 Jun 2004 17:36:10 +0100, Richard Herring <junk@[127.0.0.1]>
>wrote:
>[color=green]
>>In message <9t03e0p6dr10h3t0sak5vgu201jquknef6@4ax.com>, Bob Hairgrove
>><wouldnt_you_like@to_know.com> writes[color=darkred]
>>>On 29 Jun 2004 05:22:49 -0700,
cirquitz@gmail.com (Joel) wrote:
>>>
>>>[snip]
>>>>But what I want to do is somehow to push the required parameters
>>>>(specified in a ParametersCollection object) onto the stack, before I
>>>>actually make the function call. Again, forgive me for using
>>>>non-technical words like "somehow". I have reached the extent of my
>>>>C++ knowledge in this particular area.
>>>>
>>>>And that's where I am at present. It's a bit of a lengthy post but I
>>>>wanted to let you know how I've been thinking about this.
>>>>
>>>>The problem is that I do not see right now a way to completely fulfill
>>>>the ideal that I have specified at the beginning of this post. Is
>>>>there a way that it could be accomplished?
>>>
>>>You cannot do this using only C++ ... by now, after all you have done,
>>>I think you must realize this.[/color]
>>
>>You can achieve equivalent functionality.[/color]
>
>Ahem ... we're waiting ... how do you do it in C++? (I'm not holding
>my breath too long).[/color]
In effect, you write an interpreter. Functors within the interpreter
represent functions in the interpreted language.
[color=blue][color=green][color=darkred]
>>>However, if you don't mind mixing in a
>>>bit of inline assembly code, you can set up the stack dynamically and
>>>call the function. You have to be careful which calling convention is
>>>used ... on Windows, there is __stdcall and __cdecl, for example. If
>>>your called function is declared as __stdcall, the caller won't have
>>>to clean up the stack after the function returns (otherwise: it does).[/color]
>>
>>Eeurgh. As soon as you do that, it's way out of the realm of standard
>>C++ and therefore off topic.[/color]
>
>His question was "how do I do this in C++"? Until you have shown us
>otherwise, my answer is: you can't, because using only C++, the caller
>must know at compile time how many arguments to pass and what type
>they are. The *callee*, OTOH, can declare the function like this:
>
>void SomeFunc(unsigned NumArgs, ...);
>
>or even just (...) if there is some convention (i.e. a null argument
>at the end) for knowing how many arguments to expect. The problem is
>with the caller's code which does not know about the arguments at
>compile time, but only at run time (if I understood the question
>correctly, that is).[/color]
That's right, and the OP identified this problem. So you have a class
representing a collection of arguments, populate that accordingly and
pass it to the functor representing your function. There's a separate
functor class for each function to be implemented, and each of those
knows about the arguments of its corresponding function at compile time,
and can extract and validate the contents of the arguments object.[color=blue]
>
>So what is better: to write one message saying: "You can't do this in
>C++; it's off-topic to tell you how, so go f*** yourself", or: "You
>can't do this in C++; but there is a way, and this is approximately
>what you can do on at least one platform to achieve your goal."[/color]
Neither. The answer is "you _can_ do this in C++, but you need to think
a little more laterally."[color=blue]
>[color=green]
>>But there's no reason why the "stack" on which his functions find their
>>arguments has to be the actual hardware stack, any more than those
>>"functions" have to be actual functions - they just need to be objects
>>which have some functional behaviour, and maybe also carry information
>>about the required parameters - in other words, function _objects_.[/color]
>
>Oh yes, there are all those nice classes in <functional> like
>std::bind2nd for wrapping multi-argument callback functions into one
>function object. Still, the code using these has to know about the
>arguments in advance, doesn't it?
>[/color]
Indeed; those are for solving different problems.
--
Richard Herring