00001 /*************************************************************************** 00002 * file klffactory.cpp 00003 * This file is part of the KLatexFormula Project. 00004 * Copyright (C) 2011 by Philippe Faist 00005 * philippe.faist at bluewin.ch 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 ***************************************************************************/ 00022 /* $Id$ */ 00023 00024 #include <QStringList> 00025 00026 #include <klfdefs.h> 00027 00028 #include "klffactory.h" 00029 00030 00031 KLFFactoryBase::KLFFactoryBase(KLFFactoryManager *factoryManager) 00032 : pFactoryManager(factoryManager) 00033 { 00034 KLF_ASSERT_NOT_NULL(pFactoryManager, "NULL factory manager given!", return; ) ; 00035 pFactoryManager->registerFactory(this); 00036 } 00037 KLFFactoryBase::~KLFFactoryBase() 00038 { 00039 KLF_ASSERT_NOT_NULL(pFactoryManager, "NULL factory manager!", return; ) ; 00040 pFactoryManager->unRegisterFactory(this); 00041 } 00042 00043 // -- 00044 00045 KLFFactoryManager::KLFFactoryManager() 00046 { 00047 } 00048 KLFFactoryManager::~KLFFactoryManager() 00049 { 00050 } 00051 00052 KLFFactoryBase * KLFFactoryManager::findFactoryFor(const QString& objType) 00053 { 00054 int k; 00055 for (k = 0; k < pRegisteredFactories.size(); ++k) { 00056 KLFFactoryBase * factory = pRegisteredFactories[k]; 00057 if (factory->supportedTypes().contains(objType)) { 00058 return factory; 00059 } 00060 } 00061 qWarning() 00062 <<"KLFFactoryManager::findFactoryFor(object type="<<objType<<"): No factory found!"; 00063 00064 return NULL; 00065 } 00066 00067 QStringList KLFFactoryManager::allSupportedTypes() 00068 { 00069 QStringList objtypes; 00070 int k; 00071 for (k = 0; k < pRegisteredFactories.size(); ++k) { 00072 objtypes << pRegisteredFactories[k]->supportedTypes(); 00073 } 00074 return objtypes; 00075 } 00076 00077 void KLFFactoryManager::registerFactory(KLFFactoryBase *factory) 00078 { 00079 if (pRegisteredFactories.indexOf(factory) != -1) { 00080 qWarning()<<"KLFFactory<>::registerFactory(): Factory " << factory << " is already registered!"; 00081 return; 00082 } 00083 pRegisteredFactories.prepend(factory); 00084 } 00085 00086 void KLFFactoryManager::unRegisterFactory(KLFFactoryBase *factory) 00087 { 00088 if (pRegisteredFactories.indexOf(factory) == -1) { 00089 qWarning()<<"KLFFactory<>::unRegisterFactory(): Factory "<<factory<<" is not registered!"; 00090 return; 00091 } 00092 pRegisteredFactories.removeAll(factory); 00093 } 00094 00095