ctkPluginGeneratorHeaderTemplate.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "ctkPluginGeneratorHeaderTemplate.h"
  16. #include "ctkPluginGeneratorConstants.h"
  17. #include <QTextStream>
  18. const QString ctkPluginGeneratorHeaderTemplate::H_CLASSNAME_MARKER = "h_classname";
  19. const QString ctkPluginGeneratorHeaderTemplate::H_INCLUDES_MARKER = "h_includes";
  20. const QString ctkPluginGeneratorHeaderTemplate::H_FORWARD_DECL_MARKER = "h_forward_decl";
  21. const QString ctkPluginGeneratorHeaderTemplate::H_FORWARD_DECL_NAMESPACE_MARKER = "h_forward_decl_namespace";
  22. const QString ctkPluginGeneratorHeaderTemplate::H_SUPERCLASSES_MARKER = "h_superclasses";
  23. const QString ctkPluginGeneratorHeaderTemplate::H_DEFAULT_ACCESS_MARKER = "h_default_access";
  24. const QString ctkPluginGeneratorHeaderTemplate::H_PUBLIC_MARKER = "h_public";
  25. const QString ctkPluginGeneratorHeaderTemplate::H_PROTECTED_MARKER = "h_protected";
  26. const QString ctkPluginGeneratorHeaderTemplate::H_PRIVATE_MARKER = "h_private";
  27. ctkPluginGeneratorHeaderTemplate::ctkPluginGeneratorHeaderTemplate(const QString& name, ctkPluginGeneratorAbstractTemplate* parent)
  28. : ctkPluginGeneratorAbstractTemplate(name, parent)
  29. {
  30. }
  31. QStringList ctkPluginGeneratorHeaderTemplate::getMarkers() const
  32. {
  33. QStringList markers = ctkPluginGeneratorAbstractTemplate::getMarkers();
  34. markers << H_CLASSNAME_MARKER
  35. << H_INCLUDES_MARKER
  36. << H_FORWARD_DECL_MARKER
  37. << H_FORWARD_DECL_NAMESPACE_MARKER
  38. << H_SUPERCLASSES_MARKER
  39. << H_DEFAULT_ACCESS_MARKER
  40. << H_PUBLIC_MARKER
  41. << H_PROTECTED_MARKER
  42. << H_PRIVATE_MARKER;
  43. return markers;
  44. }
  45. QString ctkPluginGeneratorHeaderTemplate::generateContent()
  46. {
  47. QString content;
  48. QTextStream stream(&content);
  49. // get the namespace
  50. QString namespaceToken;
  51. QStringList namespc = this->getContent(ctkPluginGeneratorConstants::PLUGIN_NAMESPACE_MARKER);
  52. if (!namespc.isEmpty() && !namespc.back().isEmpty())
  53. {
  54. namespaceToken = namespc.back();
  55. }
  56. // get the classname
  57. QString classNameToken;
  58. QStringList classname = this->getContent(H_CLASSNAME_MARKER);
  59. if (!classname.isEmpty() && !classname.back().isEmpty())
  60. {
  61. classNameToken = classname.back();
  62. }
  63. else
  64. {
  65. // use the filename without ending
  66. classNameToken = getFilename().left(getFilename().lastIndexOf("."));
  67. }
  68. // License header
  69. QStringList licenseText = this->getContent(ctkPluginGeneratorConstants::PLUGIN_LICENSE_MARKER);
  70. if (!licenseText.isEmpty() && !licenseText.back().isEmpty())
  71. {
  72. stream << licenseText.back() << "\n\n";
  73. }
  74. // include guard
  75. QString includeGuardToken;
  76. if (!namespaceToken.isEmpty())
  77. {
  78. includeGuardToken += namespaceToken.toUpper() + "_";
  79. }
  80. includeGuardToken += getFilename().toUpper().replace(".", "_");
  81. stream << "#ifndef " << includeGuardToken << "\n";
  82. stream << "#define " << includeGuardToken << "\n\n";
  83. // include statements
  84. QStringList includes = this->getContent(H_INCLUDES_MARKER);
  85. if (!includes.isEmpty())
  86. {
  87. foreach(QString includeStatement, includes)
  88. {
  89. stream << includeStatement << "\n";
  90. }
  91. stream << "\n";
  92. }
  93. // forward declarations
  94. QStringList forwards = this->getContent(H_FORWARD_DECL_MARKER);
  95. if (!forwards.isEmpty())
  96. {
  97. foreach(QString forward, forwards)
  98. {
  99. stream << forward << "\n";
  100. }
  101. stream << "\n";
  102. }
  103. // namespace
  104. if (!namespaceToken.isEmpty())
  105. {
  106. stream << "namespace " << namespaceToken << " {\n\n";
  107. }
  108. // forward declarations inside namespace
  109. QStringList forwardsN = this->getContent(H_FORWARD_DECL_NAMESPACE_MARKER);
  110. if (!forwardsN.isEmpty())
  111. {
  112. foreach(QString forward, forwardsN)
  113. {
  114. stream << forward << "\n";
  115. }
  116. stream << "\n";
  117. }
  118. // class declaration
  119. stream << "class ";
  120. QStringList exportMacro = this->getContent(ctkPluginGeneratorConstants::PLUGIN_EXPORTMACRO_MARKER);
  121. if (!exportMacro.isEmpty() && !exportMacro.back().isEmpty())
  122. {
  123. stream << exportMacro.back() << " ";
  124. }
  125. stream << classNameToken;
  126. QStringList superclasses = this->getContent(H_SUPERCLASSES_MARKER);
  127. if (!superclasses.isEmpty())
  128. {
  129. stream << " :\n";
  130. int i = 1;
  131. foreach(QString superclass, superclasses)
  132. {
  133. stream << " " << superclass;
  134. if (i < superclasses.size()) stream << ",";
  135. }
  136. }
  137. stream << "\n{\n";
  138. // method and member declarations
  139. // default access
  140. QStringList defaultAccess = this->getContent(H_DEFAULT_ACCESS_MARKER);
  141. if (!defaultAccess.isEmpty())
  142. {
  143. foreach(QString entry, defaultAccess)
  144. {
  145. stream << " " << entry.replace("\n", "\n ") << "\n";
  146. }
  147. stream << "\n";
  148. }
  149. // public access
  150. QStringList publicAccess = this->getContent(H_PUBLIC_MARKER);
  151. if (!publicAccess.isEmpty())
  152. {
  153. stream << "public:\n\n";
  154. foreach(QString entry, publicAccess)
  155. {
  156. stream << " " << entry.replace("\n", "\n ") << "\n\n";
  157. }
  158. stream << "\n";
  159. }
  160. // protected access
  161. QStringList protectedAccess = this->getContent(H_PROTECTED_MARKER);
  162. if (!protectedAccess.isEmpty())
  163. {
  164. stream << "protected:\n\n";
  165. foreach(QString entry, protectedAccess)
  166. {
  167. stream << " " << entry.replace("\n", "\n ") << "\n\n";
  168. }
  169. stream << "\n";
  170. }
  171. // private access
  172. QStringList privateAccess = this->getContent(H_PRIVATE_MARKER);
  173. if (!privateAccess.isEmpty())
  174. {
  175. stream << "private:\n\n";
  176. foreach(QString entry, privateAccess)
  177. {
  178. stream << " " << entry.replace("\n", "\n ") << "\n\n";
  179. }
  180. stream << "\n";
  181. }
  182. // end class declaration
  183. stream << "}; // " << classNameToken << "\n\n";
  184. // end namespace
  185. if (!namespaceToken.isEmpty())
  186. {
  187. stream << "} // end namespace " << namespaceToken << "\n";
  188. }
  189. // end include guard
  190. stream << "#endif // " << includeGuardToken << "\n";
  191. return content;
  192. }