CppClass.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "CppClass.h"
25
26#include <set>
27
28#include <boost/format.hpp>
29
30#include <SimoxUtility/algorithm/string/string_tools.h>
31
33
34
35using namespace armarx;
36
37CppClass::CppClass(const std::vector<std::string>& namespaces,
38 const std::string& name,
39 const std::string& templates) :
41{
42 this->namespaces = namespaces;
43 this->templates = templates;
44}
45
46void
48{
49 CppWriterPtr w = std::dynamic_pointer_cast<CppWriter>(writer);
50 writeCpp(w);
51}
52
53void
54CppClass::WriteCpp(const std::vector<CppClassPtr>& classes, const CppWriterPtr& writer)
55{
56 for (const auto& meta : classes)
57 {
58 meta->writeCpp(writer);
59 }
60}
61
62void
64{
65
66 if (includes.size() > 0)
67 {
68 for (const auto& incl : includes)
69 {
70 writer->header.lineCheckDuplicate(boost::str(boost::format("#include %s") % incl));
71 }
72 writer->header.line();
73 }
74
75 for (const auto& ns : namespaces)
76 {
77 writer->body.startBlock(std::string("namespace ") + ns);
78 }
79
80 if (!docString.empty())
81 {
82 const std::string preSpace = " ";
83 writer->body.line("/**");
84 std::vector<std::string> docLines = simox::alg::split(docString, "\n");
85 std::vector<std::string> blockLines;
86 writer->ConvertToTextWithMaxWidth(80, docLines, blockLines);
87
88 for (auto& line : blockLines)
89 {
90 writer->body.line(preSpace + line);
91 }
92
93 writer->body.line(" */");
94 }
95 if (!templates.empty())
96 {
97 writer->body.line(templates);
98 }
99
100 writer->body.line(std::string("class ") + name);
101
102 for (size_t i = 0; i < inherit.size(); i++)
103 {
104 writer->body.line(boost::str(boost::format("%s%s%s") % (i == 0 ? ": " : " ") %
105 inherit.at(i) % (i < inherit.size() - 1 ? "," : "")),
106 1);
107 }
108
109 writer->body.startBlock();
110
111 if (innerEnums.size() > 0)
112 {
113 writer->body.line("public:", -1);
114 writer->body.line();
115
116 for (const auto& inner : innerEnums)
117 {
118 inner->writeCpp(writer);
119 writer->body.line();
120 }
121 }
122
123 if (innerClasses.size() > 0)
124 {
125 writer->body.line("protected:", -1);
126 writer->body.line();
127
128 for (const auto& inner : innerClasses)
129 {
130 inner->writeCpp(writer);
131 writer->body.line();
132 }
133 }
134
135 if (privateFields.size() > 0)
136 {
137 writer->body.line("private:", -1);
138 writer->body.line();
139
140 for (const auto& field : privateFields)
141 {
142 field->writeCpp(writer);
143 writer->body.line();
144 }
145 }
146
147 if (protectedFields.size() > 0)
148 {
149 writer->body.line("protected:", -1);
150 writer->body.line();
151
152 for (const auto& field : protectedFields)
153 {
154 field->writeCpp(writer);
155 writer->body.line();
156 }
157 }
158
159 if (publicFields.size() > 0)
160 {
161 writer->body.line("public:", -1);
162 writer->body.line();
163
164 for (const auto& field : publicFields)
165 {
166 field->writeCpp(writer);
167 writer->body.line();
168 }
169 }
170
171 if (ctors.size() > 0)
172 {
173 writer->body.line("public:", -1);
174 writer->body.line();
175
176 for (const auto& ctor : ctors)
177 {
178 ctor->writeCpp(writer);
179 writer->body.line();
180 }
181 }
182
183 if (publicMethods.size() > 0)
184 {
185 writer->body.line("public:", -1);
186 writer->body.line();
187
188 for (const auto& method : publicMethods)
189 {
190 method->writeCpp(writer);
191 writer->body.line();
192 }
193 }
194
195 writer->body.endBlock("; // class " + name); // end of class
196
197 for (std::vector<std::string>::reverse_iterator it = namespaces.rbegin();
198 it != namespaces.rend();
199 it++)
200 {
201 writer->body.endBlockComment(std::string("namespace ") + *it);
202 }
203}
204
205void
207{
208 ARMARX_CHECK(method);
209 publicMethods.push_back(method);
210}
211
213CppClass::addPublicMethod(const std::string& header,
214 const std::string& doc,
215 const bool enforceBlockGeneration)
216{
217 CppMethodPtr method(new CppMethod(header, doc, enforceBlockGeneration));
218 publicMethods.push_back(method);
219 return method;
220}
221
222void
224{
225 ctors.push_back(ctor);
226}
227
229CppClass::addCtor(const std::string& arguments)
230{
231 CppCtorPtr ctor(new CppCtor(boost::str(boost::format("%s(%s)") % name % arguments)));
232 ctors.push_back(ctor);
233 return ctor;
234}
235
237CppClass::addCtor(const std::string& arguments, const CppBlockPtr& b /* may be null */)
238{
239 CppCtorPtr ctor(new CppCtor(boost::str(boost::format("%s(%s)") % name % arguments), b));
240 ctors.push_back(ctor);
241 return ctor;
242}
243
244void
246{
247 ARMARX_CHECK(field);
248 privateFields.push_back(field);
249}
250
251void
253{
254 ARMARX_CHECK(field);
255 protectedFields.push_back(field);
256}
257
258void
260{
261 ARMARX_CHECK(field);
262 publicFields.push_back(field);
263}
264
266CppClass::addInnerClass(const std::string& name)
267{
268 CppClassPtr innerClass(new CppClass(std::vector<std::string>(), name));
269 innerClasses.push_back(innerClass);
270 return innerClass;
271}
272
273void
275{
276 innerClasses.push_back(inner);
277}
278
279void
281{
282 ARMARX_CHECK(inner);
283 ARMARX_CHECK(inner->getNamespaces().empty());
284 innerEnums.push_back(inner);
285}
286
288CppClass::addInnerEnum(const std::string& name)
289{
290 CppEnumPtr innerEnum(new CppEnum({}, name));
291 innerEnums.push_back(innerEnum);
292 return innerEnum;
293}
294
295void
296CppClass::addInherit(const std::string& inherit)
297{
298 this->inherit.push_back(inherit);
299}
300
301void
302CppClass::addInclude(const std::string include)
303{
304 includes.push_back(include);
305}
306
307bool
308CppClass::hasInclude(const std::string include)
309{
310 return (std::find(includes.begin(), includes.end(), include) != includes.end());
311}
312
313void
314CppClass::setTemplates(const std::string& s)
315{
316 templates = s;
317}
void addInnerEnum(const CppEnumPtr inner)
Definition CppClass.cpp:280
CppClass(const std::vector< std::string > &namespaces, const std::string &name, const std::string &templates="")
Definition CppClass.cpp:37
void setTemplates(const std::string &)
Definition CppClass.cpp:314
void addProtectedField(const CppFieldPtr &field)
Definition CppClass.cpp:252
void addPrivateField(const CppFieldPtr &field)
Definition CppClass.cpp:245
void addPublicMethod(const CppMethodPtr method)
Definition CppClass.cpp:206
void writeCpp(const CppWriterPtr &writer)
Definition CppClass.cpp:63
void addCtor(const CppCtorPtr &)
Definition CppClass.cpp:223
bool hasInclude(const std::string include)
Definition CppClass.cpp:308
void addInclude(const std::string include)
Definition CppClass.cpp:302
void addInnerClass(const CppClassPtr inner)
Definition CppClass.cpp:274
virtual void write(const MetaWriterPtr &writer) override
Definition CppClass.cpp:47
void addPublicField(const CppFieldPtr &field)
Definition CppClass.cpp:259
void addInherit(const std::string &inherit)
Definition CppClass.cpp:296
static void WriteCpp(const std::vector< CppClassPtr > &classes, const CppWriterPtr &writer)
Definition CppClass.cpp:54
MetaClass(const std::string &name)
Definition MetaClass.cpp:35
std::string name
Definition MetaClass.h:49
std::string docString
Definition MetaClass.h:50
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< CppClass > CppClassPtr
Definition CppClass.h:35
std::shared_ptr< MetaWriter > MetaWriterPtr
Definition MetaWriter.h:37
std::shared_ptr< CppEnum > CppEnumPtr
Definition CppEnum.h:54
std::shared_ptr< CppWriter > CppWriterPtr
Definition CppWriter.h:35
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
std::shared_ptr< CppCtor > CppCtorPtr
Definition CppCtor.h:31
std::shared_ptr< CppMethod > CppMethodPtr
Definition CppMethod.h:31
std::shared_ptr< CppField > CppFieldPtr
Definition CppField.h:33