swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Selector of method returning 'float'


From: rmb
Subject: Re: Selector of method returning 'float'
Date: Wed, 19 Feb 1997 13:02:54 -0700

> Hi,
> I want to perform this kind of operation:
> 
> float f;
> f = [obj perform: method];
> 
> where 'method' is selector of the method returning
> float. However I am unable to compile it - method
> 'perform' is declared to return id. On the run-time
> however actually returned value is float. I wonder
> if there is some standard way to do this kind of thing.

No, there isn't under standard GNU Objective C.  I believe that the Next
version of Objective C has some additional library support for creating and
executing an "Invocation" object that may support a float or double return,
but the standard perform messages simply don't do this.  Our probe machinery
does some of the same internal operations that the Next Invocation object
does, but isn't really set up for general-purpose use; it's currently only
for feeding probe objects.

> I found some solution, however it is a dirty one (and
> proves that C is powerful...). I do it in this way:
>
> *((id*) &f) = [obj perform: method];
> 
> it works, however I don't feel too comfortable doing this
> kind of thing...

This is definitely non-portable, so your feeling of discomfort is
well-justified.

There is one portable way of doing what you want, which is to look up
the method and then call it directly, but this requires more knowledge
of how Objective C calls its methods directly.  The following code should
work (though I haven't tried it):

  IMP    method;
  float  f;

  method = [[obj getClass] getMethodFor: M(messageName)];
  f = (((float(*)( id, SEL, ...))method)( obj, M(messageName) );

I said portable, not simple.  But something like this should work.
getMethodFor: on a class is the standard Swarm way of getting a method
pointer from a message selector.  A method pointer (for which IMP is
the standard predefined type) requires the object receiver as the first
argument, the message selector as its second, and any remaining arguments
in the variable argument list section following.  This code merely casts
the method pointer to return the correct type.

-Roger



> Wojtek
> 
> - -- 
> - -----------------------------------------------------------
> Wojtek Kopczuk              "There is no third way between
> address@hidden             right and wrong"
> IGS: wojtek                Friedrich von Hayek 
> http://www.econ.lsa.umich.edu/~wojtek
> - -----------------------------------------------------------
> ------- End of forwarded message -------
> 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]