/*========================================================================= Library: CTK Copyright (c) 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.apache.org/licenses/LICENSE-2.0.txt 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 __ctkAbstractFactory_tpp #define __ctkAbstractFactory_tpp // QT includes #include // CTK includes #include "ctkAbstractFactory.h" //---------------------------------------------------------------------------- // ctkAbstractFactoryItem methods //---------------------------------------------------------------------------- template ctkAbstractFactoryItem::ctkAbstractFactoryItem() :Instance() { this->Verbose = false; } //---------------------------------------------------------------------------- template QString ctkAbstractFactoryItem::loadErrorString()const { return QString(); } //---------------------------------------------------------------------------- template BaseClassType* ctkAbstractFactoryItem::instantiate() { if (this->Instance) { return this->Instance; } this->Instance = this->instanciator(); return this->Instance; } //---------------------------------------------------------------------------- template bool ctkAbstractFactoryItem::instantiated()const { return (this->Instance != 0); } //---------------------------------------------------------------------------- template void ctkAbstractFactoryItem::uninstantiate() { if (!this->Instance) { return; } delete this->Instance; // Make sure the pointer is set to 0. Doing so, Will prevent attempt to // delete unextising object if uninstantiate() methods is called multiple times. this->Instance = 0; } //---------------------------------------------------------------------------- template void ctkAbstractFactoryItem::setVerbose(bool value) { this->Verbose = value; } //---------------------------------------------------------------------------- template bool ctkAbstractFactoryItem::verbose()const { return this->Verbose; } //---------------------------------------------------------------------------- // ctkAbstractFactory methods //---------------------------------------------------------------------------- template ctkAbstractFactory::ctkAbstractFactory() { this->Verbose = false; this->RegisteredItemMap = QSharedPointer(new HashType); } //---------------------------------------------------------------------------- template ctkAbstractFactory::~ctkAbstractFactory() { } //---------------------------------------------------------------------------- template void ctkAbstractFactory::printAdditionalInfo() { qDebug() << "ctkAbstractFactory (" << this << ")"; // TODO } //---------------------------------------------------------------------------- template BaseClassType* ctkAbstractFactory::instantiate(const QString& itemKey) { ctkAbstractFactoryItem* _item = this->item(itemKey); return (_item ? _item->instantiate() : 0); } //---------------------------------------------------------------------------- template void ctkAbstractFactory::uninstantiate(const QString& itemKey) { ctkAbstractFactoryItem * _item = this->item(itemKey); if (!_item) { return; } _item->uninstantiate(); } //---------------------------------------------------------------------------- template QStringList ctkAbstractFactory::keys() const { // Since by construction, we checked if a name was already in the QHash, // there is no need to call 'uniqueKeys' return this->RegisteredItemMap->keys(); } //---------------------------------------------------------------------------- template bool ctkAbstractFactory::registerItem(const QString& key, const QSharedPointer > & _item) { // Sanity checks if (!_item || key.isEmpty() || this->item(key)) { if (this->verbose()) { qDebug() << __FUNCTION__ << "key is empty or already exists:" << key << "; item: " << _item; } return false; } // Attempt to load it if (!_item->load()) { QString errorStr; if (!_item->loadErrorString().isEmpty()) { errorStr = " - " + _item->loadErrorString(); } if (this->verbose()) { qCritical() << "Failed to load object:" << key << errorStr ; } return false; } // Store its reference using a QSharedPointer this->RegisteredItemMap->insert(key, _item); return true; } //---------------------------------------------------------------------------- template ctkAbstractFactoryItem * ctkAbstractFactory::item(const QString& itemKey)const { ConstIterator iter = this->RegisteredItemMap->find(itemKey); if ( iter == this->RegisteredItemMap->constEnd()) { return 0; } return iter.value().data(); } //---------------------------------------------------------------------------- template void ctkAbstractFactory::setVerbose(bool value) { this->Verbose = value; } //---------------------------------------------------------------------------- template bool ctkAbstractFactory::verbose()const { return this->Verbose; } //---------------------------------------------------------------------------- template void ctkAbstractFactory::setRegisteredItems(const QSharedPointer& items) { this->RegisteredItemMap = items; } //---------------------------------------------------------------------------- template QSharedPointer::HashType> ctkAbstractFactory::registeredItems() { return this->RegisteredItemMap; } #endif