File indexing completed on 2024-12-22 04:17:59
0001 /*************************************************************************** 0002 * * 0003 * copyright : (C) 2007 The University of Toronto * 0004 * netterfield@astro.utoronto.ca * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 ***************************************************************************/ 0012 0013 #include "objectfactory.h" 0014 0015 #include <QCoreApplication> 0016 #include <QMap> 0017 0018 namespace Kst { 0019 0020 static QMap<QString, ObjectFactory*> *factories = 0; 0021 0022 void cleanupObjects() { 0023 foreach (ObjectFactory *f, *factories) { 0024 delete f; 0025 } 0026 delete factories; 0027 factories = 0; 0028 } 0029 0030 0031 ObjectFactory::ObjectFactory() { 0032 } 0033 0034 0035 ObjectFactory::~ObjectFactory() { 0036 } 0037 0038 0039 void ObjectFactory::registerFactory(const QString& node, ObjectFactory *factory) { 0040 if (!factories) { 0041 factories = new QMap<QString,ObjectFactory*>; 0042 qAddPostRoutine(cleanupObjects); 0043 } 0044 factories->insert(node, factory); 0045 } 0046 0047 0048 void ObjectFactory::registerFactory(const QStringList& nodes, ObjectFactory *factory) { 0049 foreach (const QString &n, nodes) { 0050 registerFactory(n, factory); 0051 } 0052 } 0053 0054 0055 DataObjectPtr ObjectFactory::parse(ObjectStore *store, QXmlStreamReader& stream) { 0056 if (!factories) { 0057 return 0; 0058 } 0059 0060 ObjectFactory *f = factories->value(stream.name().toString()); 0061 if (!f) { 0062 return 0; 0063 } 0064 0065 return f->generateObject(store, stream); 0066 } 0067 0068 0069 } 0070 0071 // vim: ts=2 sw=2 et