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 
27 #include <SimoxUtility/algorithm/string/string_tools.h>
28 
29 #include <boost/format.hpp>
30 
31 #include <set>
32 
33 
34 using namespace armarx;
35 
36 CppClass::CppClass(const std::vector<std::string>& namespaces, const std::string& name, const std::string& templates)
37  : MetaClass(name)
38 {
39  this->namespaces = namespaces;
40  this->templates = templates;
41 }
42 
43 void CppClass::write(const MetaWriterPtr& writer)
44 {
45  CppWriterPtr w = std::dynamic_pointer_cast<CppWriter>(writer);
46  writeCpp(w);
47 }
48 
49 void CppClass::WriteCpp(const std::vector<CppClassPtr>& classes, const CppWriterPtr& writer)
50 {
51  for (const auto& meta : classes)
52  {
53  meta->writeCpp(writer);
54  }
55 }
56 
57 void CppClass::writeCpp(const CppWriterPtr& writer)
58 {
59 
60  if (includes.size() > 0)
61  {
62  for (const auto& incl : includes)
63  {
64  writer->header.lineCheckDuplicate(boost::str(boost::format("#include %s") % incl));
65  }
66  writer->header.line();
67  }
68 
69  for (const auto& ns : namespaces)
70  {
71  writer->body.startBlock(std::string("namespace ") + ns);
72  }
73 
74  if (!docString.empty())
75  {
76  const std::string preSpace = " ";
77  writer->body.line("/**");
78  std::vector<std::string> docLines = simox::alg::split(docString, "\n");
79  std::vector<std::string> blockLines;
80  writer->ConvertToTextWithMaxWidth(80, docLines, blockLines);
81 
82  for (auto& line : blockLines)
83  {
84  writer->body.line(preSpace + line);
85  }
86 
87  writer->body.line(" */");
88  }
89  if (!templates.empty())
90  {
91  writer->body.line(templates);
92  }
93 
94  writer->body.line(std::string("class ") + name);
95 
96  for (size_t i = 0; i < inherit.size(); i++)
97  {
98  writer->body.line(boost::str(boost::format("%s%s%s") % (i == 0 ? ": " : " ") % inherit.at(i) % (i < inherit.size() - 1 ? "," : "")), 1);
99  }
100 
101  writer->body.startBlock();
102 
103  if (innerEnums.size() > 0)
104  {
105  writer->body.line("public:", -1);
106  writer->body.line();
107 
108  for (const auto& inner : innerEnums)
109  {
110  inner->writeCpp(writer);
111  writer->body.line();
112  }
113  }
114 
115  if (innerClasses.size() > 0)
116  {
117  writer->body.line("protected:", -1);
118  writer->body.line();
119 
120  for (const auto& inner : innerClasses)
121  {
122  inner->writeCpp(writer);
123  writer->body.line();
124  }
125  }
126 
127  if (privateFields.size() > 0)
128  {
129  writer->body.line("private:", -1);
130  writer->body.line();
131 
132  for (const auto& field : privateFields)
133  {
134  field->writeCpp(writer);
135  writer->body.line();
136  }
137  }
138 
139  if (protectedFields.size() > 0)
140  {
141  writer->body.line("protected:", -1);
142  writer->body.line();
143 
144  for (const auto& field : protectedFields)
145  {
146  field->writeCpp(writer);
147  writer->body.line();
148  }
149  }
150 
151  if (publicFields.size() > 0)
152  {
153  writer->body.line("public:", -1);
154  writer->body.line();
155 
156  for (const auto& field : publicFields)
157  {
158  field->writeCpp(writer);
159  writer->body.line();
160  }
161  }
162 
163  if (ctors.size() > 0)
164  {
165  writer->body.line("public:", -1);
166  writer->body.line();
167 
168  for (const auto& ctor : ctors)
169  {
170  ctor->writeCpp(writer);
171  writer->body.line();
172  }
173  }
174 
175  if (publicMethods.size() > 0)
176  {
177  writer->body.line("public:", -1);
178  writer->body.line();
179 
180  for (const auto& method : publicMethods)
181  {
182  method->writeCpp(writer);
183  writer->body.line();
184  }
185  }
186 
187  writer->body.endBlock("; // class " + name); // end of class
188 
189  for (std::vector<std::string>::reverse_iterator it = namespaces.rbegin(); it != namespaces.rend(); it++)
190  {
191  writer->body.endBlockComment(std::string("namespace ") + *it);
192  }
193 }
194 
196 {
197  ARMARX_CHECK(method);
198  publicMethods.push_back(method);
199 }
200 
201 CppMethodPtr CppClass::addPublicMethod(const std::string& header, const std::string& doc, const bool enforceBlockGeneration)
202 {
203  CppMethodPtr method(new CppMethod(header, doc, enforceBlockGeneration));
204  publicMethods.push_back(method);
205  return method;
206 }
207 
208 void CppClass::addCtor(const CppCtorPtr& ctor)
209 {
210  ctors.push_back(ctor);
211 }
212 
213 CppCtorPtr CppClass::addCtor(const std::string& arguments)
214 {
215  CppCtorPtr ctor(new CppCtor(boost::str(boost::format("%s(%s)") % name % arguments)));
216  ctors.push_back(ctor);
217  return ctor;
218 }
219 
220 CppCtorPtr CppClass::addCtor(const std::string& arguments, const CppBlockPtr& b /* may be null */)
221 {
222  CppCtorPtr ctor(new CppCtor(boost::str(boost::format("%s(%s)") % name % arguments), b));
223  ctors.push_back(ctor);
224  return ctor;
225 }
226 
228 {
229  ARMARX_CHECK(field);
230  privateFields.push_back(field);
231 }
232 
234 {
235  ARMARX_CHECK(field);
236  protectedFields.push_back(field);
237 }
238 
240 {
241  ARMARX_CHECK(field);
242  publicFields.push_back(field);
243 }
244 
245 CppClassPtr CppClass::addInnerClass(const std::string& name)
246 {
247  CppClassPtr innerClass(new CppClass(std::vector<std::string>(), name));
248  innerClasses.push_back(innerClass);
249  return innerClass;
250 }
251 
253 {
254  innerClasses.push_back(inner);
255 }
256 
258 {
259  ARMARX_CHECK(inner);
260  ARMARX_CHECK(inner->getNamespaces().empty());
261  innerEnums.push_back(inner);
262 }
263 
264 CppEnumPtr CppClass::addInnerEnum(const std::string& name)
265 {
266  CppEnumPtr innerEnum(new CppEnum({}, name));
267  innerEnums.push_back(innerEnum);
268  return innerEnum;
269 }
270 
271 void CppClass::addInherit(const std::string& inherit)
272 {
273  this->inherit.push_back(inherit);
274 }
275 
276 void CppClass::addInclude(const std::string include)
277 {
278  includes.push_back(include);
279 }
280 
281 bool CppClass::hasInclude(const std::string include)
282 {
283  return (std::find(includes.begin(), includes.end(), include) != includes.end());
284 
285 }
286 
287 void CppClass::setTemplates(const std::string& s)
288 {
289  templates = s;
290 }
armarx::CppClass::writeCpp
void writeCpp(const CppWriterPtr &writer)
Definition: CppClass.cpp:57
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::CppCtorPtr
std::shared_ptr< CppCtor > CppCtorPtr
Definition: CppCtor.h:31
armarx::MetaClass::docString
std::string docString
Definition: MetaClass.h:49
armarx::CppClass::write
virtual void write(const MetaWriterPtr &writer) override
Definition: CppClass.cpp:43
armarx::CppClassPtr
std::shared_ptr< CppClass > CppClassPtr
Definition: CppClass.h:36
armarx::CppBlockPtr
std::shared_ptr< CppBlock > CppBlockPtr
Definition: CppBlock.h:35
armarx::CppClass::addPublicMethod
void addPublicMethod(const CppMethodPtr method)
Definition: CppClass.cpp:195
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::CppClass::addInherit
void addInherit(const std::string &inherit)
Definition: CppClass.cpp:271
armarx::CppFieldPtr
std::shared_ptr< CppField > CppFieldPtr
Definition: CppField.h:34
armarx::CppEnumPtr
std::shared_ptr< CppEnum > CppEnumPtr
Definition: CppEnum.h:55
armarx::MetaClass::name
std::string name
Definition: MetaClass.h:48
armarx::CppClass::CppClass
CppClass(const std::vector< std::string > &namespaces, const std::string &name, const std::string &templates="")
Definition: CppClass.cpp:36
CppClass.h
armarx::CppClass::hasInclude
bool hasInclude(const std::string include)
Definition: CppClass.cpp:281
armarx::CppClass::addInnerClass
void addInnerClass(const CppClassPtr inner)
Definition: CppClass.cpp:252
armarx::CppMethodPtr
std::shared_ptr< CppMethod > CppMethodPtr
Definition: CppMethod.h:32
armarx::CppCtor
Definition: CppCtor.h:33
armarx::CppClass::setTemplates
void setTemplates(const std::string &)
Definition: CppClass.cpp:287
armarx::MetaClass
Definition: MetaClass.h:35
armarx::CppClass::WriteCpp
static void WriteCpp(const std::vector< CppClassPtr > &classes, const CppWriterPtr &writer)
Definition: CppClass.cpp:49
armarx::MetaWriterPtr
std::shared_ptr< MetaWriter > MetaWriterPtr
Definition: MetaWriter.h:37
ExpressionException.h
armarx::CppClass::addPublicField
void addPublicField(const CppFieldPtr &field)
Definition: CppClass.cpp:239
armarx::CppClass::addPrivateField
void addPrivateField(const CppFieldPtr &field)
Definition: CppClass.cpp:227
armarx::CppEnum
Definition: CppEnum.h:57
armarx::CppClass::addInclude
void addInclude(const std::string include)
Definition: CppClass.cpp:276
armarx::CppWriterPtr
std::shared_ptr< CppWriter > CppWriterPtr
Definition: CppWriter.h:35
armarx::CppMethod
Definition: CppMethod.h:34
armarx::CppClass::addInnerEnum
void addInnerEnum(const CppEnumPtr inner)
Definition: CppClass.cpp:257
armarx::CppClass::addCtor
void addCtor(const CppCtorPtr &)
Definition: CppClass.cpp:208
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::CppClass::addProtectedField
void addProtectedField(const CppFieldPtr &field)
Definition: CppClass.cpp:233
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36