1 /*=============================================================================
2 
3   Library: CTK
4 
5   Copyright (c) German Cancer Research Center,
6     Division of Medical and Biological Informatics
7 
8   Licensed under the Apache License, Version 2.0 (the "License");
9   you may not use this file except in compliance with the License.
10   You may obtain a copy of the License at
11 
12     http://www.apache.org/licenses/LICENSE-2.0
13 
14   Unless required by applicable law or agreed to in writing, software
15   distributed under the License is distributed on an "AS IS" BASIS,
16   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   See the License for the specific language governing permissions and
18   limitations under the License.
19 
20 =============================================================================*/
21 
22 
23 #include "ctkPluginGeneratorCppTemplate.h"
24 
25 #include "ctkPluginGeneratorConstants.h"
26 
27 #include <QTextStream>
28 
29 const QString ctkPluginGeneratorCppTemplate::CPP_CLASSNAME_MARKER = "cpp_classname";
30 const QString ctkPluginGeneratorCppTemplate::CPP_INCLUDES_MARKER = "cpp_includes";
31 const QString ctkPluginGeneratorCppTemplate::CPP_GLOBAL_MARKER = "cpp_globals";
32 const QString ctkPluginGeneratorCppTemplate::CPP_METHODS_MARKER = "cpp_methods";
33 const QString ctkPluginGeneratorCppTemplate::CPP_CONSTRUCTOR_INITLIST_MARKER = "cpp_constructor_initlist";
34 const QString ctkPluginGeneratorCppTemplate::CPP_CONSTRUCTOR_BODY_MARKER = "cpp_constructor_body";
35 const QString ctkPluginGeneratorCppTemplate::CPP_DESTRUCTOR_BODY_MARKER = "cpp_destructor_body";
36 
ctkPluginGeneratorCppTemplate(const QString & name,ctkPluginGeneratorAbstractTemplate * parent)37 ctkPluginGeneratorCppTemplate::ctkPluginGeneratorCppTemplate(
38     const QString& name, ctkPluginGeneratorAbstractTemplate* parent)
39   : ctkPluginGeneratorAbstractTemplate(name, parent)
40 {
41 
42 }
43 
getMarkers() const44 QStringList ctkPluginGeneratorCppTemplate::getMarkers() const
45 {
46   QStringList markers = ctkPluginGeneratorAbstractTemplate::getMarkers();
47 
48   markers << CPP_CLASSNAME_MARKER
49       << CPP_INCLUDES_MARKER
50       << CPP_GLOBAL_MARKER
51       << CPP_METHODS_MARKER
52       << CPP_CONSTRUCTOR_INITLIST_MARKER
53       << CPP_CONSTRUCTOR_BODY_MARKER;
54 
55   return markers;
56 }
57 
generateContent()58 QString ctkPluginGeneratorCppTemplate::generateContent()
59 {
60   QString content;
61   QTextStream stream(&content);
62 
63   // get the namespace
64   QString namespaceToken;
65   QStringList namespc = this->getContent(ctkPluginGeneratorConstants::PLUGIN_NAMESPACE_MARKER);
66   if (!namespc.isEmpty() && !namespc.back().isEmpty())
67   {
68     namespaceToken = namespc.back();
69   }
70 
71   // License header
72   QStringList licenseText = this->getContent(ctkPluginGeneratorConstants::PLUGIN_LICENSE_MARKER);
73   if (!licenseText.isEmpty() && !licenseText.back().isEmpty())
74   {
75     stream << licenseText.back() << "\n\n";
76   }
77 
78   // include statements
79   QStringList includes = this->getContent(CPP_INCLUDES_MARKER);
80   if (!includes.isEmpty())
81   {
82     foreach(QString includeStatement, includes)
83     {
84       stream << includeStatement << "\n";
85     }
86     stream << "\n";
87   }
88 
89   // namespace
90   if (!namespaceToken.isEmpty())
91   {
92     stream << "namespace " << namespaceToken << " {\n\n";
93   }
94 
95   // global definitions
96   QStringList globals = this->getContent(CPP_GLOBAL_MARKER);
97   if (!globals.isEmpty())
98   {
99     foreach (QString global, globals)
100     {
101       stream << global << "\n";
102     }
103     stream << "\n";
104   }
105 
106   // method definitions
107   QStringList methods = this->getContent(CPP_METHODS_MARKER);
108   if (!methods.isEmpty())
109   {
110     foreach (QString method, methods)
111     {
112       stream << method << "\n\n";
113     }
114   }
115 
116   // end namespace
117   if (!namespaceToken.isEmpty())
118   {
119     stream << "} // end namespace " << namespaceToken << "\n";
120   }
121 
122   return content;
123 }
124 
getClassNameToken() const125 QString ctkPluginGeneratorCppTemplate::getClassNameToken() const
126 {
127   QString classNameToken;
128   QStringList classname = this->getContent(CPP_CLASSNAME_MARKER);
129   if (!classname.isEmpty() && !classname.back().isEmpty())
130   {
131     classNameToken = classname.back();
132   }
133   else
134   {
135     // use the filename without ending
136     classNameToken = getFilename().left(getFilename().lastIndexOf("."));
137   }
138 
139   return classNameToken;
140 }
141