ctkAbstractLibraryFactoryTest1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "ctkAbstractLibraryFactory.h"
  18. #include "ctkDummyPlugin.h"
  19. // STD includes
  20. #include <cstdlib>
  21. #include <iostream>
  22. //-----------------------------------------------------------------------------
  23. class ctkDummyLibrary
  24. {
  25. };
  26. //-----------------------------------------------------------------------------
  27. class ctkDummyLibraryItem: public ctkFactoryLibraryItem<ctkDummyLibrary>
  28. {
  29. protected:
  30. virtual ctkDummyLibrary* instanciator()
  31. {
  32. // Using a scoped pointer ensures the memory will be cleaned if instanciator
  33. // fails before returning the module. See QScopedPointer::take()
  34. QScopedPointer<ctkDummyLibrary> module(new ctkDummyLibrary());
  35. foreach(QString symbol, this->Symbols)
  36. {
  37. void* res = this->symbolAddress(symbol);
  38. if (!res)
  39. {
  40. }
  41. }
  42. return module.take();
  43. }
  44. };
  45. //-----------------------------------------------------------------------------
  46. class ctkDummyLibraryFactoryItem: public ctkAbstractLibraryFactory<ctkDummyLibrary>
  47. {
  48. protected:
  49. //-----------------------------------------------------------------------------
  50. ctkAbstractFactoryItem<ctkDummyLibrary>* createFactoryFileBasedItem()
  51. {
  52. return new ctkDummyLibraryItem();
  53. }
  54. };
  55. //-----------------------------------------------------------------------------
  56. int ctkAbstractLibraryFactoryTest1(int argc, char * argv [])
  57. {
  58. QCoreApplication app(argc, argv);
  59. if (argc <= 1)
  60. {
  61. std::cerr << "Missing argument" << std::endl;
  62. return EXIT_FAILURE;
  63. }
  64. QString filePath(argv[1]);
  65. QFileInfo file(filePath);
  66. while (filePath.contains("$(OutDir)"))
  67. {
  68. QString debugFilePath = filePath;
  69. debugFilePath.replace("$(OutDir)","Debug");
  70. if (QFile::exists(QString(debugFilePath)))
  71. {
  72. file = QFileInfo(debugFilePath);
  73. break;
  74. }
  75. QString releaseFilePath = filePath;
  76. releaseFilePath.replace("$(OutDir)","Release");
  77. if (QFile::exists(QString(releaseFilePath)))
  78. {
  79. file = QFileInfo(releaseFilePath);
  80. break;
  81. }
  82. return EXIT_FAILURE;
  83. }
  84. ctkDummyLibraryFactoryItem libraryFactory;
  85. libraryFactory.setVerbose(true);
  86. bool res = libraryFactory.registerFileItem("fail", QFileInfo("foo/bar.txt"));
  87. if (res)
  88. {
  89. std::cerr << "ctkAbstractLibraryFactory::registerLibrary() registered bad file"
  90. << std::endl;
  91. return EXIT_FAILURE;
  92. }
  93. res = libraryFactory.registerFileItem("lib", file);
  94. if (!res || libraryFactory.itemKeys().count() != 1)
  95. {
  96. std::cerr << "ctkAbstractLibraryFactory::registerLibrary() failed"
  97. << libraryFactory.itemKeys().count() << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. // register twice must return false
  101. res = libraryFactory.registerFileItem("lib", file);
  102. if (res || libraryFactory.itemKeys().count() != 1)
  103. {
  104. std::cerr << "ctkAbstractLibraryFactory::registerLibrary() failed"
  105. << libraryFactory.itemKeys().count() << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. if (QFileInfo(libraryFactory.path("lib")) != file)
  109. {
  110. std::cerr << "ctkAbstractLibraryFactory::registerLibrary() failed"
  111. << libraryFactory.path("lib").toStdString() << std::endl;
  112. return EXIT_FAILURE;
  113. }
  114. ctkDummyLibrary* library = libraryFactory.instantiate("lib");
  115. if (library == 0)
  116. {
  117. std::cerr << "ctkAbstractLibraryFactory::instantiate() failed" << std::endl;
  118. return EXIT_FAILURE;
  119. }
  120. libraryFactory.uninstantiate("lib");
  121. return EXIT_SUCCESS;
  122. }