/*========================================================================= Library: CTK Copyright (c) Kitware Inc. All rights reserved. Distributed under a BSD License. See LICENSE.txt file. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #ifndef __ctkAbstractLibraryFactory_tpp #define __ctkAbstractLibraryFactory_tpp // CTK includes #include "ctkAbstractFactory.h" //---------------------------------------------------------------------------- template ctkFactoryLibraryItem::ctkFactoryLibraryItem(const QString& _key, const QString& _path) :ctkAbstractFactoryItem(_key) ,Path(_path) { } //---------------------------------------------------------------------------- template bool ctkFactoryLibraryItem::load() { this->Library.setFileName(this->path()); bool loaded = this->Library.load(); if (loaded) { this->resolve(); return true; } return false; } //---------------------------------------------------------------------------- template QString ctkFactoryLibraryItem::path()const { return this->Path; } //---------------------------------------------------------------------------- template QString ctkFactoryLibraryItem::loadErrorString()const { return this->Library.errorString(); } //---------------------------------------------------------------------------- template void ctkFactoryLibraryItem::setSymbols(const QStringList& symbols) { this->Symbols = symbols; } //----------------------------------------------------------------------------- template void ctkFactoryLibraryItem::resolve() { foreach(const QString& symbol, this->Symbols) { // Sanity checks if (symbol.isEmpty()) { continue; } // Make sure the symbols haven't been registered if (this->ResolvedSymbols.contains(symbol)) { qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->Path; continue; } void * resolvedSymbol = this->Library.resolve(symbol.toLatin1()); if (!resolvedSymbol) { qWarning() << "Failed to resolve symbol '" << symbol << "' - Path:" << this->Path; } this->ResolvedSymbols[symbol] = resolvedSymbol; } } //----------------------------------------------------------------------------- template void* ctkFactoryLibraryItem::symbolAddress(const QString& symbol)const { ConstIterator iter = this->ResolvedSymbols.find(symbol); Q_ASSERT(iter != this->ResolvedSymbols.constEnd()); if ( iter == this->ResolvedSymbols.constEnd()) { return 0; } return iter.value(); } //----------------------------------------------------------------------------- template ctkAbstractLibraryFactory::ctkAbstractLibraryFactory() :ctkAbstractFactory() { } //----------------------------------------------------------------------------- template ctkAbstractLibraryFactory::~ctkAbstractLibraryFactory() { } //----------------------------------------------------------------------------- template void ctkAbstractLibraryFactory::setSymbols(const QStringList& symbols) { this->Symbols = symbols; } //----------------------------------------------------------------------------- template bool ctkAbstractLibraryFactory::registerLibrary(const QFileInfo& file, QString& key) { key = file.fileName(); // Check if already registered if (this->item(key)) { return false; } QSharedPointer _item = QSharedPointer(new FactoryItemType(key, file.filePath())); _item->setSymbols(this->Symbols); return this->registerItem(_item); } #endif