将基本 CFTypeRef 转换为更具体的 CoreFoundation 类型

发布于 2024-12-04 15:48:25 字数 743 浏览 4 评论 0原文

有没有一种简单的方法可以将 CTypeRef 转换为特定的 CoreFoundation 类型?我不想将内联转换为 (CFStringRef)myObjectRef 但想创建一个辅助方法来为我的所有 CoreFoundation 类型执行此操作。

我知道可以使用像 CFGetTypeID(myObjectRef) == CFStringGetTypeID() 之类的东西来确定 CTypeRef 是否是 CFString。然而,创建单个方法来执行此操作可能会变得非常冗长并且有很多 if 语句。

使用针对 CFGetTypeID() 的大量 if 语句构建方法是唯一的方法吗?或者有更简单的方法来做到这一点吗?

更新:举个例子,

我想创建一个辅助函数来处理一些我无法更改的遗留代码。目前,它生成 CFDictionaryRefCFStringRefCFURLRef 之一作为作为 CTypeRef 提供的返回值。我目前正在通过对返回值运行 CFGetTypeID() 来解决此问题,但这并不理想。我宁愿有一个 CastToCF() 帮助器来帮我处理这个问题,而不是到处都有 C 风格的强制转换。这也将有助于使未来的测试变得更加容易。

PS 我不担心可变类型。

Is there a simple way to convert a CTypeRef to a specific CoreFoundation type? I'm not looking to cast inline as (CFStringRef)myObjectRef but would like to create a helper method to do this for me for all CoreFoundation types.

I know it's possible to use something like CFGetTypeID(myObjectRef) == CFStringGetTypeID() to find out whether a CTypeRef is a CFString. However creating a single method to do this could become very verbose and have a lot of if statements.

Is building out a method with a bunch of if statements against CFGetTypeID() the only way? Or is there a simpler way to do this?

UPDATE: with example

I'd like to make a helper function to work with some legacy code I can't change. Currently it produces one of CFDictionaryRef, CFStringRef or CFURLRef as a return value provided as a CTypeRef. I'm currently working around this by running CFGetTypeID() on the returned value but this isn't ideal. Rather than having C-style casts all over the place, I'd rather have a CastToCF() helper which handles this for me. This would help make testing easier in the future as well.

P.S. I'm not worried about mutable types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-11 15:48:25

这样做没有明显的意义。 ac 风格的转换与其他语言不同 - 它是一种类型转换,左侧的地址与右侧的地址相同。如果您进行了错误的转换,cftypes 不会抛出或返回 null(与其他语言不同)。 iow,它只是您指定类型的装饰,并且 ac 编译器将假定您的强制转换是有效的。

或者也许您可以提供一个更好的示例来说明如何使用它(如果这没有帮助)。

更新

好的。既然您将其标记为 objc++,我只需创建一个辅助类,该类具有大量诊断功能并执行所有嘈杂的转换(最小说明):

class t_helper {
public:
    t_helper(CFTypeRef cf) : d_cf(cf), d_type(CFGetTypeID(cf)) { assert(this->d_cf); }
    ~t_helper() {}

    /* type info */
    bool isString() const { return CFStringGetTypeID() == this->type(); }
    CFStringRef string() { assert(this->isString()); return this->cf_cast<CFStringRef>(); }

    bool isDictionary() const { return CFDictionaryGetTypeID() == this->type(); }
    CFDictionaryRef dictionary() { assert(this->isDictionary()); return this->cf_cast<CFDictionaryRef>(); }

 ...

    /* and a trivial example of an operation */
    void appendMutableCopyToArray(CFMutableArrayRef array) {
        if (this->isString()) {
            CFMutableStringRef cp(CFStringCreateMutableCopy(0,0,this->string()));
            CFArrayAppendValue(array, cp);
            CFRelease(cp);
        }
        ...
    }

 ...

private:
    template < typename T > T cf_cast() { return reinterpret_cast<T>(this->d_cf); }
    const CFTypeID type() const { return this->d_type; }
private:
    CFTypeRef d_cf;
    const CFTypeID d_type;
};

这大约是我在没有您正在处理的程序的真正具体示例的情况下所能得到的最准确的结果和。

there's no obvious point in doing this. a c style cast is not like other languages - it is a typecast which the address on the left will be identical to the address on the right. cftypes will not throw or return null if you do a bad cast (unlike other languages). iow, it's merely a decoration for you to specify a type, and a c compiler will assume your cast is valid.

or perhaps you can provide a better example of how you would use this, if that did not help.

Update

ok. since you tagged it objc++, i'd just create a helper class which had plenty of diagnostics and did all the noisy conversions (minimal illustration):

class t_helper {
public:
    t_helper(CFTypeRef cf) : d_cf(cf), d_type(CFGetTypeID(cf)) { assert(this->d_cf); }
    ~t_helper() {}

    /* type info */
    bool isString() const { return CFStringGetTypeID() == this->type(); }
    CFStringRef string() { assert(this->isString()); return this->cf_cast<CFStringRef>(); }

    bool isDictionary() const { return CFDictionaryGetTypeID() == this->type(); }
    CFDictionaryRef dictionary() { assert(this->isDictionary()); return this->cf_cast<CFDictionaryRef>(); }

 ...

    /* and a trivial example of an operation */
    void appendMutableCopyToArray(CFMutableArrayRef array) {
        if (this->isString()) {
            CFMutableStringRef cp(CFStringCreateMutableCopy(0,0,this->string()));
            CFArrayAppendValue(array, cp);
            CFRelease(cp);
        }
        ...
    }

 ...

private:
    template < typename T > T cf_cast() { return reinterpret_cast<T>(this->d_cf); }
    const CFTypeID type() const { return this->d_type; }
private:
    CFTypeRef d_cf;
    const CFTypeID d_type;
};

that's about as accurate as i can get get without a really specific example of the program you are dealing with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文