ctkAbstractLibraryFactory.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.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_h
  15. #define __ctkAbstractLibraryFactory_h
  16. // Qt includes
  17. #include <QFileInfo>
  18. #include <QLibrary>
  19. #include <QStringList>
  20. // CTK includes
  21. #include "ctkAbstractFileBasedFactory.h"
  22. //----------------------------------------------------------------------------
  23. template<typename BaseClassType>
  24. class ctkFactoryLibraryItem : public ctkAbstractFactoryFileBasedItem<BaseClassType>
  25. {
  26. protected:
  27. typedef typename QHash<QString, void*>::const_iterator ConstIterator;
  28. typedef typename QHash<QString, void*>::iterator Iterator;
  29. public:
  30. explicit ctkFactoryLibraryItem(const QString& path);
  31. virtual bool load();
  32. virtual QString loadErrorString()const;
  33. ///
  34. /// Set list of required symbols
  35. void setSymbols(const QStringList& symbols);
  36. ///
  37. /// \brief Resolve symbols
  38. /// \note The function will return False if it fails to resolve one
  39. /// of the required symbols set using ::setSymbols
  40. bool resolve();
  41. ///
  42. /// Get symbol address
  43. void* symbolAddress(const QString& symbol)const;
  44. private:
  45. QLibrary Library;
  46. QHash<QString, void*> ResolvedSymbols;
  47. QStringList Symbols;
  48. };
  49. //----------------------------------------------------------------------------
  50. template<typename BaseClassType>
  51. class ctkAbstractLibraryFactory : public ctkAbstractFileBasedFactory<BaseClassType>
  52. {
  53. public:
  54. ///
  55. /// Constructor
  56. explicit ctkAbstractLibraryFactory();
  57. virtual ~ctkAbstractLibraryFactory();
  58. /// Set the list of symbols
  59. void setSymbols(const QStringList& symbols);
  60. /// \brief Register a plugin in the factory
  61. /// The parameter \a key must be unique
  62. bool registerLibrary(const QString& key, const QFileInfo& file);
  63. /// \brief Utility function to register a QLibrary
  64. /// The parameter \a key must be unique
  65. bool registerQLibrary(const QString& key, const QFileInfo& file);
  66. protected:
  67. virtual ctkFactoryLibraryItem<BaseClassType>* createFactoryLibraryItem(
  68. const QFileInfo& library)const;
  69. private:
  70. ctkAbstractLibraryFactory(const ctkAbstractLibraryFactory &); /// Not implemented
  71. void operator=(const ctkAbstractLibraryFactory&); /// Not implemented
  72. QStringList Symbols;
  73. };
  74. #include "ctkAbstractLibraryFactory.tpp"
  75. #endif