-
Notifications
You must be signed in to change notification settings - Fork 0
Toll free bridging
Parent page: Design, CoreFoundation
##Design
- Our design example here is based on CFArray type.
- In CFArray.h typedef struct __CFArray* CFArrayRef
- In CFArray.c struct __CFArray { }
- __CFArray struct is empty, as it is a placeholder opaque type which will be replaced in runtime by GSArray or any of its subclasses.
- GSArray is defined in Foundation/GSPrivate.h
- _callBacks variable will be added to GSArray class @interface GSArray : NSArray { @public id *_contents_array; unsigned _count; const CFArrayCallBacks *_callBacks; } @end
- CFTypeRef is placeholder opaque type which will be replaced by NSObject.
##Toll Free Bridging in CFArray From CFArray to NSArray : When pointing to a CFArrayRef using NSArray* like CFArrayRef arr = CFArrayCreate( .... ); NSArray* nsArr = arr;
the NSArray pointer points to an object of type NSCFArray which is implemented in NSCFArray.m in Foundation. This was already implemented in GNUStep to delegate every NSArray function to it's alternative in CFArray like - (NSUInteger) count { return (NSUInteger)CFArrayGetCount (self); }
##To make this array fast enumerable (can be used in for each) we implement NSFastEnumeration protocol method countByEnumeratingWithState: objects: count: and the objectEnumerator method. We implement a custom enumerator called NSCFArrayEnumerator. It implements NSEnumerator with a method initWithArray: and NSEnumerator method nextObject. From NSArray to CFArray: NSArray* arr = [ [ NSArray alloc ] init ]; CFArrayRef cfArr = arr;
Here when a CFArray method is called, the macro CF_OBJC_FUNCDISPATCH0 checks if the passed object is NS object, If so it sends it the right message that is mapped to the function called. like CFIndex CFArrayGetCount (CFArrayRef array) { //this macro checks if array is NS? If yes it send the message //count to it and return the result. CF_OBJC_FUNCDISPATCH0(_kCFArrayTypeID, CFIndex, array, "count"); //If it is not NS this will be executed to return CFArray count return array->_count; }
##Toll Free Bridging in CFDictionary It's the same as the CFArray, Except that we had to implement NSCFDictionary from scratch and add all the functionality to it.
##See also
##External links