KDGenericFactory Class | Qt Installer Framework Manual

Qt Installer Framework Manual | KDGenericFactory

# KDGenericFactory Class

The KDGenericFactory class implements a template-based generic factory. More...

Header: | #include <KDGenericFactory>
Inherited By: | KDUpdater::FileDownloaderFactory and KDUpdater::UpdateOperationFactory

- List of all members, including inherited members

## Public Types

typedef | FactoryFunction
typedef | FactoryFunctionWithArg

## Public Functions

virtual | ~KDGenericFactory()
T_Product * | create(const T_Identifier &name) const
T_Product * | createWithArg(const T_Identifier &name, const T_Argument &arg) const
void | registerProduct(const T_Identifier &name)
void | registerProductWithArg(const T_Identifier &name)

## Detailed Description

The KDGenericFactory class implements a template-based generic factory.

KDGenericFactory is an implementation of the factory pattern. It can be used to produce instances of different classes having a common superclass `T_Product`. The user of the factory registers those producible classes in the factory by using an identifier `T_Identifier`. That identifier can then be used to produce as many instances of the registered product as the user wants.

The advanced user can even choose the type of the class the factory is using to store its FactoryFunctions by passing a `T_Map` template parameter. It defaults to QHash.

KDGenericFactory expects the storage container to be a template class accepting `T_Identifier` and `FactoryFunction` as parameters. Additionally it needs to provide:

Method | Description
--- | ---
`const_iterator` | A type that provides a random-access iterator that can read a const element and when dereferenced has type `const &FactoryFunction`.
`insert(T_Identifier, FactoryFunction)` | A function that inserts a new item with `T_Identifier` as key and `FactoryFunction` as value. If there is already an item with `T_Identifier` as key, that item's value must be replaced with `FactoryFunction`.
`find(T_Identifier)` | A function returning an iterator pointing to the item with the key `T_Identifier`.
`end()` | A function returning an iterator pointing to the imaginary item after the last item.
`size()` | A function returning the number of items.
`remove(T_Identifier)` | A function removing all items that have the key `T_Identifier`.
`keys()` | A function returning a list of all the keys `T_Identifier` in an arbitrary order.

The only two class templates that currently match this concept are QHash and QMap. QMultiHash and QMultiMap do not work, because they violate the requirement on insert() above and std::map and std::unordered_map do not match, because they do not have keys() and, because a dereferenced iterator has type `std::pair<const T_Identifier, FactoryFunction>` instead of just `FactoryFunction`.

The following example shows how the general use case of KDGenericFactory looks like:

      class Fruit
      {
      };

      class Apple : public Fruit
      {
      };

      class Pear : public Fruit
      {
      };

      int main()
      {
          // creates a common fruit "factory"
          KDGenericFactory< Fruit > fruitPlantation;
          // registers the product "Apple"
          fruitPlantation.registerProduct< Apple >( "Apple" );
          // registers the product "Pear"
          fruitPlantation.registerProduct< Pear >( "Pear" );

          // lets create some stuff - here comes our tasty apple:
          Fruit* myApple = fruitPlantation.create( "Apple" );

          // and a pear, please:
          Fruit* myPear = fruitPlantation.create( "Pear" );

          // ohh - that doesn't work, returns a null pointer:
          Fruit* myCherry = fruitPlantation.create( "Cherry" );
      }

## Member Type Documentation

### typedef KDGenericFactory::FactoryFunction

This typedef defines a factory function producing an object of type T_Product.

### typedef KDGenericFactory::FactoryFunctionWithArg

This typedef defines a factory function producing an object of type T_Product with the arguments specified by arg.

## Member Function Documentation

### `[virtual]` KDGenericFactory::~KDGenericFactory()

Destroys the generic factory.

### T_Product *KDGenericFactory::create(const T_Identifier &name) const

Creates and returns a product of the type T identified by name. Ownership of the product is transferred to the caller.

### T_Product *KDGenericFactory::createWithArg(const T_Identifier &name, const T_Argument &arg) const

Creates and returns a product of the type T identified by name with the arguments specified by arg. Ownership of the product is transferred to the caller.

### void KDGenericFactory::registerProduct(const T_Identifier &name)

Registers a product of the type T, identified by name in the factory. Any type with the same name gets unregistered.

If a product was registered via this method, it will be created using its default constructor.

### void KDGenericFactory::registerProductWithArg(const T_Identifier &name)

Registers a product of the type T, identified by name, with arguments. Any type with the same name gets unregistered.

If a product was registered via this method, it will be created using its default constructor.