nsCOMPtr versus RefPtr 编辑

Gecko code uses both nsCOMPtr and RefPtr as smart pointers. This guide provides some explanation and advice on how to choose between them.

General Rule of Thumb for nsCOMPtr versus RefPtr

The general rule of thumb is to use nsCOMPtr<T> when T is an interface type, and RefPtr<T> when T is a concrete type.

This basic rule derives from the fact that some of the nsCOMPtr<T> code is factored into the nsCOMPtr_base base class, which stores the underlying mRawPtr as an nsISupports*. (although, confusingly, debug builds don't work this way). This design saves some space in the binary (or at least it used to). Since nsCOMPtr stores the pointer as an nsISupports*, it must be possible to unambiguously cast from T* to nsISupports*. Many concrete classes inherit from nsISupports in more than one way, so they cannot be unambiguously cast to nsISupports*. Thus, these concrete classes cannot be used with nsCOMPtr.

While is possible to use nsCOMPtr<T> on concrete classes T that only singly inherit from nsISupports, it is best to avoid doing so. In the future, more base classes might be added to T that would then cause unrelated code to break, which would be very confusing. Hence, the interface versus concrete class rule of thumb: interfaces will never multiply inherit from nsISupports, so they can always use be used with nsCOMPtr without fear of breaking in the future. Concrete classes should only be used with RefPtr.

nsCOMPtr<T> also requires that you can QueryInterface to type T. It does this so that it can assert that mRawPtr is a canonical T pointer (i.e., that mRawPtr->QueryInterface(T_IID) == mRawPtr).

do_QueryInterface versus do_QueryObject

The do_QueryInterface helper is only available when assigning to nsCOMPtr. It also requires that the argument singly inherit from nsISupports (since the type of the argument is nsISupports*). For other cases, there is do_QueryObject, which is essentially a more powerful form of do_QueryInterface. It differs from do_QueryInterface as follows:

  • do_QueryObject inherits from nsCOMPtr_helper, so it can be assigned into both nsCOMPtr and RefPtr. The downside of this inheritance is that do_QueryObject requires an extra virtual call to operator() in the helper method.
  • do_QueryObject is templated on the argument type, so it's possible to pass in objects that multiply inherit from nsISupports. However, when the destination type is an XPCOM interface, it's probably better to static_cast to a class that unambiguously inherits from nsISupports and use do_QueryInterface in such cases.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:62 次

字数:3670

最后编辑:7年前

编辑次数:0 次

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