/*========================================================================= Library: CTK Copyright (c) 2010 Kitware Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.commontk.org/LICENSE Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =========================================================================*/ #ifndef __ctkAbstractLibraryFactory_tpp #define __ctkAbstractLibraryFactory_tpp // CTK includes #include "ctkAbstractFactory.h" //---------------------------------------------------------------------------- // ctkFactoryLibraryItem methods //---------------------------------------------------------------------------- template ctkFactoryLibraryItem::ctkFactoryLibraryItem(const QString& _path) :ctkAbstractFactoryItem() ,Path(_path) { } //---------------------------------------------------------------------------- template bool ctkFactoryLibraryItem::load() { this->Library.setFileName(this->path()); bool loaded = this->Library.load(); if (loaded) { if (!this->resolve()) { return false; } 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 bool 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)) { if (this->verbose()) { qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->Path; } continue; } void * resolvedSymbol = this->Library.resolve(symbol.toLatin1()); if (!resolvedSymbol) { return false; } this->ResolvedSymbols[symbol] = resolvedSymbol; } return true; } //----------------------------------------------------------------------------- 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(); } //---------------------------------------------------------------------------- // ctkAbstractLibraryFactory methods //----------------------------------------------------------------------------- template ctkAbstractLibraryFactory::ctkAbstractLibraryFactory() :ctkAbstractFactory() { } //----------------------------------------------------------------------------- template ctkAbstractLibraryFactory::~ctkAbstractLibraryFactory() { } //----------------------------------------------------------------------------- template void ctkAbstractLibraryFactory::setSymbols( const QStringList& symbols) { this->Symbols = symbols; } //----------------------------------------------------------------------------- template bool ctkAbstractLibraryFactory::registerLibrary( const QString& key, const QFileInfo& file) { QSharedPointer > itemToRegister = QSharedPointer >( this->createFactoryLibraryItem(file)); //new ctkFactoryLibraryItem(key, file.filePath())); if (itemToRegister.isNull()) { return false; } itemToRegister->setVerbose(this->verbose()); itemToRegister->setSymbols(this->Symbols); return this->registerItem(key, itemToRegister); } //----------------------------------------------------------------------------- template bool ctkAbstractLibraryFactory::registerQLibrary( const QString& key, const QFileInfo& file) { // Skip if current file isn't a library if (!QLibrary::isLibrary(file.fileName())) { return false; } if (this->verbose()) { qDebug() << "Attempt to register QLibrary:" << file.fileName(); } return this->registerLibrary(key, file); } //----------------------------------------------------------------------------- template ctkFactoryLibraryItem* ctkAbstractLibraryFactory:: createFactoryLibraryItem(const QFileInfo& library)const { Q_UNUSED(library); return 0; } #endif