1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
25 * @brief A generic object factory.
26 * @author James Hogan <james@albanarts.com>
29 #include "functionInfo.h"
33 /** A generic object factory.
34 * Types can be registered to this class and then constructed using a key.
36 template <typename KEY
, typename BASE
, typename TUPLE
>
42 * Constructors + destructor
45 /// Default constructor.
54 foreach (BaseConstructor
* constructor
, m_constructors
)
64 /** Add a type to the factory.
65 * @param T Type to add.
66 * @param key Identifier used to construct.
69 bool addType(const KEY
& key
)
71 typename
ConstructorHash::const_iterator it
= m_constructors
.constFind(key
);
72 if (it
== m_constructors
.constEnd())
74 m_constructors
[key
] = new Constructor
<T
>();
84 BASE
* construct(KEY key
)
86 return constructFromPack(key
, TUPLE::pack());
88 template <typename Param1
>
89 BASE
* construct(KEY key
, const Param1
& param1
)
91 return constructFromPack(key
, TUPLE::pack(param1
));
93 template <typename Param1
, typename Param2
>
94 BASE
* construct(KEY key
, const Param1
& param1
, const Param2
& param2
)
96 return constructFromPack(key
, TUPLE::pack(param1
, param2
));
99 /** Construct a type using a pack of arguments.
100 * @param key identifier of type to construct.
101 * @param pack pack of arguments.
103 BASE
* constructFromPack(KEY key
, const typename
TUPLE::Pack
& pack
)
105 typename
ConstructorHash::const_iterator it
= m_constructors
.constFind(key
);
106 if (it
!= m_constructors
.constEnd())
108 return (*it
)->construct(pack
);
122 /// Base constructor class.
123 class BaseConstructor
126 virtual ~BaseConstructor() {}
127 /// Construct an instance of a dervied type.
128 virtual BASE
* construct(const typename
TUPLE::Pack
& pack
) const = 0;
131 /** Type specific generic constructor.
132 * @param TYPE Type to construct.
134 template <typename TYPE
>
135 class Constructor
: public BaseConstructor
138 virtual ~Constructor() {}
139 virtual BASE
* construct(const typename
TUPLE::Pack
& pack
) const
141 return pack
.template construct
<TYPE
>();
145 /// Hash of keys to constructors.
146 typedef QHash
<KEY
, BaseConstructor
*> ConstructorHash
;
152 /// Constructors by key.
153 ConstructorHash m_constructors
;
156 #endif // _Factory_h_