Is accessing mutable type values from the CFArrayRef a straightforward task ? No! it is not as simple as it is for non-mutable types.
Background
The CFArrayRef is the standard collection provided by the core foundation framework. This framework also provides the functions to create/query/manipulate the array. One such function has the following prototype.
const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx)
This function gives the value in the array at index idx. If,for example, theArray contains the strings as defined by the opaque structure CFStringRef . Accessing the values from theArray and storing it in CFStringRef type variable is fairly straightforward and can be done as-
CFStringRef var = reinterpret_cast(CFArrayGetValueAtIndex(theArray, index));
But the above mentioned code cannot be used if “var” variable is of type CFPluginRef or CFBundleRef. The two steps, fully C++ compliant, workaround is as shown-
Step1: “Remove the const property”
Step2: “Store the value in appropriate type”
This can be done as in the following code snippet
C++ Compliant code
void* arrayValue = const_cast (CFArrayGetValueAtIndex(theArray, index)); CFPluginRef var = reinterpret_cast< CFPluginRef > (arrayValue);
C Compliant code
CFPluginRef var = (CFPluginRef)CFArrayGetValueAtIndex(theArray, index);
The reason is that the types CFStringRef and CFPluginRef have following typedef definitions
typedef const struct __CFString * CFStringRef; typedef struct __CFPlugin * CFPluginRef;
Since CFPluginRef is a non-const type, the compiler complains when we attempt to write a statement as following:
CFPluginRef var = reinterpret_cast< CFPluginRef > (CFArrayGetValueAtIndex(theArray, index));