I am trying to use CoreFoundataion containers. It seems to be a really convenient way when accessing property list. However, I notice that it’s really awkward to access internal containers if the container is nested
(for example, A CFArrayRef contains a CFDictionaryRef, which then has a key whose value is another CFArrayRef. Maybe a graph below demonstrate a little better).
CFArrayRef a
|----CFDictionaryRef b (assume it's at index 2 of a)
|----CFArrayRef c (assume, they key value is "array")
So let’s say we got an CFArrayRef a
And I want to access the element at CFIndex 0 in CFArrayRef c
I have to type something like this:
CFArrayGetValueAtIndex((CFArrayRef)CFDictionaryGetValue((CFDictionaryRef)CFArrayGetValueAtIndex(a, 2), CFSTR("array")), 0)
That looks to me is a lot of typing. My guess is that CoreFoundation is based on C rather than C++, so it doesn’t provide operator overloading like “[]” to access its element.
Is there a way to make this access easier on typing (maybe for reading as well?)
I am using C++, i am considering overload the operator “[]” for certain CFTypeRef (such as CFArrayRef, CFDictionaryRef, CFStringRef), not sure if it’s a good idea.
As you noted the Core Foundation API is a C API, so there is no [] operator to overload. You basically have 3 choices:
I’d say stick with 1.