Add toolbar to bible plugin with show now, insert into playlist
[kworship.git] / include / Factory.h
blob582e5a12e9c95a1a6289bdf230de1c69868c723a
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #ifndef _Factory_h_
21 #define _Factory_h_
23 /**
24 * @file Factory.h
25 * @brief A generic object factory.
26 * @author James Hogan <james@albanarts.com>
29 #include "functionInfo.h"
31 #include <QHash>
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>
37 class Factory
39 public:
42 * Constructors + destructor
45 /// Default constructor.
46 Factory()
47 : m_constructors()
51 /// Destructor.
52 ~Factory()
54 foreach (BaseConstructor* constructor, m_constructors)
56 delete constructor;
61 * Type registration
64 /** Add a type to the factory.
65 * @param T Type to add.
66 * @param key Identifier used to construct.
68 template <typename T>
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>();
75 return true;
77 return false;
81 * Construction
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);
110 else
112 return 0;
116 private:
119 * Types
122 /// Base constructor class.
123 class BaseConstructor
125 public:
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
137 public:
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;
149 * Variables
152 /// Constructors by key.
153 ConstructorHash m_constructors;
156 #endif // _Factory_h_