qfactory.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Gav Wood                                        *
00003  *   gav@kde.org                                                           *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef _QT_FACTORY_H
00022 #define _QT_FACTORY_H
00023 
00024 #include <typeinfo>
00025 
00026 #include <qmap.h>
00027 #include <qstring.h>
00028 #include <qlibrary.h>
00029 #include <qstringlist.h>
00030 
00031 #define MESSAGES 0
00032 
00033 template<class Base>
00034 class QFactory : private QLibrary
00035 {
00036  QMap<QString, Base *(*)()> theCreators;
00037  QMap<QString, int> theVersions;
00038  QStringList theIds;
00039 
00040 public:
00042  Base *createInstance(const QString &name) { if(isOpen()) if(theIds.contains(name)) return theCreators[name](); return 0; }
00043 
00045  int getVersion(const QString &name) { if(isOpen()) if(theIds.contains(name)) return theVersions[name]; return -1; }
00046 
00048  const QStringList &getAvailable() { return theIds; }
00049 
00051  bool isOpen() { return isLoaded(); }
00052 
00054  QFactory(const QString &libPath);
00055 };
00056 
00057 template<class Base>
00058 QFactory<Base>::QFactory(const QString &libPath) : QLibrary(libPath)
00059 {
00060  load();
00061  QStringList provIds;
00062  theIds.clear();
00063  if(MESSAGES) qDebug("Retrieving available classes derived from %s...", typeid(Base).name());
00064  if(resolve("getAvailable"))
00065   provIds = ((const QStringList &(*)(const QString &))(resolve("getAvailable")))(typeid(Base).name());
00066  if(MESSAGES) qDebug("Found %d candidates.", provIds.count());
00067  for(QStringList::const_iterator i = provIds.begin(); i != provIds.end(); i++)
00068  { if(MESSAGES) qDebug("Loading class %s (derived from %s)...", (*i).latin1(), typeid(Base).name());
00069   Base *(*creator)();
00070   int version = -1;
00071   creator = (Base *(*)())(resolve("create" + *i));
00072   if(resolve("version" + *i))
00073    version = ((int (*)())(resolve("version" + *i)))();
00074   if(creator && version > -1)
00075   { theIds += *i;
00076    theCreators[*i] = creator;
00077    theVersions[*i] = version;
00078    if(MESSAGES) qDebug("Loaded OK (Version: %d)", version);
00079   }
00080  }
00081  if(!theIds.size())
00082   unload();
00083 }
00084 
00085 #undef MESSAGES
00086 
00087 #endif

Generated on Thu Jul 13 06:56:33 2006 for Exscalibar by  doxygen 1.4.7