Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Objective-C)  »  runtime arguments for variadic functions e.g...
Page 1 of 1    

runtime arguments for variadic functions e.g...

Author Message
Greg Parker...
Posted: Thu Jun 25, 2009 5:17 am
Guest
Lambda-c <bainomugisha at (no spam) gmail.com> writes:
Quote:
I want to make calls to objc_msgSend with arguments to the selector
constructed at runtime
e.g

void func_call(void rec, void sel, args, ...){

objc_msgSend((id)receiver, (SEL)sel, ?????);
}

The challenge is that I want to populate the sel and args at runtime.
What tweaks do I need to do generate calls to objc_msgSend???

If your call always takes the same number and type of arguments,
then you should cast objc_msgSend to an appropriate function
pointer type and call that.

If you do not know at compile time the argument count and types,
then the simplest solution is to use NSInvocation or libffi to
build your argument list and call the function.

(In C, there is no way to pass parameters directly from `...`
to `...`. That's why functions like vprintf() exist, identical to
printf() except using `va_list` instead of `...`. But there is no
va_list version of objc_msgSend(), so you can't do it that way.)


--
Greg Parker gparker at (no spam) apple.com Runtime Wrangler
 
Lambda-c...
Posted: Fri Jun 26, 2009 8:15 am
Guest
On Jun 25, 4:06 am, Greg Parker <gpar... at (no spam) apple.com> wrote:
Quote:
Lambda-c <bainomugi... at (no spam) gmail.com> writes:
I want to make calls to objc_msgSend with arguments to the selector
constructed at runtime
e.g

void func_call(void rec, void sel, args, ...){

  objc_msgSend((id)receiver, (SEL)sel,  ?????);
}

The challenge is that I want to populate the sel and args at runtime.
What tweaks do I need to do generate calls to objc_msgSend???

If your call always takes the same number and type of arguments,
then you should cast objc_msgSend to an appropriate function
pointer type and call that.

If you do not know at compile time the argument count and types,
then the simplest solution is to use NSInvocation or libffi to
build your argument list and call the function.

(In C, there is no way to pass parameters directly from `...`
to `...`. That's why functions like vprintf() exist, identical to
printf() except using `va_list` instead of `...`. But there is no
va_list version of objc_msgSend(), so you can't do it that way.)

Thanks Greg. I can go away with passing args from '...' to '...' I
have a situation where the receiver, selector and arguments
information is available at runtime. What I really want is how I can
generate calls such as-
objc_msgSend(obj, sel, args[0])
objc_msgSend(obj, sel, args[0], args[2], args[3])
objc_msgSend(obj, sel, args[0], args[2])
......
In the above case given the obj, sel and an array of arguments I can
generate objc_msgSend depending on arguments count in the array.


Quote:

--
Greg Parker     gpar... at (no spam) apple.com     Runtime Wrangler

T
 
Lambda-c...
Posted: Fri Jun 26, 2009 3:59 pm
Guest
On Jun 26, 5:40 pm, David Phillip Oster <os... at (no spam) ieee.org> wrote:
Quote:
In article
3573e313-0f1c-4551-819d-15f1c1199... at (no spam) v4g2000vba.googlegroups.com>,



 Lambda-c <bainomugi... at (no spam) gmail.com> wrote:
On Jun 25, 4:06 am, Greg Parker <gpar... at (no spam) apple.com> wrote:
Lambda-c <bainomugi... at (no spam) gmail.com> writes:
I want to make calls to objc_msgSend with arguments to the selector
constructed at runtime
e.g

void func_call(void rec, void sel, args, ...){

  objc_msgSend((id)receiver, (SEL)sel,  ?????);
}

The challenge is that I want to populate the sel and args at runtime.
What tweaks do I need to do generate calls to objc_msgSend???

If your call always takes the same number and type of arguments,
then you should cast objc_msgSend to an appropriate function
pointer type and call that.

If you do not know at compile time the argument count and types,
then the simplest solution is to use NSInvocation or libffi to
build your argument list and call the function.

(In C, there is no way to pass parameters directly from `...`
to `...`. That's why functions like vprintf() exist, identical to
printf() except using `va_list` instead of `...`. But there is no
va_list version of objc_msgSend(), so you can't do it that way.)

Thanks Greg. I can go away with passing args from '...' to '...' I
have a situation where the receiver, selector and arguments
information is available at runtime. What I really want is how I can
generate calls such as-
 objc_msgSend(obj, sel, args[0])
 objc_msgSend(obj, sel, args[0], args[2], args[3])
 objc_msgSend(obj, sel, args[0], args[2])
.....
In the above case given the obj, sel and an array of arguments I can
generate objc_msgSend depending on arguments count in the array.

Greg already gave you two correct answers to that question:

1) Use NSInvocation - somewhat painful and somewhat slow.

2) Use libffi - even more painful but fast.

Thanks David. NSInvocation seems to do the job.
 
David Phillip Oster...
Posted: Fri Jun 26, 2009 7:40 pm
Guest
In article
<3573e313-0f1c-4551-819d-15f1c1199991 at (no spam) v4g2000vba.googlegroups.com>,
Lambda-c <bainomugisha at (no spam) gmail.com> wrote:

Quote:
On Jun 25, 4:06 am, Greg Parker <gpar... at (no spam) apple.com> wrote:
Lambda-c <bainomugi... at (no spam) gmail.com> writes:
I want to make calls to objc_msgSend with arguments to the selector
constructed at runtime
e.g

void func_call(void rec, void sel, args, ...){

  objc_msgSend((id)receiver, (SEL)sel,  ?????);
}

The challenge is that I want to populate the sel and args at runtime.
What tweaks do I need to do generate calls to objc_msgSend???

If your call always takes the same number and type of arguments,
then you should cast objc_msgSend to an appropriate function
pointer type and call that.

If you do not know at compile time the argument count and types,
then the simplest solution is to use NSInvocation or libffi to
build your argument list and call the function.

(In C, there is no way to pass parameters directly from `...`
to `...`. That's why functions like vprintf() exist, identical to
printf() except using `va_list` instead of `...`. But there is no
va_list version of objc_msgSend(), so you can't do it that way.)

Thanks Greg. I can go away with passing args from '...' to '...' I
have a situation where the receiver, selector and arguments
information is available at runtime. What I really want is how I can
generate calls such as-
objc_msgSend(obj, sel, args[0])
objc_msgSend(obj, sel, args[0], args[2], args[3])
objc_msgSend(obj, sel, args[0], args[2])
.....
In the above case given the obj, sel and an array of arguments I can
generate objc_msgSend depending on arguments count in the array.


Greg already gave you two correct answers to that question:

1) Use NSInvocation - somewhat painful and somewhat slow.

2) Use libffi - even more painful but fast.
 
 
Page 1 of 1    
All times are GMT
The time now is Mon Nov 30, 2009 2:00 am