summaryrefslogtreecommitdiffstats
path: root/src/libs/kdtools/kdgenericfactory.cpp
blob: 683b18876fbebf3222bb6d5fd80920e7cfdca7f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************************
**
** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB)
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "kdgenericfactory.h"

/*!
   \inmodule kdupdater
   \class KDGenericFactory
   \brief 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 \c T_Product. The user of the
   factory registers those producible classes in the factory by using an identifier
   \c 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 \c T_Map template parameter. It defaults to QHash.

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

    \table
        \header
            \li Method
            \li Description
        \row
            \li \c {const_iterator}
            \li A type that provides a random-access iterator that can read a const element and
                when dereferenced has type \c {const &FactoryFunction}.
        \row
            \li \c {insert(T_Identifier, FactoryFunction)}
            \li A function that inserts a new item with \c T_Identifier as key and \c FactoryFunction
                as value. If there is already an item with \c T_Identifier as key, that item's value
                must be replaced with \c FactoryFunction.
        \row
            \li \c {find(T_Identifier)}
            \li A function returning an iterator pointing to the item with the key \c T_Identifier.

        \row
            \li \c {end()}
            \li A function returning an iterator pointing to the imaginary item after the last item.

        \row
            \li \c {size()}
            \li A function returning the number of items.
        \row
            \li \c {remove(T_Identifier)}
            \li A function removing all items that have the key \c T_Identifier.
        \row
            \li \c {keys()}
            \li A function returning a list of all the keys \c T_Identifier in an arbitrary order.
    \endtable

   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 \c {std::pair<const T_Identifier, FactoryFunction>} instead of just
   \c FactoryFunction.

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

    \code

    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" );
    }
    \endcode
*/

/*!
   \fn KDGenericFactory::~KDGenericFactory()

   Destructor.
*/

/*!
    \typedef KDGenericFactory::FactoryFunction

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

/*!
   \fn KDGenericFactory::registerProduct( const T_Identifier& name )

   Registers a product of type T, identified by \a 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.
*/

/*!
   \fn KDGenericFactory::create( const T_Identifier& name ) const

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