Re: [Pyobjc-dev] DictType -> ObjC
Brought to you by:
ronaldoussoren
From: Bill B. <bb...@co...> - 2002-10-17 20:59:08
|
On Thursday, October 17, 2002, at 04:32 PM, Jack Jansen wrote: > On donderdag, oktober 17, 2002, at 01:12 , Bill Bumgarner wrote: >> How hard would it be to proxy DictType, ArrayType, and TupleType >> into Obj-C such that they look/feel/act/behave/are subclasses of >> NSDictionary and NSArray? >> >> That is, such that they implement the primitive methods of the >> NSDictionary/NSArray class clusters and internally use the Py* APIs >> to retrieve/set values [converting as necessary]? >> >> This would be significantly valuable in that it doesn't imply a >> conversion as the objects come across the bridge from Python->ObjC, >> yet they would be compatible with NSDictionary/NSArray and friends. > > Will these objects still be compatible with CoreFoundation? I'm not > familiar enough with how Apple made CF and NS objects interchangeable, > but if Pyobjc would handle most objects not by actually converting > them but by putting them in something that merely > "looks/feels/acts/behaves" like the real NS class that may not be good > enough. I assume that "are" is good enough, but again I'm not sure. Yes -- it is simply a matter of implementing the appropriate primitive methods for each class [for class clusters]: #import <Foundation/Foundation.h> #import <CoreFoundation/CoreFoundation.h> @interface FooArray:NSArray id stuff[100]; @end @implementation FooArray - init { [super init]; int i; for(i = 0; i < 100; i++) stuff[i] = [[NSNumber numberWithInt: i] retain]; return self; } - (id) objectAtIndex: (int) anIndex { return stuff[anIndex]; } - (unsigned) count { return 100; } @end int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; FooArray *fooArray = [[FooArray alloc] init]; NSLog(@"objectAtIndex: 76 -- %@ - 0x%x", [fooArray objectAtIndex: 76], [fooArray objectAtIndex: 76]); CFArrayRef fooRef = (CFArrayRef) fooArray; NSLog(@"CFArrayGetValueAtIndex(..., 76) -- %@ - 0x%x", CFArrayGetValueAtIndex(fooRef, 76), CFArrayGetValueAtIndex(fooRef, 76)); [pool release]; return 0; } Outputs: 2002-10-17 16:54:06.121 arraytest[23625] objectAtIndex: 76 -- 76 - 0x59a80 2002-10-17 16:54:06.122 arraytest[23625] CFArrayGetValueAtIndex(..., 76) -- 76 - 0x59a80 The same can generally be said/done with any no-cost bridged object, just that it may require more methods to be implemented. In any case, a PyArray subclass simply has to provide implementations of -objectAtIndex: and -count:. All other methods in the NSArray API are implemented against those two primitive methods. The CF "no cost" bridge is truly a "no cost" bridge -- it just calls the method IMPs directly (not sure how it looks 'em up). The source to the CF<->NS 'no cost' bridge is in the Darwin CVS repository -- CFBase, I believe. b.bum |