Generic factory 编辑

Original Document Information

In XPCOM, a generic factory is a factory created using the facilities in xpcom/glue/nsIGenericFactory.h.

Summary

Most XPCOM factories can be very simple. Rick Potts wrote a templated-based generic factory (nsFactory<T>) that simplifies the factory creation process that just requires writing a CreateInstance() method. The new nsIGenericFactory interface takes this a step further, by providing a single interface that can be reused anytime a simple implementation of nsIFactory is needed. Here is the interface, and a description of its use.

/**
 * Provides a Generic nsIFactory implementation that can be used by
 * DLLs with very simple factory needs.
 */
class nsIGenericFactory : public nsIFactory {
public:
    static const nsIID& IID() { static nsIID iid = NS_IGENERICFACTORY_IID; return iid; }

    typedef NS_CALLBACK(ConstructorProcPtr) (nsISupports *aOuter, REFNSIID aIID, void **aResult);

    /**
     * Establishes the generic factory's constructor function, which will be called
     * by CreateInstance.
     */
    NS_IMETHOD SetConstructor(ConstructorProcPtr constructor) = 0;
};

Using nsIGenericFactory is simple. Create a new instance from the repository with a CID of NS_GENERICFACTORY_CID, and and IID of NS_IGENERICFACTORY_IID. Define a constructor function that matches the ConstructorProcPtr prototype, and call nsIGenericFactory::SetConstructor with a pointer to that function. You're done. You now have a fully functional factory object.

Examples

class nsIComponent : public nsISupports {
public:
  NS_IMETHOD DoSomething() = 0;
};

class MyComponent : public nsIComponent {
public:
  MyComponent();
  virtual ~MyComponent();

  static NS_METHOD Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);

  NS_IMPL_ISUPPORTS

  NS_IMETHOD DoSomething();
};

To create a factory for this class, simply write the following:

nsIFactory* NewComponentFactory(nsIRepository* repository)
{
    nsIGenericFactory* factory = NULL;
    nsCID kGenericFactoryCID = NS_GENERICFACTORY_CID;
    nsresult res = repository->CreateInstance(kGenericFactoryCID, NULL, nsIGenericFactory::IID(), &factory);
    if (res == NS_OK) {
        factory->SetConstructor(&MyComponent::Create);
    }
    return factory;
}

This example assumes that the XPCOM repository is available as an interface (which it soon will be).

Background

(This is based on my original news posting <beard-2402991733140001@h-198-93-95-151.mcom.com>.)

We seem to be creating a huge number of different factory implementations. It seems to me that we can cut down on code size (all those QueryInterface, AddRef, Release implementations) if we just use the following class for all of the simple factories:

// Idea:  Why not create a generic factory facility so we can avoid
 // duplication of so much nsIFactory code? All we need is an allocator
 // function, the rest of the implementation is exactly the same.

 #include "nsIFactory.h"

 class nsGenericFactory : public nsIFactory {
 public:
    typedef nsresult (*CreatorProcPtr) (nsISupports *aOuter,
                                        REFNSIID aIID, void **aResult);

    nsGenericFactory(CreatorProcPtr creator);
    virtual ~nsGenericFactory();

    NS_DECL_ISUPPORTS

    NS_IMETHOD CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult);

    NS_IMETHOD LockFactory(PRBool aLock);

 private:
    CreatorProcPtr mCreator;
 };

 nsGenericFactory::nsGenericFactory(CreatorProcPtr creator)
    :  mCreator(creator)
 {
    NS_INIT_REFCNT();
 }

 nsGenericFactory::~nsGenericFactory() {}

 static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);

 NS_IMPL_ISUPPORTS(nsGenericFactory, kIFactoryIID)

 NS_IMETHODIMP nsGenericFactory::CreateInstance(nsISupports *aOuter,
                                                REFNSIID aIID, void **aResult)
 {
    return mCreator(aOuter, aIID, aResult);
 }

 NS_IMETHODIMP nsGenericFactory::LockFactory(PRBool aLock)
 {
    return NS_OK;
 }

Many of our classes already have a static entry point that serves as the creator function, so in most cases, creating a new factory for a class is just:

nsIFactory* NewMallocFactory()
{
   nsIFactory* factory = new nsGenericFactory(&nsMalloc::Create);
   factory->AddRef();
   return factory;
}

Talking to Warren, he suggests that we even provide a shorthand for this, we should be able to register a factory with just a function pointer.

  • Author: Patrick Beard
  • Last Updated Date: February 26, 1999
  • Copyright Information: Portions of this content are © 1998–2007 by individual mozilla.org contributors; content available under a Creative Commons license | Details.

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

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

发布评论

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

词条统计

浏览:62 次

字数:5698

最后编辑:7年前

编辑次数:0 次

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