• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
2 issues with NSInvocation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

2 issues with NSInvocation


  • Subject: 2 issues with NSInvocation
  • From: Tim Hart <email@hidden>
  • Date: Mon, 26 Jan 2004 09:04:47 -0700

Summary:

1. How do I determine the size of an argument if I don't know the type at compile time?

2. On 10.2.8, the following behavior is observed:

typedef struct{
int a;
char b, c;
void* d;
}foo;

@interface MyClass

-( void ) setFoo:( foo ) aFoo;

@end

@implementation MyClass

-( void ) setFoo:( foo ) aFoo
{
NSLog( @"aFoo.d = %#x", aFoo );
}

@end
//code snippets...
int a;
foo myFoo;
myFoo.d = &a;

[ myClass setFoo:myFoo ];//Output is something like aFoo.d = 0xffdc4484
NSInvocation *myInvocation;
// set up invocation with method signature, selector

[ myInvocation setArgument:&myFoo atIndex:2 ];
[ myInvocation invokeWithTarget:myClass ];//Output is aFoo.d = 0xffdc0000
@end

Detail:

1). With the exception of issue 2 above, it seems that NSInvocation is able to progamatically determine the size of an argument at any particular index. I've tried objc's 'method_getArgumentInfo', but the return values seem completely wrong for sizes. It returned 2 for the size of a char, and 3 for the size of an id.

I can ( to a limited degree ) take the encoded type of the argument, and pass it to a big case statement to retrieve the size, but that leads me to 2 other issues:

1. The struct above is encoded as "{?=icc^v}". Simply summing their individual sizes is 10. sizeof( foo ) returns 12. I either have to limit my functionality to simple types ( no structs or unions ), or try to figure out at runtime all the alignment/padding issues ( which can be compile time choices ). To the best of my knowledge, determining alignment at runtime is impossible without knowing something about the data.

2. I'd rather not reinvent the wheel. Since -[ NSInvocation setArgument:atIndex ] doesn't require a size, I'm assuming that *somewhere* in the runtime there is code to determine the size of an argument.

In my implementation, I can require the user of my API to pass in argument sizes, but I'm hoping for 2 - so the user won't have to.

P.S. - yes, I realize MacOSX is up to 10.3.2 now. I will test against 10.3 when I get the chance to upgrade in a couple of weeks. I was hoping someone knew about this issue and a workaround.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: =?ISO-8859-1?Q?Re:_Disabling_Expos =E9_for_some_windows,_some_da y?= =3 F
  • Next by Date: iPhoto Library Info
  • Previous by thread: Problems with NSIntersectsRect
  • Next by thread: Re: 2 issues with NSInvocation
  • Index(es):
    • Date
    • Thread