ctkPluginGeneratorCppPluginActivator.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginGeneratorCppPluginActivator.h"
  16. #include "ctkPluginGeneratorConstants.h"
  17. #include <QTextStream>
  18. const QString ctkPluginGeneratorCppPluginActivator::PLUGINACTIVATOR_START_MARKER = "pluginactivator_start";
  19. const QString ctkPluginGeneratorCppPluginActivator::PLUGINACTIVATOR_STOP_MARKER = "pluginactivator_stop";
  20. ctkPluginGeneratorCppPluginActivator::ctkPluginGeneratorCppPluginActivator(ctkPluginGeneratorAbstractTemplate* parent)
  21. : ctkPluginGeneratorCppTemplate(ctkPluginGeneratorConstants::TEMPLATE_PLUGINACTIVATOR_CPP, parent)
  22. {
  23. }
  24. QString ctkPluginGeneratorCppPluginActivator::generateContent()
  25. {
  26. addContent(CPP_INCLUDES_MARKER, "#include <QtPlugin>");
  27. QString constructorContent;
  28. QTextStream constructor(&constructorContent);
  29. QStringList constructorInit = getContent(CPP_CONSTRUCTOR_INITLIST_MARKER);
  30. QStringList constructorBody = getContent(CPP_CONSTRUCTOR_BODY_MARKER);
  31. if (!constructorInit.isEmpty() || !constructorBody.isEmpty())
  32. {
  33. constructor << this->getClassNameToken() << "::" << this->getClassNameToken() << "()\n";
  34. if (!constructorInit.isEmpty())
  35. {
  36. constructor << " : ";
  37. int i = 1;
  38. foreach(QString initToken, constructorInit)
  39. {
  40. constructor << initToken;
  41. if (i < constructorInit.size()) constructor << ", ";
  42. ++i;
  43. }
  44. constructor << "\n";
  45. }
  46. constructor << "{\n";
  47. if (!constructorBody.isEmpty())
  48. {
  49. int i = 1;
  50. foreach(QString bodyToken, constructorBody)
  51. {
  52. constructor << " " << bodyToken.replace("\n", "\n ") << "\n";
  53. if (i < constructorBody.size()) constructor << "\n";
  54. ++i;
  55. }
  56. }
  57. constructor << "}";
  58. }
  59. QString destructorContent;
  60. QTextStream destructor(&destructorContent);
  61. QStringList destructorBody = getContent(CPP_DESTRUCTOR_BODY_MARKER);
  62. if (!destructorBody.isEmpty())
  63. {
  64. destructor << this->getClassNameToken() << "::~" << this->getClassNameToken() << "()\n"
  65. << "{\n";
  66. int i = 1;
  67. foreach(QString bodyToken, destructorBody)
  68. {
  69. if (!bodyToken.isEmpty())
  70. {
  71. destructor << "\n";
  72. }
  73. else
  74. {
  75. destructor << " " << bodyToken.replace("\n", "\n ") << "\n";
  76. if (i < destructorBody.size()) destructor << "\n";
  77. }
  78. ++i;
  79. }
  80. destructor << "}";
  81. }
  82. QString startMethodContent;
  83. QTextStream startMethod(&startMethodContent);
  84. startMethod << "void " << this->getClassNameToken() << "::start(ctkPluginContext* context)\n"
  85. << "{\n";
  86. QStringList startContent = getContent(PLUGINACTIVATOR_START_MARKER);
  87. if (!startContent.isEmpty())
  88. {
  89. int i = 1;
  90. foreach(QString block, startContent)
  91. {
  92. startMethod << " " << block.replace("\n", "\n ") << "\n";
  93. if (i < startContent.size()) startMethod << "\n";
  94. ++i;
  95. }
  96. }
  97. else
  98. {
  99. startMethod << " Q_UNUSED(context)\n";
  100. }
  101. startMethod << "}";
  102. QString stopMethodContent;
  103. QTextStream stopMethod(&stopMethodContent);
  104. stopMethod << "void " << this->getClassNameToken() << "::stop(ctkPluginContext* context)\n"
  105. << "{\n";
  106. QStringList stopContent = getContent(PLUGINACTIVATOR_STOP_MARKER);
  107. if (!stopContent.isEmpty())
  108. {
  109. int i = 1;
  110. foreach(QString block, stopContent)
  111. {
  112. stopMethod << " " << block.replace("\n", "\n ") << "\n";
  113. if (i < stopContent.size()) stopMethod << "\n";
  114. }
  115. ++i;
  116. }
  117. else
  118. {
  119. stopMethod << " Q_UNUSED(context)\n";
  120. }
  121. stopMethod << "}";
  122. addContent(CPP_METHODS_MARKER, stopMethodContent, PREPEND);
  123. addContent(CPP_METHODS_MARKER, startMethodContent, PREPEND);
  124. if (!destructorContent.isEmpty())
  125. {
  126. addContent(CPP_METHODS_MARKER, destructorContent, PREPEND);
  127. }
  128. if (!constructorContent.isEmpty())
  129. {
  130. addContent(CPP_METHODS_MARKER, constructorContent, PREPEND);
  131. }
  132. QString exportPlugin = "Q_EXPORT_PLUGIN2(" + getSymbolicName() + ", " + getClassNameToken() + ")\n";
  133. addContent(CPP_METHODS_MARKER, exportPlugin);
  134. return ctkPluginGeneratorCppTemplate::generateContent();
  135. }
  136. QStringList ctkPluginGeneratorCppPluginActivator::getMarkers() const
  137. {
  138. QStringList markers;
  139. markers << PLUGINACTIVATOR_START_MARKER
  140. << PLUGINACTIVATOR_STOP_MARKER;
  141. return markers;
  142. }