1 /***************************************************************************
2     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #ifndef TELLICO_COLLECTIONFACTORY_H
26 #define TELLICO_COLLECTIONFACTORY_H
27 
28 #include "datavectors.h"
29 
30 #include <QHash>
31 
32 namespace Tellico {
33 
34 typedef QHash<int, QString> CollectionNameHash;
35 
36 /**
37  * A factory class for dealing with the different types of collections.
38  *
39  * @author Robby Stephenson
40  */
41 class CollectionFactory {
42 
43 typedef Data::CollPtr (*CREATE_COLL_FN)(bool);
44 
45 public:
46   /** Singleton access.
47    */
48   static CollectionFactory& self();
49   static Data::CollPtr collection(int type, bool addDefaultFields);
50   static Data::CollPtr collection(const QString& typeName, bool addDefaultFields);
51   static CollectionNameHash nameHash();
52   static QString typeName(int type);
53   static QString typeName(Data::CollPtr coll);
54   static bool isDefaultField(int type, const QString& name);
55 
56   // public so we can iterate over them
57   typedef QHash<int, QString> TypeStringHash;
58   TypeStringHash nameRegistry;
59 
60   /**
61    * Classes derived from Collection call this function once
62    * per program to register the class ID key, and a pointer to
63    * the function that creates the class.
64    */
65   void registerFunction(int type, const QString& typeName, CREATE_COLL_FN func);
66 
67   /**
68    * Create a new collection of type
69    *
70    * @param addDefaultFields add the default fields to the collection
71    */
72   Data::CollPtr create(int type, bool addDefaultFields) const;
73 
74 private:
75   /**
76    * no copying
77    */
78   CollectionFactory();
79   CollectionFactory(const CollectionFactory&); ///< Not implemented.
80   CollectionFactory &operator=(const CollectionFactory&); ///< Not implemented.
81 
82   /**
83    * Keep a hash of all the function pointers to create classes
84    */
85   typedef QHash<int, CREATE_COLL_FN> FunctionRegistry;
86   FunctionRegistry functionRegistry;
87 };
88 
89 /**
90  * Helper template for registering collection classes
91  */
92 template <class Derived>
93 class RegisterCollection {
94 public:
createInstance(bool addDefaultFields)95   static Tellico::Data::CollPtr createInstance(bool addDefaultFields) {
96     return Tellico::Data::CollPtr(new Derived(addDefaultFields));
97   }
RegisterCollection(int type,const char * typeName)98   RegisterCollection(int type, const char* typeName) {
99     CollectionFactory::self().registerFunction(type, QLatin1String(typeName), createInstance);
100   }
101 };
102 
103 } // end namespace
104 #endif
105