ctkAbstractLibraryFactory.tpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.commontk.org/LICENSE
  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. // ctkFactoryLibraryItem methods
  20. //----------------------------------------------------------------------------
  21. template<typename BaseClassType>
  22. ctkFactoryLibraryItem<BaseClassType>::ctkFactoryLibraryItem(const QString& _key,
  23. const QString& _path)
  24. :ctkAbstractFactoryItem<BaseClassType>(_key)
  25. ,Path(_path)
  26. {
  27. }
  28. //----------------------------------------------------------------------------
  29. template<typename BaseClassType>
  30. bool ctkFactoryLibraryItem<BaseClassType>::load()
  31. {
  32. this->Library.setFileName(this->path());
  33. bool loaded = this->Library.load();
  34. if (loaded)
  35. {
  36. if (!this->resolve())
  37. {
  38. return false;
  39. }
  40. return true;
  41. }
  42. return false;
  43. }
  44. //----------------------------------------------------------------------------
  45. template<typename BaseClassType>
  46. QString ctkFactoryLibraryItem<BaseClassType>::path()const
  47. {
  48. return this->Path;
  49. }
  50. //----------------------------------------------------------------------------
  51. template<typename BaseClassType>
  52. QString ctkFactoryLibraryItem<BaseClassType>::loadErrorString()const
  53. {
  54. return this->Library.errorString();
  55. }
  56. //----------------------------------------------------------------------------
  57. template<typename BaseClassType>
  58. void ctkFactoryLibraryItem<BaseClassType>::setSymbols(const QStringList& symbols)
  59. {
  60. this->Symbols = symbols;
  61. }
  62. //-----------------------------------------------------------------------------
  63. template<typename BaseClassType>
  64. bool ctkFactoryLibraryItem<BaseClassType>::resolve()
  65. {
  66. foreach(const QString& symbol, this->Symbols)
  67. {
  68. // Sanity checks
  69. if (symbol.isEmpty())
  70. {
  71. continue;
  72. }
  73. // Make sure the symbols haven't been registered
  74. if (this->ResolvedSymbols.contains(symbol))
  75. {
  76. if (this->verbose())
  77. {
  78. qWarning() << "Symbol '" << symbol << "' already resolved - Path:" << this->Path;
  79. }
  80. continue;
  81. }
  82. void * resolvedSymbol = this->Library.resolve(symbol.toLatin1());
  83. if (!resolvedSymbol)
  84. {
  85. return false;
  86. }
  87. this->ResolvedSymbols[symbol] = resolvedSymbol;
  88. }
  89. return true;
  90. }
  91. //-----------------------------------------------------------------------------
  92. template<typename BaseClassType>
  93. void* ctkFactoryLibraryItem<BaseClassType>::symbolAddress(const QString& symbol)const
  94. {
  95. ConstIterator iter = this->ResolvedSymbols.find(symbol);
  96. Q_ASSERT(iter != this->ResolvedSymbols.constEnd());
  97. if ( iter == this->ResolvedSymbols.constEnd())
  98. {
  99. return 0;
  100. }
  101. return iter.value();
  102. }
  103. //----------------------------------------------------------------------------
  104. // ctkAbstractLibraryFactory methods
  105. //-----------------------------------------------------------------------------
  106. template<typename BaseClassType, typename FactoryItemType>
  107. ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::ctkAbstractLibraryFactory()
  108. :ctkAbstractFactory<BaseClassType>()
  109. {
  110. }
  111. //-----------------------------------------------------------------------------
  112. template<typename BaseClassType, typename FactoryItemType>
  113. ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::~ctkAbstractLibraryFactory()
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. template<typename BaseClassType, typename FactoryItemType>
  118. void ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::setSymbols(
  119. const QStringList& symbols)
  120. {
  121. this->Symbols = symbols;
  122. }
  123. //-----------------------------------------------------------------------------
  124. template<typename BaseClassType, typename FactoryItemType>
  125. QString ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::fileNameToKey(
  126. const QString& fileName)
  127. {
  128. return fileName;
  129. }
  130. //-----------------------------------------------------------------------------
  131. template<typename BaseClassType, typename FactoryItemType>
  132. bool ctkAbstractLibraryFactory<BaseClassType, FactoryItemType>::registerLibrary(
  133. const QFileInfo& file, QString& key)
  134. {
  135. key = this->fileNameToKey(file.fileName());
  136. // Check if already registered
  137. if (this->item(key))
  138. {
  139. return false;
  140. }
  141. QSharedPointer<FactoryItemType> _item =
  142. QSharedPointer<FactoryItemType>(new FactoryItemType(key, file.filePath()));
  143. _item->setVerbose(this->verbose());
  144. _item->setSymbols(this->Symbols);
  145. return this->registerItem(_item);
  146. }
  147. #endif