ctkAbstractFactoryTest1.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // Qt includes
  15. #include <QCoreApplication>
  16. // CTK includes
  17. #include "ctkAbstractFactory.h"
  18. #include "ctkModelTester.h"
  19. // STD includes
  20. #include <cstdlib>
  21. #include <iostream>
  22. template<class BaseClassType>
  23. class FactoryItem : public ctkAbstractFactoryItem<BaseClassType>
  24. {
  25. public:
  26. virtual bool load()
  27. {
  28. return true;
  29. }
  30. protected:
  31. /// Must be reimplemented in subclasses to instanciate a BaseClassType*
  32. virtual BaseClassType* instanciator()
  33. {
  34. return new BaseClassType;
  35. }
  36. };
  37. template<class BaseClassType>
  38. class Factory: public ctkAbstractFactory<BaseClassType>
  39. {
  40. public:
  41. virtual void registerItems()
  42. {
  43. this->registerItem("item1", QSharedPointer<ctkAbstractFactoryItem<BaseClassType> >(new FactoryItem<BaseClassType>()));
  44. this->registerItem("item2", QSharedPointer<ctkAbstractFactoryItem<BaseClassType> >(new FactoryItem<BaseClassType>()));
  45. }
  46. };
  47. struct Item{
  48. };
  49. //-----------------------------------------------------------------------------
  50. int ctkAbstractFactoryTest1(int argc, char * argv [] )
  51. {
  52. QCoreApplication app(argc, argv);
  53. ctkAbstractFactory<Item> abstractFactory;
  54. abstractFactory.printAdditionalInfo();
  55. Item* item = abstractFactory.instantiate("unregistered item");
  56. if (item != 0)
  57. {
  58. std::cerr << "ctkAbstractFactory::instantiate() failed" << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. abstractFactory.uninstantiate("uninstanced item");
  62. if (!abstractFactory.path("wrong key").isEmpty())
  63. {
  64. std::cerr<< "ctkAbstractFactory::path() failed" << std::endl;
  65. return EXIT_FAILURE;
  66. }
  67. abstractFactory.registerItems();
  68. if (abstractFactory.itemKeys().count() != 0)
  69. {
  70. std::cerr<< "ctkAbstractFactory::keys() failed" << std::endl;
  71. return EXIT_FAILURE;
  72. }
  73. abstractFactory.setVerbose(true);
  74. if (abstractFactory.verbose() != true)
  75. {
  76. std::cerr<< "ctkAbstractFactory::setVerbose() failed" << std::endl;
  77. return EXIT_FAILURE;
  78. }
  79. Factory<Item> factory;
  80. factory.printAdditionalInfo();
  81. Item* item1 = factory.instantiate("item1");
  82. if (item != 0)
  83. {
  84. std::cerr << "ctkAbstractFactory::instantiate() failed" << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. factory.uninstantiate("item1");
  88. if (!factory.path("wrong key").isEmpty())
  89. {
  90. std::cerr<< "ctkAbstractFactory::path() failed" << std::endl;
  91. return EXIT_FAILURE;
  92. }
  93. factory.registerItems();
  94. if (factory.itemKeys().count() != 2 ||
  95. factory.itemKeys()[0] != "item1" ||
  96. factory.itemKeys()[1] != "item2")
  97. {
  98. std::cerr<< "ctkAbstractFactory::keys() failed" << std::endl;
  99. return EXIT_FAILURE;
  100. }
  101. item1 = factory.instantiate("item1");
  102. if (item1 == 0)
  103. {
  104. std::cerr << "ctkAbstractFactory::instantiate() failed" << std::endl;
  105. return EXIT_FAILURE;
  106. }
  107. factory.uninstantiate("item1");
  108. return EXIT_SUCCESS;
  109. }