GenericFactory Class | Qt Installer Framework Manual

- Qt Installer Framework Manual
- GenericFactory

# GenericFactory Class

template <typename BASE, typename IDENTIFIER, typename ARGUMENTS> class GenericFactory

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

Header: | #include <GenericFactory>
Inherited By: | KDUpdater::FileDownloaderFactory, KDUpdater::UpdateOperationFactory, and QInstaller::ArchiveFactory

- List of all members, including inherited members

## Public Types

| FactoryFunction

## Public Functions

virtual | ~GenericFactory()
bool | containsProduct(const IDENTIFIER &id) const
BASE * | create(const IDENTIFIER &id, ARGUMENTS... args) const
void | registerProduct(const IDENTIFIER &id)
void | registerProduct(const IDENTIFIER &id, GenericFactory::FactoryFunction func)

## Protected Functions

| GenericFactory()

## Detailed Description

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

One factory instance is able to produce instances of different types of DERIVED classes only when the constructor of DERIVED or the registered generator function have a common signature for all DERIVED classes. This signature is described by the declaration order of ARGUMENTS. It is referred to as SIGNATURE in the following paragraphs.

If a class derived from BASE does not contain a SIGNATURE matching the registered one for the constructor or the generator function, it is not possible to create instances of it using one instance of GenericFactory subclass. In that case, more than one GenericFactory subclass and instance are needed.

It is possible to register a subclass of BASE inside an instance of GenericFactory subclass using the registerProduct() function. At least one of the following conditions needs to be met:

- A global or static function with SIGNATURE exists.
- The DERIVED class has a constructor with SIGNATURE.
- The DERIVED class has a static function with SIGNATURE.

To get a new instance of DERIVED, one needs to call the create() function. The value of IDENTIFIER determines the product's subclass registered in the factory, while the values of SIGNATURE are the actual arguments passed to the class constructor or the registered generator function.

## Member Type Documentation

### GenericFactory::FactoryFunction

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

## Member Function Documentation

### `[protected]` GenericFactory::GenericFactory()

Creates the generic factory.

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

Destroys the generic factory.

### bool GenericFactory::containsProduct(const IDENTIFIER &id) const

Returns `true` if the factory contains a type with the id; otherwise returns false.

### BASE *GenericFactory::create(const IDENTIFIER &id, ARGUMENTS... args) const

Creates and returns the type identified by id with variable number of arguments args passed to the object's constructor, but automatically upcasted to BASE. Ownership of the type is transferred to the caller.

### template <typename DERIVED> void GenericFactory::registerProduct(const IDENTIFIER &id)

Registers a type DERIVED, identified by id in the factory. Any type with the same id gets unregistered.

### void GenericFactory::registerProduct(const IDENTIFIER &id, GenericFactory::FactoryFunction func)

This is an overloaded function.

Registers a function func that can create the type DERIVED, identified by id in the factory. Any type with the same id gets unregistered.