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

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

From: "Edwards, Waverly" <Waverly.Edwards@...>
Date: Mon, 18 Jul 2011 09:25:43 -0500
// any number of fields; all must be CF types
begin record MyRecord
dim as CFStringRef  foo, bar
end record

That is extraordinarily interesting...

I would have thought that you would need to create a CFData, then fill it with " MyRecord".  To read it back out you would have to read the contents of the CFData back into a structure.  I'm sure I've seen Bernie use CFData or CFDictionary to do the same.  This way seems more light-weight ( going from memory), if you can work with all your struct members being a CF type.  Hopefully I'll get to try this out soon.

Thanks,


W.

-----Original Message-----
From: futurebasic@... [mailto:futurebasic@...] On Behalf Of Robert Purves
Sent: Monday, July 18, 2011 7:29 AM
To: futurebasic@...
Subject: Re: [FB] Dynamic array of CoreFoundation types


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/