Prev Up Next
Go backward to Top
Go up to Top
Go forward to 2 STL Iterators Versus Pix

1 Use BaseTypeFactory instead of Virtual Constructors

In older versions of libdap (3.2 and earlier), the variables defined by your subclasses of Byte, ..., Grid were created using `virtual constructors'[1]. The code looked like:

    Byte *
    NewByte(const string &n)
    {
        return new NCByte(n);
    }

In this example, the virtual constructor returns an instance of the NCByte class, which is apparently a subclass of Byte (although I don't show that explicitly, it's implied since the NCByte pointer is returned via a Byte pointer).

The DDS parser knew to use those virtual constructors to make new instances of those types so that the instances would include the specializations you added.

This worked fine, why change it? Using the functions, only one set of specializations could be created per process. The virtual constructors have been replaced with a factory class. This enables one process to create many different set of specializations by making several different subclasses of the factory.

How it works in practice: The class BaseTypeFactory defines a factory class for the Byte, ..., Grid classes and provides a default implementation. To make a factory for your specializations, create a subclass of BaseTypeFactory. Here's an example:

    class NCTypeFactory: public BaseTypeFactory {
    public:
        NCTypeFactory() {} 
        virtual ~NCTypeFactory() {}

        virtual Byte *NewByte(const string &n = "") const;
        // more 'New' methods
    };

And the implementation:

    Byte *
    NCTypeFactory::NewByte(const string &n ) const 
    { 
        return new NCByte(n);
    }

1.1 Using the factory

It's great to have the factories, but how does the DDS class know to use it? It now has a constructor that takes a pointer to a BaseTypeFactory as a formal parameter. This determines which factory will be used to instantiate variables by DDS::parse(). It's the caller's responsibility to free the factory once the DDS instance is deleted. See the documentation for those classes for more information about the new constructors.

    DDS(BaseTypeFactory *factory, const string &n = "");

See Appendix A for the complete declaration and definition files.


James Gallagher <jgallagher@opendap.org>, 2005-11-02, Revision: 12461

Prev Up Next