qfactorymanager.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_FACTORYMANAGER_H
00022 #define _QT_FACTORYMANAGER_H
00023 
00024 #include <qmap.h>
00025 #include <qstring.h>
00026 #include <qstringlist.h>
00027 #include <qdir.h>
00028 
00029 #include "qfactory.h"
00030 
00031 #define MESSAGES 0
00032 
00039 template<class Base>
00040 class QFactoryManager
00041 {
00042  QPtrList<QFactory<Base> > theFactories;
00043  QMap<QString, QFactory<Base> * > theMappings;
00044  QStringList theIds;
00045 
00046  void loadLibrary(const QString &theFile);
00047  void loadLibraries(const QString &thePath);
00048 
00049  void clear()
00050  {
00051 //  for(QMapIterator<QString, QFactory<Base> *> i = theFactories.begin(); i != theFactories.end(); i++)
00052 //   delete i.data();
00053   theFactories.clear();
00054   theIds.clear();
00055   theMappings.clear();
00056  }
00057 public:
00058  void reloadAll(const QString &thePath)
00059  {
00060   clear();
00061   loadLibraries(thePath);
00062  }
00063 
00064  void reloadAll(const QStringList &thePaths)
00065  {
00066   clear();
00067   for(uint i = 0; i < thePaths.size(); i++)
00068    loadLibraries(thePaths[i]);
00069  }
00070 
00071  const QStringList &getAvailable() { return theIds; }
00072  const bool isAvailable(const QString &id) { return theIds.contains(id); }
00073 
00074  const int getVersion(const QString &id)
00075  {
00076   if(!isAvailable(id)) return -1;
00077   return theMappings[id]->getVersion(id);
00078  }
00079  Base *createInstance(const QString &id)
00080  {
00081   if(!isAvailable(id)) return 0;
00082   return theMappings[id]->createInstance(id);
00083  }
00084  Base *operator[](const QString &id) { return createInstance(id); }
00085 
00086  QFactoryManager() { theFactories.setAutoDelete(true); }
00087 };
00088 
00089 template<class Base>
00090 void QFactoryManager<Base>::loadLibrary(const QString &theFile)
00091 {
00092  if(MESSAGES) qDebug("Loading library %s...", theFile.latin1());
00093  QFactory<Base> *newFactory = new QFactory<Base>(theFile);
00094  if(!newFactory->isOpen()) { delete newFactory; return; }
00095 
00096  bool used = false;
00097  const QStringList &ids = newFactory->getAvailable();
00098  for(QStringList::const_iterator i = ids.begin(); i != ids.end(); i++)
00099  {
00100   if(MESSAGES) qDebug("Found processor %s...", (*i).latin1());
00101   if(!theIds.contains(*i))
00102    theIds += *i;
00103   else if(newFactory->getVersion(*i) <= getVersion(*i))
00104    continue;
00105   if(MESSAGES) qDebug("Using it (new version: %d)", newFactory->getVersion(*i));
00106   theMappings[*i] = newFactory;
00107   used = true;
00108  }
00109  if(used)
00110   theFactories.append(newFactory);
00111  else
00112   delete newFactory;
00113 }
00114 
00115 template<class Base>
00116 void QFactoryManager<Base>::loadLibraries(const QString &thePath)
00117 {
00118  if(MESSAGES) qDebug("Scanning path: %s...", thePath.latin1());
00119  QDir d(thePath);
00120  d.setFilter(QDir::Readable | QDir::Executable | QDir::Files | QDir::NoSymLinks);
00121  QStringList l = d.entryList();
00122  for(uint i = 0; i < l.count(); i++)
00123  { if(MESSAGES) qDebug("Loading library %s...", l[i].latin1());
00124   loadLibrary(thePath + "/" + l[i]);
00125  }
00126 }
00127 
00128 #undef MESSAGES
00129 
00130 #endif

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