ctkPluginGeneratorCppTemplate.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 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 "ctkPluginGeneratorCppTemplate.h"
  16. #include "ctkPluginGeneratorConstants.h"
  17. #include <QTextStream>
  18. const QString ctkPluginGeneratorCppTemplate::CPP_CLASSNAME_MARKER = "cpp_classname";
  19. const QString ctkPluginGeneratorCppTemplate::CPP_INCLUDES_MARKER = "cpp_includes";
  20. const QString ctkPluginGeneratorCppTemplate::CPP_GLOBAL_MARKER = "cpp_globals";
  21. const QString ctkPluginGeneratorCppTemplate::CPP_METHODS_MARKER = "cpp_methods";
  22. const QString ctkPluginGeneratorCppTemplate::CPP_CONSTRUCTOR_INITLIST_MARKER = "cpp_constructor_initlist";
  23. const QString ctkPluginGeneratorCppTemplate::CPP_CONSTRUCTOR_BODY_MARKER = "cpp_constructor_body";
  24. const QString ctkPluginGeneratorCppTemplate::CPP_DESTRUCTOR_BODY_MARKER = "cpp_destructor_body";
  25. ctkPluginGeneratorCppTemplate::ctkPluginGeneratorCppTemplate(
  26. const QString& name, ctkPluginGeneratorAbstractTemplate* parent)
  27. : ctkPluginGeneratorAbstractTemplate(name, parent)
  28. {
  29. }
  30. QStringList ctkPluginGeneratorCppTemplate::getMarkers() const
  31. {
  32. QStringList markers = ctkPluginGeneratorAbstractTemplate::getMarkers();
  33. markers << CPP_CLASSNAME_MARKER
  34. << CPP_INCLUDES_MARKER
  35. << CPP_GLOBAL_MARKER
  36. << CPP_METHODS_MARKER
  37. << CPP_CONSTRUCTOR_INITLIST_MARKER
  38. << CPP_CONSTRUCTOR_BODY_MARKER;
  39. return markers;
  40. }
  41. QString ctkPluginGeneratorCppTemplate::generateContent()
  42. {
  43. QString content;
  44. QTextStream stream(&content);
  45. // get the namespace
  46. QString namespaceToken;
  47. QStringList namespc = this->getContent(ctkPluginGeneratorConstants::PLUGIN_NAMESPACE_MARKER);
  48. if (!namespc.isEmpty() && !namespc.back().isEmpty())
  49. {
  50. namespaceToken = namespc.back();
  51. }
  52. // License header
  53. QStringList licenseText = this->getContent(ctkPluginGeneratorConstants::PLUGIN_LICENSE_MARKER);
  54. if (!licenseText.isEmpty() && !licenseText.back().isEmpty())
  55. {
  56. stream << licenseText.back() << "\n\n";
  57. }
  58. // include statements
  59. QStringList includes = this->getContent(CPP_INCLUDES_MARKER);
  60. if (!includes.isEmpty())
  61. {
  62. foreach(QString includeStatement, includes)
  63. {
  64. stream << includeStatement << "\n";
  65. }
  66. stream << "\n";
  67. }
  68. // namespace
  69. if (!namespaceToken.isEmpty())
  70. {
  71. stream << "namespace " << namespaceToken << " {\n\n";
  72. }
  73. // global definitions
  74. QStringList globals = this->getContent(CPP_GLOBAL_MARKER);
  75. if (!globals.isEmpty())
  76. {
  77. foreach (QString global, globals)
  78. {
  79. stream << global << "\n";
  80. }
  81. stream << "\n";
  82. }
  83. // method definitions
  84. QStringList methods = this->getContent(CPP_METHODS_MARKER);
  85. if (!methods.isEmpty())
  86. {
  87. foreach (QString method, methods)
  88. {
  89. stream << method << "\n\n";
  90. }
  91. }
  92. // end namespace
  93. if (!namespaceToken.isEmpty())
  94. {
  95. stream << "} // end namespace " << namespaceToken << "\n";
  96. }
  97. return content;
  98. }
  99. QString ctkPluginGeneratorCppTemplate::getClassNameToken() const
  100. {
  101. QString classNameToken;
  102. QStringList classname = this->getContent(CPP_CLASSNAME_MARKER);
  103. if (!classname.isEmpty() && !classname.back().isEmpty())
  104. {
  105. classNameToken = classname.back();
  106. }
  107. else
  108. {
  109. // use the filename without ending
  110. classNameToken = getFilename().left(getFilename().lastIndexOf("."));
  111. }
  112. return classNameToken;
  113. }