ctkAbstractLibraryFactory.tpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkAbstractLibraryFactory_tpp
  15. #define __ctkAbstractLibraryFactory_tpp
  16. // CTK includes
  17. #include "ctkAbstractFactory.h"
  18. //----------------------------------------------------------------------------
  19. template<typename BaseClassType>
  20. ctkFactoryLibraryItem<BaseClassType>::ctkFactoryLibraryItem(const QString& _key,
  21. const QString& _path)
  22. :ctkAbstractFactoryItem<BaseClassType>(_key)
  23. ,Path(_path)
  24. {
  25. }
  26. //----------------------------------------------------------------------------
  27. template<typename BaseClassType>
  28. bool ctkFactoryLibraryItem<BaseClassType>::load()
  29. {
  30. this->Library.setFileName(this->path());
  31. bool loaded = this->Library.load();
  32. if (loaded)
  33. {
  34. this->resolve();
  35. return true;
  36. }
  37. return false;
  38. }
  39. //----------------------------------------------------------------------------
  40. template<typename BaseClassType>
  41. QString ctkFactoryLibraryItem<BaseClassType>::path()const
  42. {
  43. return this->Path;
  44. }
  45. //----------------------------------------------------------------------------
  46. template<typename BaseClassType>
  47. QString ctkFactoryLibraryItem<BaseClassType>::loadErrorString()const
  48. {
  49. return this->Library.errorString();
  50. }
  51. //----------------------------------------------------------------------------
  52. template<typename BaseClassType>
  53. void ctkFactoryLibraryItem<BaseClassType>::setSymbols(const QStringList& symbols)
  54. {
  55. this->Symbols = symbols;
  56. }
  57. //-----------------------------------------------------------------------------
  58. template<typename BaseClassType>
  59. void ctkFactoryLibraryItem<BaseClassType>::resolve()
  60. {
  61. foreach(const QString& symbol, this->Symbols)
  62. {
  63. // Sanity checks
  64. if (symbol.isEmpty())
  65. {
  66. continue;
  67. }
  68. // Make sure the symbols haven't been registered
  69. if (this->ResolvedSymbols.contains(symbol))
  70. {
  71. qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->Path;
  72. continue;
  73. }
  74. void * resolvedSymbol = this->Library.resolve(symbol.toLatin1());
  75. if (!resolvedSymbol)
  76. {
  77. qWarning() << "Failed to resolve symbol '" << symbol << "' - Path:" << this->Path;
  78. }
  79. this->ResolvedSymbols[symbol] = resolvedSymbol;
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. template<typename BaseClassType>
  84. void* ctkFactoryLibraryItem<BaseClassType>::symbolAddress(const QString& symbol)const
  85. {
  86. ConstIterator iter = this->ResolvedSymbols.find(symbol);
  87. Q_ASSERT(iter != this->ResolvedSymbols.constEnd());
  88. if ( iter == this->ResolvedSymbols.constEnd())
  89. {
  90. return 0;
  91. }
  92. return iter.value();
  93. }
  94. //-----------------------------------------------------------------------------
  95. template<typename BaseClassType, typename FactoryItemType>
  96. ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::ctkAbstractLibraryFactory()
  97. :ctkAbstractFactory<BaseClassType>()
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. template<typename BaseClassType, typename FactoryItemType>
  102. ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::~ctkAbstractLibraryFactory()
  103. {
  104. }
  105. //-----------------------------------------------------------------------------
  106. template<typename BaseClassType, typename FactoryItemType>
  107. void ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::setSymbols(const QStringList& symbols)
  108. {
  109. this->Symbols = symbols;
  110. }
  111. //-----------------------------------------------------------------------------
  112. template<typename BaseClassType, typename FactoryItemType>
  113. bool ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::registerLibrary(const QFileInfo& file, QString& key)
  114. {
  115. key = file.fileName();
  116. // Check if already registered
  117. if (this->item(key))
  118. {
  119. return false;
  120. }
  121. QSharedPointer<FactoryItemType> _item =
  122. QSharedPointer<FactoryItemType>(new FactoryItemType(key, file.filePath()));
  123. _item->setSymbols(this->Symbols);
  124. return this->registerItem(_item);
  125. }
  126. #endif