ctkAbstractObjectFactory.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. #ifndef __ctkAbstractObjectFactory_h
  11. #define __ctkAbstractObjectFactory_h
  12. // Qt includes
  13. #include <QDebug>
  14. // CTK includes
  15. #include "ctkAbstractFactory.h"
  16. //----------------------------------------------------------------------------
  17. namespace{
  18. ///
  19. /// Function in charge of instanciating an object of type: ClassType
  20. template<typename BaseClassType, typename ClassType>
  21. BaseClassType *instantiateObject()
  22. {
  23. return new ClassType;
  24. }
  25. }
  26. //----------------------------------------------------------------------------
  27. template<typename BaseClassType, typename ClassType>
  28. class ctkFactoryObjectItem : public ctkAbstractFactoryItem<BaseClassType>
  29. {
  30. protected:
  31. typedef BaseClassType *(*InstantiateObjectFunc)();
  32. public:
  33. explicit ctkFactoryObjectItem(const QString& key);
  34. virtual bool load();
  35. protected:
  36. virtual BaseClassType* instanciator();
  37. private:
  38. InstantiateObjectFunc instantiateObjectFunc;
  39. };
  40. //----------------------------------------------------------------------------
  41. template<typename BaseClassType>
  42. class ctkAbstractObjectFactory : public ctkAbstractFactory<BaseClassType>
  43. {
  44. public:
  45. //-----------------------------------------------------------------------------
  46. ///
  47. /// Constructor/Desctructor
  48. explicit ctkAbstractObjectFactory();
  49. virtual ~ctkAbstractObjectFactory();
  50. //-----------------------------------------------------------------------------
  51. ///
  52. /// Register an object in the factory
  53. template<typename ClassType>
  54. bool registerObject(const QString& key);
  55. private:
  56. ctkAbstractObjectFactory(const ctkAbstractObjectFactory &); /// Not implemented
  57. void operator=(const ctkAbstractObjectFactory&); /// Not implemented
  58. };
  59. #include "ctkAbstractObjectFactory.tpp"
  60. #endif