ctkAbstractLibraryFactory.h 3.1 KB

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