File indexing completed on 2024-04-28 05:08:15

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_COLLECTIONFACTORY_H
0026 #define TELLICO_COLLECTIONFACTORY_H
0027 
0028 #include "datavectors.h"
0029 
0030 #include <QHash>
0031 
0032 namespace Tellico {
0033 
0034 typedef QHash<int, QString> CollectionNameHash;
0035 
0036 /**
0037  * A factory class for dealing with the different types of collections.
0038  *
0039  * @author Robby Stephenson
0040  */
0041 class CollectionFactory {
0042 
0043 typedef Data::CollPtr (*CREATE_COLL_FN)(bool);
0044 
0045 public:
0046   /** Singleton access.
0047    */
0048   static CollectionFactory& self();
0049   static Data::CollPtr collection(int type, bool addDefaultFields);
0050   static Data::CollPtr collection(const QString& typeName, bool addDefaultFields);
0051   static CollectionNameHash nameHash();
0052   static QString typeName(int type);
0053   static QString typeName(Data::CollPtr coll);
0054   static bool isDefaultField(int type, const QString& name);
0055 
0056   // public so we can iterate over them
0057   typedef QHash<int, QString> TypeStringHash;
0058   TypeStringHash nameRegistry;
0059 
0060   /**
0061    * Classes derived from Collection call this function once
0062    * per program to register the class ID key, and a pointer to
0063    * the function that creates the class.
0064    */
0065   void registerFunction(int type, const QString& typeName, CREATE_COLL_FN func);
0066 
0067   /**
0068    * Create a new collection of type
0069    *
0070    * @param addDefaultFields add the default fields to the collection
0071    */
0072   Data::CollPtr create(int type, bool addDefaultFields) const;
0073 
0074 private:
0075   /**
0076    * no copying
0077    */
0078   CollectionFactory();
0079   CollectionFactory(const CollectionFactory&); ///< Not implemented.
0080   CollectionFactory &operator=(const CollectionFactory&); ///< Not implemented.
0081 
0082   /**
0083    * Keep a hash of all the function pointers to create classes
0084    */
0085   typedef QHash<int, CREATE_COLL_FN> FunctionRegistry;
0086   FunctionRegistry functionRegistry;
0087 };
0088 
0089 /**
0090  * Helper template for registering collection classes
0091  */
0092 template <class Derived>
0093 class RegisterCollection {
0094 public:
0095   static Tellico::Data::CollPtr createInstance(bool addDefaultFields) {
0096     return Tellico::Data::CollPtr(new Derived(addDefaultFields));
0097   }
0098   RegisterCollection(int type, const char* typeName) {
0099     CollectionFactory::self().registerFunction(type, QLatin1String(typeName), createInstance);
0100   }
0101 };
0102 
0103 } // end namespace
0104 #endif