ctkAbstractLibraryFactory.tpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 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.txt
  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 "ctkAbstractLibraryFactory.h"
  18. //----------------------------------------------------------------------------
  19. // ctkFactoryLibraryItem methods
  20. /*
  21. //----------------------------------------------------------------------------
  22. template<typename BaseClassType>
  23. ctkFactoryLibraryItem<BaseClassType>::ctkFactoryLibraryItem(const QString& _path)
  24. :ctkAbstractFactoryFileBasedItem<BaseClassType>(_path)
  25. {
  26. }*/
  27. //----------------------------------------------------------------------------
  28. template<typename BaseClassType>
  29. bool ctkFactoryLibraryItem<BaseClassType>::load()
  30. {
  31. this->Library.setFileName(this->path());
  32. bool loaded = this->Library.load();
  33. if (loaded)
  34. {
  35. if (!this->resolve())
  36. {
  37. return false;
  38. }
  39. return true;
  40. }
  41. return false;
  42. }
  43. //----------------------------------------------------------------------------
  44. template<typename BaseClassType>
  45. QString ctkFactoryLibraryItem<BaseClassType>::loadErrorString()const
  46. {
  47. return this->Library.errorString();
  48. }
  49. //----------------------------------------------------------------------------
  50. template<typename BaseClassType>
  51. void ctkFactoryLibraryItem<BaseClassType>::setSymbols(const QStringList& symbols)
  52. {
  53. this->Symbols = symbols;
  54. }
  55. //-----------------------------------------------------------------------------
  56. template<typename BaseClassType>
  57. bool ctkFactoryLibraryItem<BaseClassType>::resolve()
  58. {
  59. foreach(const QString& symbol, this->Symbols)
  60. {
  61. // Sanity checks
  62. if (symbol.isEmpty())
  63. {
  64. continue;
  65. }
  66. // Make sure the symbols haven't been registered
  67. if (this->ResolvedSymbols.contains(symbol))
  68. {
  69. if (this->verbose())
  70. {
  71. qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->path();
  72. }
  73. continue;
  74. }
  75. void * resolvedSymbol = this->Library.resolve(symbol.toLatin1());
  76. if (!resolvedSymbol)
  77. {
  78. return false;
  79. }
  80. this->ResolvedSymbols[symbol] = resolvedSymbol;
  81. }
  82. return true;
  83. }
  84. //-----------------------------------------------------------------------------
  85. template<typename BaseClassType>
  86. void* ctkFactoryLibraryItem<BaseClassType>::symbolAddress(const QString& symbol)const
  87. {
  88. ConstIterator iter = this->ResolvedSymbols.find(symbol);
  89. Q_ASSERT(iter != this->ResolvedSymbols.constEnd());
  90. if ( iter == this->ResolvedSymbols.constEnd())
  91. {
  92. return 0;
  93. }
  94. return iter.value();
  95. }
  96. //----------------------------------------------------------------------------
  97. // ctkAbstractLibraryFactory methods
  98. //-----------------------------------------------------------------------------
  99. template<typename BaseClassType>
  100. void ctkAbstractLibraryFactory<BaseClassType>::setSymbols(
  101. const QStringList& symbols)
  102. {
  103. this->Symbols = symbols;
  104. }
  105. //-----------------------------------------------------------------------------
  106. template<typename BaseClassType>
  107. bool ctkAbstractLibraryFactory<BaseClassType>
  108. ::isValidFile(const QFileInfo& file)const
  109. {
  110. return this->ctkAbstractFileBasedFactory<BaseClassType>::isValidFile(file) &&
  111. QLibrary::isLibrary(file.fileName());
  112. }
  113. //-----------------------------------------------------------------------------
  114. template<typename BaseClassType>
  115. void ctkAbstractLibraryFactory<BaseClassType>::
  116. initItem(ctkAbstractFactoryItem<BaseClassType>* item)
  117. {
  118. this->ctkAbstractFileBasedFactory<BaseClassType>::initItem(item);
  119. dynamic_cast<ctkFactoryLibraryItem<BaseClassType>*>(item)->setSymbols(this->Symbols);
  120. }
  121. #endif