[futurebasic] Re: [FB] Dynamic array of CoreFoundation types

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2011 : Group Archive : Group : All Groups

From: Steve <mactech@...>
Date: Mon, 18 Jul 2011 12:56:04 -0400
thanks for explaining and the demo

On Jul 18, 2011, at 7:29 AM, Robert Purves wrote:

> 
> Steve wrote:
> 
>>>> Sure do wish Dynamic Arrays could use CFStringRef
>>> And even if they do why not use CFArrays?
>> 
>> That person is me and it's because I don't know how to use CFArrays for my purpose.  I like the simplicity of dynamic arrays, in adding, deleting, and read/write to disk.
>> 
>> I have large record structures comprised of CFStringRefs that are being converted to pascal strings and placed into the Dynamic array record structures.
>> 
>> It would be great is the strings could be replaced and CFStringRef were used in their place.   Robert's example is encouraging but it is a simple array of a single CFString.
> 
> 
> 
> A CFMutableArray is intrinsically dynamic in that you can append items without limit. CFArrayRemoveAllValues() is equivalent to 'kill dynamic', although it's more common to *really* kill the array with CFRelease(), and create a new one when needed.
> 
> A solution for storing records in a CFMutableArray is nesting.
> In the demo below, each element of a CFMutableArray (mutArray in the main program) is a fixed-length immutable CFArray that represents a record MyRecord. 
> To see the nested array structure, uncomment the line:
> 'fn ConsoleCFPrint( mutArray ) : print 
> 
> Robert P.
> 
> 
> '------------------
> include "ConsoleWindow"
> 
> // for debug display
> local fn ConsoleCFPrint( typeRef as CFTypeRef )
> '~'1
> dim as CFStringRef   oldText, newText
> 
> oldText = fn HIViewCopyText( sConsoleHITextView )
> newText = fn CFStringCreateWithFormat( _kCFAllocatorDefault, #0, @"%@%@\r", oldText, typeRef )
> CFRelease( oldText )
> fn HIViewSetText( sConsoleHITextView, newText )
> CFRelease( newText )
> end fn
> 
> 
> // any number of fields; all must be CF types
> begin record MyRecord
> dim as CFStringRef  foo, bar
> end record
> 
> _nFieldsInMyRecord = sizeof( MyRecord )/sizeof( CFTypeRef )
> 
> 
> local fn CFArrayAppendMyRecord( iAry as CFMutableArrayRef, iRec as ^MyRecord )
> '~'1
> dim as CFArrayRef  recAry
> 
> recAry = fn CFArrayCreate( _kCFAllocatorDefault, (ptr)iRec, _nFieldsInMyRecord, kCFTypeArrayCallBacks )
> CFArrayAppendValue( iAry, recAry )
> end fn
> 
> 
> local fn CFArrayGetMyRecordAtIndex( iAry as CFMutableArrayRef, idx as CFIndex, oRec as ^MyRecord )
> '~'1
> dim as CFArrayRef  recAry
> 
> recAry = fn CFArrayGetValueAtIndex( iAry, idx )
> CFArrayGetValues( recAry, fn CFRangeMake( 0, _nFieldsInMyRecord ), (ptr)oRec )
> end fn
> 
> 
> // demo main
> dim as CFMutableArrayRef  mutArray
> dim as MyRecord           theRecord
> 
> mutArray = fn CFArrayCreateMutable( _kCFAllocatorDefault, 0, kCFTypeArrayCallBacks )
> 
> // append an item
> theRecord.foo = @"alpha"
> theRecord.bar = @"beta"
> fn CFArrayAppendMyRecord( mutArray, @theRecord )
> // don't release the string literals
> 
> // append another item
> theRecord.foo = fn CFStringCreateCopy( _kCFAllocatorDefault, @"gamma" )
> theRecord.bar = fn CFStringCreateCopy( _kCFAllocatorDefault, @"delta" )
> fn CFArrayAppendMyRecord( mutArray, @theRecord )
> CFRelease( theRecord.foo ) // release the created/copied string
> CFRelease( theRecord.bar ) // release the created/copied string
> 
> //...
> 'fn ConsoleCFPrint( mutArray ) : print 
> 
> // retrieve and display items
> fn CFArrayGetMyRecordAtIndex( mutArray, 0, @theRecord )
> fn ConsoleCFPrint( theRecord.foo )
> fn ConsoleCFPrint( theRecord.bar )
> 
> fn CFArrayGetMyRecordAtIndex( mutArray, 1, @theRecord )
> fn ConsoleCFPrint( theRecord.foo )
> fn ConsoleCFPrint( theRecord.bar )
> //...
> 
> // done with array
> CFRelease( mutArray ) 
> // CFArrayRemoveAllValues() might be appropriate instead
> '------------------
> 
> 
> 
> --
> To unsubscribe, send ANY message to: futurebasic-unsubscribe@...
> To access the list archives, go to:  http://freegroups.net/groups/futurebasic/
>