Reputation: 21
My application is interfaced with the CoreFoundation library. Some functions of another library returns a core foundation object and I need to identify the kind of object in order to process the data. Now, looking on the CFType library reference, Apple clearly states the following: "Because the value for a type ID can change from release to release, your code should not rely on stored or hard-coded type IDs nor should it hard-code any observed properties of a type ID (such as, for example, it being a small integer)."
Based on that, I have to avoid any enum (CFArray = 18, CFBoolean = 21
and so on).
The only thing that should work and be immune to changes of new releases is something like:
int ID = CFGetTypeID(obj);
if ID = CFBooleanGetTypeID()
then...
if ID = CFStringGetTypeID()
then...
if ID = CFDataGetTypeID()
then..
and so on...
This is really something horrible. Lots of calls only to identify an object.
Apple also recommends to not create dependencies on the content or format of the information returned from CFCopyTypeIDDescription and therefore I have to exclude also this option.
Anyone know how I can easily identify a returned core foundation type and why Apple always try to break existing code with the new releases ?
Upvotes: 2
Views: 2239
Reputation: 22717
In your initialization code, you could set up a static structure, maybe a dictionary or std::map, associating CFTypeID
s to function pointers or selectors. That way you'll be using CFBooleanGetTypeID()
and friends, but only calling each such function once.
Upvotes: 1
Reputation: 78393
Unfortunately you do have to compare if you don't want to risk your app breaking with future OS updates:
if( CFGetTypeID(myUnknownCFObject) == CFArrayGetTypeID() ) {
// handle the object as a CFArray
} else if( /* ... etc. ... */ ) {
} else {
// we don't know how to deal with this object
}
Upvotes: 2