Before you can create a CFTree object you must define its context.This means you must declare and initialize a structure of
CFTreeContext
. This structure has the following definition in
CFTree.h
:
typedef struct { CFIndex version; void *info; const void *(*retain)(const void *info); void (*release)(const void *info); CFStringRef (*copyDescription)(const void *info); } CFTreeContext;
The
info
member of this structure points to data that you define (and allocate, if necessary). The other members of the context structure (except for the version member) point to functions that take the
info
pointer as an argument and perform specific operations related to the pointed-to data, such as retaining it, releasing it, and describing it.
When you've properly initialized a
CFTreeContext
structure, call the
CFTreeCreate
function, passing in a pointer to the structure. Listing 12 gives an example of this technique.
Listing 12 Creating a CFTree object
static CFTreeRef CreateMyTree(CFAllocatorRef allocator) { MyTreeInfo *info; CFTreeContext ctx; info = CFAllocatorAllocate(allocator, sizeof(MyTreeInfo), 0); info->address = 0; info->symbol = NULL; info->countCurrent = 0; info->countTotal = 0; ctx.version = 0; ctx.info = info; ctx.retain = NULL; ctx.release = FreeTreeInfo; ctx.copyDescription = NULL; return CFTreeCreate(allocator, &ctx); }
As this example shows, you can initialize the function-pointer members of the
CFTreeContext
structure to
NULL
if you do not want to define callback functions for the CFTree object's context.