|
From: | Maxthon Chan |
Subject: | Re: __NSCF** classes and libobjc2 dependency on C++ runtime |
Date: | Wed, 05 Jun 2013 03:29:48 +0800 |
Well my idea was to boil down everything into a single Objective-C protocol: @protocol GSTollFreeBridging <NSCopying, NSObject> Some points when using this model: 1) All CoreFoundation objects, no matter if it is toll-free bridgeable or not, is bridged to a class. Some of the bridged classes are subclass of a Foundation class and hence is public, while some other does not have that public interface hence documented as non-toll-free bridgeable. 2) CoreFoundation itself have no function on destructing objects. All object destruction is done in this companion class's -(void)dealloc method. This is due to reference counting is done via NSObject protocol methods -(id)retain and -(void)release, which may be overridden on CoreFoundation objects. 3) If a class is toll-free bridgeable, its -(id)copy method should return CoreFoundation objects. This should be enforced in the base class. 4) If a class if toll-free bridgeable, its CoreFoundation counterpart must implement functions that mirror the behavior of the must-override methods, and decide if the call should be dispatched into Objective-C code. Meanwhile, its companion class should implement all must-override methods using these CoreFoundation functions. An example: // CoreFoundation object typedef __CFFoo { Class isa; // ... } *CFFooRef // Its public abstract class @interface NSFoo : NSObject <NSCopying> - (NSData *)data; - (void)doSonething:(NSData *)data // a must-override @end // CF initializer CFFooRef CFFooCreate(CFAllocator alloc, CFDataRef data); // Mirroring CF function void CFFooDoSomething(CFFooRef foo, CFDataRef data); // This is private. @interface NSFoo (GSBridging) <GSTollFreeBridging> @end @implementation NSFoo - (id)copyWithZone:(NSZone *)zone { // The bridging is clearly tagged using clang ARC keywords. return (__bridge id)CFFooCreate(NULL, (__bridge CFDataRef)[[self data] copy]); } @end // Private companion class @interface GSCFFoo : NSFoo @end @implementation GSCFFoo - (void)doSomething:(NSData *)data { // Methods are bridged to functions using dispatching mechanism and polymorphism existed in Objective-C CFFooDoSomething((__bridge CFFooRef)self, (__bridge CFDataRef)data); // this data is passed in directly, and CF will resolve it. } @end // The method, accepting another bridgeable object void CFFooDoSomething(CFFooRef foo, CFDataRef data) { if ([foo class] != [GSCFFoo class]) // Foundation objects should be bridged back { [(__bridge NSFoo *)foo doSomething:(__bridge NSData *)data]; } else { if ([data class] != [GSCFData class]) // Resolve the bridgeable CFData data = "" NSData *)data copy]; // Real logic goes here. } } 在 2013-6-5,上午3:00,Stefan Bidi <address@hidden> 写道:
|
[Prev in Thread] | Current Thread | [Next in Thread] |