Aggregating the In-Memory Datasource 编辑

 

Introduction

You can use XPCOM aggregation1 with the in-memory datasource. Why would you want to do this? Say you were writing a datasource2, and the way you chose to implement it was to "wrap" the in-memory datasource; i.e.,

MyClass : public nsIMyInterface, public nsIRDFDataSource {
private:
    nsCOMPtr<nsIRDFDataSource> mInner;

public:
    // nsIRDFDataSource methods
    NS_IMETHOD Init(const char* aURI) {
        return mInner->Init(aURI);
    }

    NS_IMETHOD GetURI(char* *aURI) {
        return mInner->GetURI(aURI);
    }

    // etc., for each method in nsIRDFDataSource!
};

Very painful, prone to errors, and fragile as the interfaces are still in flux (a wee bit). Aggregation to the rescue! Here are the gory details on how.

When It Won't Work

Although this magic is terribly convenient to use, it won't work in the case that you want to "override" some of the in-memory datasource's methods. For example, while writing the bookmarks datasource, I wanted to be able to trap Assert() to enforce the bookmarks datasource would only accept "bookmarks related" assertions. If I'd just delegated to the in-memory datasource, Assert() would've taken any old random garbage. Similarly, I wanted to trap Flush() so that I could write the bookmarks.html file back to disk.

In short, the only case where this technique is useful is when you're implementing a datasource to get "read-only reflection". That is, you want to reflect the contents of something as an RDF graph (presumably so that it can be aggregated with other information or displayed as styled content).

Technical Details

As before, have an nsCOMPtr as your delegate, but this time around,don't derive from nsIRDFDataSource. Also, instead of keeping an nsCOMPtr<nsIRDFDataSource>, you'll just want an nsCOMPtr<nsISupports>:

class MyClass : public nsIMyInterface {
    ...
private:
    nsCOMPtr<nsISupports> mInner;
};

Construct the datasource delegate when your object is constructed (or, at worst, when somebody QI's for it):

rv = nsComponentManager::CreateInstance(
        kRDFInMemoryDataSourceCID,
        this, /* the "outer" */
        nsCOMTypeInfo<nsISupports>::GetIID(),
        getter_AddRefs(mInner));

Note passing this as the "outer" parameter.

Now, if the in-memory datasource's implementation of QueryInterface() fails because it doesn't support the requested interface, it willforward the query interface to its "outer" (which is "us"). This preserves the symmetrical property of QueryInterface().

For us to preserve symmetry, our QueryInterface() implementation needs to forward nsIRDFDataSource to the delegate3:

NS_IMETHODIMP
MyClass::QueryInterface(REFNSIID aIID, void** aResult)
{
  NS_PRECONDITION(aResult != nsnull, "null ptr");
  if (! aResult)
    return NS_ERROR_NULL_POINTER;

  if (aIID.Equals(nsCOMTypeInfo<nsIMyInterface>::GetIID()) ||
      aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID())) {
    *aResult = NS_STATIC_CAST(nsIGlobalHistory*, this);
  }
  else if (aIID.Equals(nsCOMTypeInfo<nsIRDFDataSource>::GetIID())) {
    return mInner->QueryInterface(aIID, aResult);
  }
  else {
    *aResult = nsnull;
    return NS_NOINTERFACE;
  }

  NS_ADDREF(NS_STATIC_CAST(nsISupports*, aResult));
  return NS_OK;
}

The only other thing that you'll need to be aware of is that you'll need to QueryInterface() from nsISupports to nsIRDFDataSource before you can actually do anything useful with the datasource from within your object. For example:

NS_IMETHODIMP
MyClass::DoSomething()
{
  nsCOMPtr<nsIRDFDataSopurce> ds = do_QueryInterface(mInner);

  rv = ds->Assert(/* something useful here */);

  // etc...

  return NS_OK;
}

It may be tempting to keep a pointer to the aggregate's nsIRDFDataSource in a member variable, butyou can't do that. Why? Because if you did, you'd hold a circular reference that would never unwind.

Notes

  1. Describing all of the vagaries of XPCOM aggregation is beyond the scope of this document. The basic idea is to overload QueryInterface(), allowing it to return adelegate object that supports the interface. There is some trickery involved on the delegate's part to ensure that reference counting is done sanely, and that the reflexive, symmetric, and transitive properties of QueryInterface() are preserved. If you're really interested, I'd recommend reading about it in a COM book.
  2. For more information on writing a datasource, see the RDF Datasource How-To document.
  3. You could also forward other interfaces to the mInner that youknow it can support; however, this is extremely risky. It's risky because another implementation of the same object mightnot support those interfaces. Then the QueryInterface() will be forwarded back to you, and we'll recurse off to infinity (and beyond!...

Interwiki Language Links

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

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

发布评论

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

词条统计

浏览:59 次

字数:6323

最后编辑:7年前

编辑次数:0 次

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