高效、快速地访问CoreFoundation容器
我正在尝试使用 CoreFoundataion 容器。访问属性列表时,这似乎是一种非常方便的方法。但是,我注意到如果容器是嵌套的,访问内部容器真的很尴尬 (例如,CFArrayRef 包含一个 CFDictionaryRef,然后它有一个键,其值是另一个 CFArrayRef。也许下图演示得更好一些)。
CFArrayRef a
|----CFDictionaryRef b (assume it's at index 2 of a)
|----CFArrayRef c (assume, they key value is "array")
假设我们有一个 CFArrayRef a 我想访问 CFArrayRef c 中 CFIndex 0 处的元素,
我必须输入如下内容:
CFArrayGetValueAtIndex((CFArrayRef)CFDictionaryGetValue((CFDictionaryRef)CFArrayGetValueAtIndex(a, 2), CFSTR("array")), 0)
在我看来,需要输入大量内容。我的猜测是 CoreFoundation 基于 C 而不是 C++,因此它不提供像“[]”这样的运算符重载来访问其元素。
有没有办法让这种访问更容易打字(也许也可以阅读?)
我正在使用 C++,我正在考虑对某些 CFTypeRef(例如 CFArrayRef、CFDictionaryRef、CFStringRef)重载运算符“[]”,不确定是否这是个好主意。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所指出的,Core Foundation API 是 C API,因此没有要重载的 [] 运算符。您基本上有 3 个选择:
我会说坚持1。
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.