Generic factory

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.

Original Document Information

  • 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.

Document Tags and Contributors

Tags:
Contributors to this page: Yoshino, Jorend, Mgjbot
Last updated by: Yoshino,