• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

umbrello/umbrello

  • sources
  • kde-4.12
  • kdesdk
  • umbrello
  • umbrello
  • codegenerators
  • cpp
cppwriter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This program is free software; you can redistribute it and/or modify *
3  * it under the terms of the GNU General Public License as published by *
4  * the Free Software Foundation; either version 2 of the License, or *
5  * (at your option) any later version. *
6  * *
7  * copyright (C) 2003 Brian Thomas *
8  * <brian.thomas@gsfc.nasa.gov> *
9  * copyright (C) 2004-2013 Umbrello UML Modeller Authors *
10  * <umbrello-devel@kde.org> *
11  ***************************************************************************/
12 
13 // own header
14 #include "cppwriter.h"
15 
16 // app includes
17 #include "association.h"
18 #include "classifier.h"
19 #include "codegen_utils.h"
20 #include "debug_utils.h"
21 #include "model_utils.h"
22 #include "uml.h"
23 #include "umldoc.h"
24 #include "operation.h"
25 #include "template.h"
26 #include "umltemplatelist.h"
27 #include "umlclassifierlistitemlist.h"
28 #include "classifierlistitem.h"
29 #include "codegenerationpolicy.h"
30 
31 // qt includes
32 #include <QFile>
33 #include <QRegExp>
34 #include <QTextStream>
35 
36 // 3-14-2003: this code developed from the javawriter with parts of the
37 // original cppwriter by Luis De la Parra Blum
38 
42 CppWriter::CppWriter()
43 {
44  // Probably we could resolve this better through the use of templates,
45  // but it is a quick n dirty fix for the timebeing.. until codegeneration
46  // template system is in place.
47  // You can insert code here. 3 general variables exist: "%VARNAME%"
48  // allows you to specify where the vector variable should be in your code,
49  // and "%ITEMCLASS%", if needed, where the class of the item is declared.
50  VECTOR_METHOD_APPEND = "%VARNAME%.push_back(add_object);"; // for std::vector
51  VECTOR_METHOD_REMOVE = "int i, size = %VARNAME%.size();\nfor (i = 0; i < size; ++i) {\n\t%ITEMCLASS% item = %VARNAME%.at(i);\n\tif(item == remove_object) {\n\t\tvector<%ITEMCLASS%>::iterator it = %VARNAME%.begin() + i;\n\t\t%VARNAME%.erase(it);\n\t\treturn;\n\t}\n }"; // for std::vector
52  VECTOR_METHOD_INIT.clear(); // nothing to be done
53  /*
54  VECTOR_METHOD_APPEND = "%VARNAME%.append(&add_object);"; // Qt lib implementation
55  VECTOR_METHOD_REMOVE = "%VARNAME%.removeRef(&remove_object);"; // Qt lib implementation
56  VECTOR_METHOD_INIT = "%VARNAME%.setAutoDelete(false);"; // Qt library
57  */
58 
59  OBJECT_METHOD_INIT = "%VARNAME% = new %ITEMCLASS%();"; // Qt library
60 
61  // boolean config params
62  INLINE_ASSOCIATION_METHODS = false;
63 }
64 
68 CppWriter::~CppWriter()
69 {
70 }
71 
76 Uml::ProgrammingLanguage::Enum CppWriter::language() const
77 {
78  return Uml::ProgrammingLanguage::Cpp;
79 }
80 
84 CPPCodeGenerationPolicy *CppWriter::policyExt()
85 {
86  return static_cast<CPPCodeGenerationPolicy*>(UMLApp::app()->policyExt());
87 }
88 
93 void CppWriter::writeClass(UMLClassifier *c)
94 {
95  if (!c) {
96  uDebug() << "Cannot write class of NULL concept!";
97  return;
98  }
99 
100  QFile fileh, filecpp;
101 
102  // find an appropriate name for our file
103  fileName_ = findFileName(c, ".h");
104  if (fileName_.isEmpty()) {
105  emit codeGenerated(c, false);
106  return;
107  }
108 
109  className_ = cleanName(c->name());
110 
111  if (c->visibility() != Uml::Visibility::Implementation) {
112  if(!openFile(fileh, fileName_)) {
113  emit codeGenerated(c, false);
114  return;
115  }
116  // write Header file
117  writeHeaderFile(c, fileh);
118  fileh.close();
119  }
120 
121  // Determine whether the implementation file is required.
122  // (It is not required if the class is an enumeration.)
123  bool need_impl = true;
124  if (c->baseType() == UMLObject::ot_Enum) {
125  need_impl = false;
126  }
127  if (need_impl) {
128  fileName_.replace(QRegExp(".h$"), ".cpp");
129  if(!openFile(filecpp, fileName_)) {
130  emit codeGenerated(c, false);
131  return;
132  }
133  // write Source file
134  writeSourceFile(c, filecpp);
135  filecpp.close();
136  }
137 
138  emit codeGenerated(c, true);
139 }
140 
144 void CppWriter::writeHeaderFile (UMLClassifier *c, QFile &file)
145 {
146  // open stream for writing
147  QTextStream h (&file);
148 
149  // up the indent level to one
150  m_indentLevel = 1;
151 
152  // write header blurb
153  QString str = getHeadingFile(".h");
154  if (!str.isEmpty()) {
155  str.replace(QRegExp("%filename%"), fileName_ + ".h");
156  str.replace(QRegExp("%filepath%"), file.fileName());
157  h << str<< m_endl;
158  }
159 
160  // Write the hash define stuff to prevent multiple parsing/inclusion of header
161  QString hashDefine = className_.toUpper().simplified().replace(QRegExp(" "), "_");
162  writeBlankLine(h);
163  h << "#ifndef "<< hashDefine + "_H" << m_endl;
164  h << "#define "<< hashDefine + "_H" << m_endl;
165 
166  writeClassDecl(c, h);
167 
168  // last thing..close our hashdefine
169  h << m_endl << "#endif // " << hashDefine + "_H" << m_endl;
170 }
171 
172 void CppWriter::writeHeaderAccessorMethodDecl(UMLClassifier *c, Uml::Visibility::Enum permitScope, QTextStream &stream)
173 {
174  // attributes
175  writeHeaderAttributeAccessorMethods(c, permitScope, true, stream); // write static attributes first
176  writeHeaderAttributeAccessorMethods(c, permitScope, false, stream);
177 
178  // associations
179  writeAssociationMethods(c->getSpecificAssocs(Uml::AssociationType::Association), permitScope,
180  true, INLINE_ASSOCIATION_METHODS, true, c->id(), stream);
181  writeAssociationMethods(c->getUniAssociationToBeImplemented(), permitScope,
182  true, INLINE_ASSOCIATION_METHODS, true, c->id(), stream);
183  writeAssociationMethods(c->getAggregations(), permitScope,
184  true, INLINE_ASSOCIATION_METHODS, true, c->id(), stream);
185  writeAssociationMethods(c->getCompositions(), permitScope,
186  true, INLINE_ASSOCIATION_METHODS, false, c->id(), stream);
187 
188  writeBlankLine(stream);
189 }
190 
195 void CppWriter::writeHeaderFieldDecl(UMLClassifier *c, Uml::Visibility::Enum permitScope, QTextStream &stream)
196 {
197  // attributes
198  writeAttributeDecls(c, permitScope, true, stream); // write static attributes first
199  writeAttributeDecls(c, permitScope, false, stream);
200 
201  // associations
202  writeAssociationDecls(c->getSpecificAssocs(Uml::AssociationType::Association), permitScope, c->id(), stream);
203  writeAssociationDecls(c->getUniAssociationToBeImplemented(), permitScope, c->id(), stream);
204  writeAssociationDecls(c->getAggregations(), permitScope, c->id(), stream);
205  writeAssociationDecls(c->getCompositions(), permitScope, c->id(), stream);
206 }
207 
211 void CppWriter::writeSourceFile(UMLClassifier *c, QFile &file)
212 {
213  // open stream for writing
214  QTextStream cpp (&file);
215 
216  // set the starting indentation at zero
217  m_indentLevel = 0;
218 
219  //try to find a heading file (license, coments, etc)
220  QString str;
221  str = getHeadingFile(".cpp");
222  if (!str.isEmpty()) {
223  str.replace(QRegExp("%filename%"), fileName_ + ".cpp");
224  str.replace(QRegExp("%filepath%"), file.fileName());
225  cpp << str << m_endl;
226  }
227 
228  // IMPORT statements
229  // Q: Why all utils? Isnt just List and Vector the only classes we are using?
230  // Our import *should* also look at operations, and check that objects being
231  // used arent in another package (and thus need to be explicitly imported here).
232  cpp << "#include \"" << className_ << ".h\"" << m_endl;
233  writeBlankLine(cpp);
234 
235  if (c->visibility() == Uml::Visibility::Implementation) {
236  writeClassDecl(c, cpp);
237  }
238 
239  // Start body of class
240 
241  // Constructors: anything we more we need to do here ?
242  //
243  if (!c->isInterface())
244  writeConstructorMethods(c, cpp);
245 
246  // METHODS
247  //
248 
249  // write comment for section IF needed
250  QString indnt = indent();
251  if (forceDoc() || c->hasAccessorMethods() || c->hasOperationMethods()) {
252  writeComment(" ", indnt, cpp);
253  writeComment("Methods", indnt, cpp);
254  writeComment(" ", indnt, cpp);
255  writeBlankLine(cpp);
256  writeBlankLine(cpp);
257  }
258 
259  // write comment for sub-section IF needed
260  if (forceDoc() || c->hasAccessorMethods()) {
261  writeComment("Accessor methods", indnt, cpp);
262  writeComment(" ", indnt, cpp);
263  writeBlankLine(cpp);
264  }
265 
266  // Accessor methods for attributes
267  const bool bInlineAccessors = policyExt()->getAccessorsAreInline();
268  if (!bInlineAccessors && c->hasAttributes()) {
269  writeAttributeMethods(c->getAttributeListStatic(Uml::Visibility::Public), Uml::Visibility::Public, false, true, !bInlineAccessors, cpp);
270  writeAttributeMethods(c->getAttributeList(Uml::Visibility::Public), Uml::Visibility::Public, false, false, !bInlineAccessors, cpp);
271  writeAttributeMethods(c->getAttributeListStatic(Uml::Visibility::Protected), Uml::Visibility::Protected, false, true, !bInlineAccessors, cpp);
272  writeAttributeMethods(c->getAttributeList(Uml::Visibility::Protected), Uml::Visibility::Protected, false, false, !bInlineAccessors, cpp);
273  writeAttributeMethods(c->getAttributeListStatic(Uml::Visibility::Private), Uml::Visibility::Private, false, true, !bInlineAccessors, cpp);
274  writeAttributeMethods(c->getAttributeList(Uml::Visibility::Private), Uml::Visibility::Private, false, false, !bInlineAccessors, cpp);
275  }
276 
277  // accessor methods for associations
278 
279  // public
280  writeAssociationMethods(c->getSpecificAssocs(Uml::AssociationType::Association), Uml::Visibility::Public, false,
281  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
282  writeAssociationMethods(c->getUniAssociationToBeImplemented(), Uml::Visibility::Public, false,
283  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
284  writeAssociationMethods(c->getAggregations(), Uml::Visibility::Public, false,
285  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
286  writeAssociationMethods(c->getCompositions(), Uml::Visibility::Public, false,
287  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
288 
289  // protected
290  writeAssociationMethods(c->getSpecificAssocs(Uml::AssociationType::Association), Uml::Visibility::Protected, false,
291  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
292  writeAssociationMethods(c->getUniAssociationToBeImplemented(), Uml::Visibility::Protected, false,
293  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
294  writeAssociationMethods(c->getAggregations(), Uml::Visibility::Protected, false,
295  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
296  writeAssociationMethods(c->getCompositions(), Uml::Visibility::Protected, false,
297  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
298 
299  // private
300  writeAssociationMethods(c->getSpecificAssocs(Uml::AssociationType::Association), Uml::Visibility::Private, false,
301  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
302  writeAssociationMethods(c->getUniAssociationToBeImplemented(), Uml::Visibility::Private, false,
303  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
304  writeAssociationMethods(c->getAggregations(), Uml::Visibility::Private, false,
305  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
306  writeAssociationMethods(c->getCompositions(), Uml::Visibility::Private, false,
307  !INLINE_ASSOCIATION_METHODS, true, c->id(), cpp);
308  writeBlankLine(cpp);
309 
310  // Other operation methods -- all other operations are now written
311  //
312 
313  // write comment for sub-section IF needed
314  if (forceDoc() || c->hasOperationMethods()) {
315  writeComment("Other methods", indnt, cpp);
316  writeComment(" ", indnt, cpp);
317  writeBlankLine(cpp);
318  }
319 
320  if (!policyExt()->getOperationsAreInline()) {
321  writeOperations(c, false, Uml::Visibility::Public, cpp);
322  writeOperations(c, false, Uml::Visibility::Protected, cpp);
323  writeOperations(c, false, Uml::Visibility::Private, cpp);
324  }
325 
326  // Yep, bringing up the back of the bus, our initialization method for attributes
327  writeInitAttributeMethod(c, cpp);
328 
329  writeBlankLine(cpp);
330 }
331 
336 void CppWriter::writeClassDecl(UMLClassifier *c, QTextStream &cpp)
337 {
338  UMLClassifierList superclasses = c->getSuperClasses();
339  foreach (UMLClassifier* classifier, superclasses) {
340  QString headerName = findFileName(classifier, ".h");
341  if (!headerName.isEmpty()) {
342  cpp << "#include \"" << headerName << "\"" << m_endl;
343  }
344  }
345 
346  writeBlankLine(cpp);
347  cpp << "#include <" << policyExt()->getStringClassNameInclude() << ">" << m_endl;
348  if (c->hasVectorFields())
349  {
350  cpp << "#include " << policyExt()->getVectorClassNameInclude() << m_endl;
351  writeBlankLine(cpp);
352  }
353 
354  if (c->hasAssociations())
355  {
356  // write all includes we need to include other classes, that arent us.
357  printAssociationIncludeDecl (c->getSpecificAssocs(Uml::AssociationType::Association), c->id(), cpp);
358  printAssociationIncludeDecl (c->getUniAssociationToBeImplemented(), c->id(), cpp);
359  printAssociationIncludeDecl (c->getAggregations(), c->id(), cpp);
360  printAssociationIncludeDecl (c->getCompositions(), c->id(), cpp);
361 
362  writeBlankLine(cpp);
363  }
364 
365  foreach (UMLClassifier* classifier, superclasses) {
366  if (classifier->package()!=c->package() && !classifier->package().isEmpty()) {
367  cpp << "using " << cleanName(classifier->package()) << "::" << cleanName(classifier->name()) << ";" << m_endl;
368  }
369  }
370 
371  if (!c->package().isEmpty() && policyExt()->getPackageIsNamespace())
372  cpp << m_endl << "namespace " << cleanName(c->package()) << " {" << m_endl << m_endl;
373 
374  //Write class Documentation if there is somthing or if force option
375  if (forceDoc() || !c->doc().isEmpty()) {
376  cpp << m_endl << "/**" << m_endl;
377  cpp << " * class " << className_ << m_endl;
378  cpp << formatDoc(c->doc()," * ");
379  cpp << " */";
380  writeBlankLine(cpp);
381  writeBlankLine(cpp);
382  }
383 
384  //check if class is abstract and / or has abstract methods
385  if ((c->isAbstract() || c->isInterface())
386  && !hasAbstractOps(c))
387  cpp << "/******************************* Abstract Class ****************************" << m_endl
388  << className_ << " does not have any pure virtual methods, but its author" << m_endl
389  << " defined it as an abstract class, so you should not use it directly." << m_endl
390  << " Inherit from it instead and create only objects from the derived classes" << m_endl
391  << "*****************************************************************************/" << m_endl << m_endl;
392 
393  if (c->baseType() == UMLObject::ot_Enum) {
394  UMLClassifierListItemList litList = c->getFilteredList(UMLObject::ot_EnumLiteral);
395  uint i = 0;
396  cpp << "enum " << className_ << " {" << m_endl;
397  foreach (UMLClassifierListItem* lit, litList) {
398  QString enumLiteral = cleanName(lit->name());
399  cpp << indent() << enumLiteral;
400  if (++i < (uint)litList.count())
401  cpp << ",";
402  cpp << m_endl;
403  }
404  cpp << m_endl << "};" << m_endl; // end of class header
405  if (!c->package().isEmpty() && policyExt()->getPackageIsNamespace())
406  cpp << "} // end of package namespace" << m_endl;
407  return;
408  }
409 
410  // Generate template parameters.
411  UMLTemplateList template_params = c->getTemplateList();
412  if (template_params.count()) {
413  cpp << "template<";
414  for (UMLTemplateListIt tlit(template_params); tlit.hasNext();) {
415  UMLTemplate* t = tlit.next();
416  QString formalName = t->name();
417  QString typeName = t->getTypeName();
418  cpp << typeName << " " << formalName;
419  if (tlit.hasNext()) {
420  tlit.next();
421  cpp << ", ";
422  }
423  }
424  cpp << ">" << m_endl;
425  }
426 
427  cpp << "class " << className_;
428  uint numOfSuperClasses = c->getSuperClasses().count();
429  if (numOfSuperClasses > 0)
430  cpp << " : ";
431  uint i = 0;
432  foreach (UMLClassifier* superClass, c->getSuperClasses()) {
433  i++;
434  if (superClass->isAbstract() || superClass->isInterface())
435  cpp << "virtual ";
436  cpp << "public " << cleanName(superClass->name());
437  if (i < numOfSuperClasses)
438  cpp << ", ";
439  }
440 
441  cpp << m_endl << "{" << m_endl; // begin the body of the class
442 
443  //
444  // write out field and operations decl grouped by visibility
445  //
446 
447  // PUBLIC attribs/methods
448  cpp << "public:" << m_endl << m_endl; // print visibility decl.
449  // for public: constructors are first ops we print out
450  if (!c->isInterface())
451  writeConstructorDecls(cpp);
452  writeHeaderFieldDecl(c, Uml::Visibility::Public, cpp);
453  writeHeaderAccessorMethodDecl(c, Uml::Visibility::Public, cpp);
454  writeOperations(c, true, Uml::Visibility::Public, cpp);
455 
456  // PROTECTED attribs/methods
457  //
458  cpp << "protected" << ":" << m_endl << m_endl; // print visibility decl.
459  writeHeaderFieldDecl(c, Uml::Visibility::Protected, cpp);
460  writeHeaderAccessorMethodDecl(c, Uml::Visibility::Protected, cpp);
461  writeOperations(c, true, Uml::Visibility::Protected, cpp);
462 
463  // PRIVATE attribs/methods
464  //
465  cpp << "private" << ":" << m_endl << m_endl; // print visibility decl.
466  writeHeaderFieldDecl(c, Uml::Visibility::Private, cpp);
467  writeHeaderAccessorMethodDecl(c, Uml::Visibility::Private, cpp);
468  writeOperations(c, true, Uml::Visibility::Private, cpp);
469  writeInitAttributeDecl(c, cpp); // this is always private, used by constructors to initialize class
470 
471  // end of class header
472  cpp << m_endl << "};" << m_endl;
473 
474  // end of class namespace, if any
475  if(!c->package().isEmpty() && policyExt()->getPackageIsNamespace())
476  cpp << "}; // end of package namespace" << m_endl;
477 
478 }
479 
487 void CppWriter::writeAttributeDecls (UMLClassifier *c, Uml::Visibility::Enum visibility, bool writeStatic, QTextStream &stream)
488 {
489  if (c->isInterface())
490  return;
491 
492  UMLAttributeList list;
493  if (writeStatic)
494  list = c->getAttributeListStatic(visibility);
495  else
496  list = c->getAttributeList(visibility);
497 
498  //write documentation
499  if (forceDoc() || list.count() > 0)
500  {
501  QString strVis = Codegen_Utils::capitalizeFirstLetter(Uml::Visibility::toString(visibility));
502  QString strStatic = writeStatic ? "Static ":"";
503  writeComment(strStatic + strVis + " attributes", indent(), stream);
504  writeComment(" ", indent(), stream);
505  writeBlankLine(stream);
506  }
507 
508  if (list.count() > 0) {
509 
510  // write attrib declarations now
511  // bool isFirstAttrib = true;
512  QString documentation;
513  foreach (UMLAttribute* at, list) {
514 
515  // bool noPriorDocExists = documentation.isEmpty();
516  documentation = at->doc();
517 
518  // add a line for code clarity IF PRIOR attrib has comment on it
519  // OR this line has documentation
520  // if(!isFirstAttrib && (!documentation.isEmpty()||!noPriorDocExists))
521  // writeBlankLine(stream);
522 
523  // isFirstAttrib = false;
524 
525  QString varName = getAttributeVariableName(at);
526 
527  QString staticValue = at->isStatic() ? "static " : "";
528  QString typeName = fixTypeName(at->getTypeName());
529  if(!documentation.isEmpty())
530  writeComment(documentation, indent(), stream);
531  stream << indent() << staticValue << typeName << " " << varName << ";" << m_endl;
532 
533  }
534 
535  /*
536  if(list.count() > 0)
537  writeBlankLine(stream);
538  */
539  }
540 }
541 
542 void CppWriter::writeHeaderAttributeAccessorMethods (UMLClassifier *c, Uml::Visibility::Enum visibility, bool writeStatic, QTextStream &stream)
543 {
544  // check the current policy about generate accessors as public
545  UMLAttributeList list;
546  if (writeStatic)
547  list = c->getAttributeListStatic(visibility);
548  else
549  list = c->getAttributeList(visibility);
550 
551  // switch to public
552  if (visibility != Uml::Visibility::Public)
553  stream << "public:" << m_endl << m_endl;
554 
555  // write accessor methods for attribs we found
556  writeAttributeMethods(list, visibility, true, false, policyExt()->getAccessorsAreInline(), stream);
557 
558  // switch back to previous vis.
559  if (visibility != Uml::Visibility::Public)
560  stream << Uml::Visibility::toString(visibility) << ":" << m_endl << m_endl;
561 }
562 
567 void CppWriter::writeAttributeMethods(UMLAttributeList attribs,
568  Uml::Visibility::Enum visibility, bool isHeaderMethod,
569  bool isStatic,
570  bool writeMethodBody, QTextStream &stream)
571 {
572  if (!policyExt()->getAutoGenerateAccessors())
573  return;
574 
575  if (forceDoc() || attribs.count() > 0)
576  {
577  QString strVis = Codegen_Utils::capitalizeFirstLetter(Uml::Visibility::toString(visibility));
578  QString strStatic = (isStatic ? " static" : "");
579  writeBlankLine(stream);
580  writeComment(strVis + strStatic + " attribute accessor methods", indent(), stream);
581  writeComment(" ", indent(), stream);
582  writeBlankLine(stream);
583  }
584 
585  // return now if NO attributes to work on
586  if (attribs.count() == 0)
587  return;
588 
589  foreach (UMLAttribute* at, attribs) {
590  QString varName = getAttributeVariableName(at);
591  QString methodBaseName = cleanName(at->name());
592 
593  // force capitalizing the field name, this is silly,
594  // from what I can tell, this IS the default behavior for
595  // cleanName. I dunno why it is not working -b.t.
596  methodBaseName = methodBaseName.trimmed();
597  methodBaseName.replace(0, 1, methodBaseName.at(0).toUpper());
598 
599  writeSingleAttributeAccessorMethods(at->getTypeName(), varName,
600  methodBaseName, at->doc(), Uml::Changeability::Changeable, isHeaderMethod,
601  at->isStatic(), writeMethodBody, stream);
602  }
603 }
604 
608 void CppWriter::writeComment(const QString &comment, const QString &myIndent, QTextStream &cpp)
609 {
610  // in the case we have several line comment..
611  // NOTE: this part of the method has the problem of adopting UNIX newline,
612  // need to resolve for using with MAC/WinDoze eventually I assume
613  if (comment.contains(QRegExp("\n"))) {
614 
615  QStringList lines = comment.split('\n');
616  for (int i= 0; i < lines.count(); ++i)
617  {
618  cpp << myIndent << "// " << lines[i] << m_endl;
619  }
620  } else {
621  // this should be more fancy in the future, breaking it up into 80 char
622  // lines so that it doesn't look too bad
623  cpp << myIndent << "// "<< comment << m_endl;
624  }
625 }
626 
630 void CppWriter::writeDocumentation(QString header, QString body, QString end, QTextStream &cpp)
631 {
632  writeBlankLine(cpp);
633  QString indnt = indent();
634 
635  cpp << indnt << "/**" << m_endl;
636  if (!header.isEmpty())
637  cpp << formatDoc(header, indnt + " * ");
638  if (!body.isEmpty())
639  cpp << formatDoc(body, indnt + " * ");
640  if (!end.isEmpty()) {
641  QStringList lines = end.split('\n');
642  for (int i = 0; i < lines.count(); ++i) {
643  cpp << formatDoc(lines[i], indnt + " * ");
644  }
645  }
646  cpp << indnt << " */" << m_endl;
647 }
648 
652 void CppWriter::writeAssociationDecls(UMLAssociationList associations, Uml::Visibility::Enum permitScope, Uml::ID::Type id, QTextStream &h)
653 {
654  if(forceSections() || !associations.isEmpty())
655  {
656  bool printRoleA = false, printRoleB = false;
657  foreach (UMLAssociation *a, associations)
658  {
659  // it may seem counter intuitive, but you want to insert the role of the
660  // *other* class into *this* class.
661  if (a->getObjectId(Uml::RoleType::A) == id && !a->getRoleName(Uml::RoleType::B).isEmpty())
662  printRoleB = true;
663 
664  if (a->getObjectId(Uml::RoleType::B) == id && !a->getRoleName(Uml::RoleType::A).isEmpty())
665  printRoleA = true;
666 
667  // First: we insert documentaion for association IF it has either role AND some documentation (!)
668  if ((printRoleA || printRoleB) && !(a->doc().isEmpty()))
669  writeComment(a->doc(), indent(), h);
670 
671  // print RoleB decl
672  if (printRoleB && a->visibility(Uml::RoleType::B) == permitScope)
673  {
674 
675  QString fieldClassName = cleanName(umlObjectName(a->getObject(Uml::RoleType::B)));
676  writeAssociationRoleDecl(fieldClassName, a->getRoleName(Uml::RoleType::B), a->getMultiplicity(Uml::RoleType::B), a->getRoleDoc(Uml::RoleType::B), h);
677  }
678 
679  // print RoleA decl
680  if (printRoleA && a->visibility(Uml::RoleType::A) == permitScope)
681  {
682  QString fieldClassName = cleanName(umlObjectName(a->getObject(Uml::RoleType::A)));
683  writeAssociationRoleDecl(fieldClassName, a->getRoleName(Uml::RoleType::A), a->getMultiplicity(Uml::RoleType::A), a->getRoleDoc(Uml::RoleType::A), h);
684  }
685 
686  // reset for next association in our loop
687  printRoleA = false;
688  printRoleB = false;
689  }
690  }
691 }
692 
696 void CppWriter::writeAssociationRoleDecl(QString fieldClassName, QString roleName, QString multi,
697  QString doc, QTextStream &stream)
698 {
699  // ONLY write out IF there is a rolename given
700  // otherwise it is not meant to be declared in the code
701  if (roleName.isEmpty())
702  return;
703 
704  QString indnt = indent();
705 
706  // always put space between this and prior decl, if any
707  writeBlankLine(stream);
708 
709  if (!doc.isEmpty())
710  writeComment(doc, indnt, stream);
711 
712  // declare the association based on whether it is this a single variable
713  // or a List (Vector). One day this will be done correctly with special
714  // multiplicity object that we don't have to figure out what it means via regex.
715  if (multi.isEmpty() || multi.contains(QRegExp("^[01]$")))
716  {
717  QString fieldVarName = "m_" + roleName.toLower();
718 
719  // record this for later consideration of initialization IF the
720  // multi value requires 1 of these objects
721  if (ObjectFieldVariables.indexOf(fieldVarName) == -1 &&
722  multi.contains(QRegExp("^1$")))
723  {
724  // ugh. UGLY. Storing variable name and its class in pairs.
725  ObjectFieldVariables.append(fieldVarName);
726  ObjectFieldVariables.append(fieldClassName);
727  }
728 
729  stream << indnt << fieldClassName << " * " << fieldVarName << ";" << m_endl;
730  }
731  else
732  {
733  QString fieldVarName = "m_" + roleName.toLower() + "Vector";
734 
735  // record unique occurrences for later when we want to check
736  // for initialization of this vector
737  if (VectorFieldVariables.indexOf(fieldVarName) == -1)
738  VectorFieldVariables.append(fieldVarName);
739 
740  stream << indnt << policyExt()->getVectorClassName() <<"<" << fieldClassName << "*";
741  stream << "> " << fieldVarName << ";" << m_endl;
742  }
743 }
744 
749 void CppWriter::writeAssociationMethods (UMLAssociationList associations,
750  Uml::Visibility::Enum permitVisib,
751  bool isHeaderMethod,
752  bool writeMethodBody,
753  bool writePointerVar,
754  Uml::ID::Type myID, QTextStream &stream)
755 {
756  if (forceSections() || !associations.isEmpty())
757  {
758  foreach (UMLAssociation *a, associations)
759  {
760 
761  // insert the methods to access the role of the other
762  // class in the code of this one
763  if (a->getObjectId(Uml::RoleType::A) == myID && a->visibility(Uml::RoleType::B) == permitVisib)
764  {
765  // only write out IF there is a rolename given
766  if (!a->getRoleName(Uml::RoleType::B).isEmpty()) {
767  QString fieldClassName = umlObjectName(a->getObject(Uml::RoleType::B)) + (writePointerVar ? " *":"");
768  writeAssociationRoleMethod(fieldClassName,
769  isHeaderMethod,
770  writeMethodBody,
771  a->getRoleName(Uml::RoleType::B),
772  a->getMultiplicity(Uml::RoleType::B), a->getRoleDoc(Uml::RoleType::B),
773  a->changeability(Uml::RoleType::B), stream);
774  }
775  }
776 
777  if (a->getObjectId(Uml::RoleType::B) == myID && a->visibility(Uml::RoleType::A) == permitVisib)
778  {
779  // only write out IF there is a rolename given
780  if (!a->getRoleName(Uml::RoleType::A).isEmpty()) {
781  QString fieldClassName = umlObjectName(a->getObject(Uml::RoleType::A)) + (writePointerVar ? " *":"");
782  writeAssociationRoleMethod(fieldClassName,
783  isHeaderMethod,
784  writeMethodBody,
785  a->getRoleName(Uml::RoleType::A),
786  a->getMultiplicity(Uml::RoleType::A),
787  a->getRoleDoc(Uml::RoleType::A),
788  a->changeability(Uml::RoleType::A),
789  stream);
790  }
791  }
792 
793  }
794  }
795 }
796 
802 void CppWriter::writeAssociationRoleMethod (const QString &fieldClassName,
803  bool isHeaderMethod,
804  bool writeMethodBody,
805  const QString &roleName, const QString &multi,
806  const QString &description, Uml::Changeability::Enum change,
807  QTextStream &stream)
808 {
809  if (multi.isEmpty() || multi.contains(QRegExp("^[01]$")))
810  {
811  QString fieldVarName = "m_" + roleName.toLower();
812  writeSingleAttributeAccessorMethods(fieldClassName, fieldVarName, roleName,
813  description, change, isHeaderMethod, false, writeMethodBody, stream);
814  }
815  else
816  {
817  QString fieldVarName = "m_" + roleName.toLower() + "Vector";
818  writeVectorAttributeAccessorMethods(fieldClassName, fieldVarName, roleName,
819  description, change, isHeaderMethod, writeMethodBody, stream);
820  }
821 }
822 
826 void CppWriter::writeVectorAttributeAccessorMethods (
827  const QString &fieldClassName, const QString &fieldVarName,
828  const QString &fieldName, const QString &description,
829  Uml::Changeability::Enum changeType,
830  bool isHeaderMethod,
831  bool writeMethodBody,
832  QTextStream &stream)
833 {
834  QString className = fixTypeName(fieldClassName);
835  QString fldName = Codegen_Utils::capitalizeFirstLetter(fieldName);
836  QString indnt = indent();
837 
838  // ONLY IF changeability is NOT Frozen
839  if (changeType != Uml::Changeability::Frozen)
840  {
841  writeDocumentation("Add a " + fldName + " object to the " + fieldVarName + " List", description,"", stream);
842  stream << indnt << "void ";
843  if(!isHeaderMethod)
844  stream << className_ << "::";
845  stream << "add" << fldName << " (" << className << " add_object)";
846  if (writeMethodBody) {
847  QString method = VECTOR_METHOD_APPEND;
848  method.replace(QRegExp("%VARNAME%"), fieldVarName);
849  method.replace(QRegExp("%VECTORTYPENAME%"), policyExt()->getVectorClassName());
850  method.replace(QRegExp("%ITEMCLASS%"), className);
851  stream << indnt << " {" << m_endl;
852  m_indentLevel++;
853  printTextAsSeparateLinesWithIndent(method, indent(), stream);
854  m_indentLevel--;
855  stream << indnt << "}" << m_endl;
856  } else
857  stream << ";" << m_endl;
858  }
859 
860  // ONLY IF changeability is Changeable
861  if (changeType == Uml::Changeability::Changeable)
862  {
863  writeDocumentation("Remove a " + fldName + " object from " + fieldVarName + " List",
864  description, "", stream);
865  stream << indnt << "void ";
866  if(!isHeaderMethod)
867  stream << className_ << "::";
868  stream << "remove" << fldName << " (" << className << " remove_object)";
869  if (writeMethodBody) {
870  QString method = VECTOR_METHOD_REMOVE;
871  method.replace(QRegExp("%VARNAME%"), fieldVarName);
872  method.replace(QRegExp("%VECTORTYPENAME%"), policyExt()->getVectorClassName());
873  method.replace(QRegExp("%ITEMCLASS%"), className);
874  stream << indnt << " {" << m_endl;
875  m_indentLevel++;
876  printTextAsSeparateLinesWithIndent(method, indent(), stream);
877  m_indentLevel--;
878  stream << indnt << "}" << m_endl;
879  } else
880  stream << ";" << m_endl;
881  }
882 
883  // always allow getting the list of stuff
884  QString returnVarName = policyExt()->getVectorClassName() + '<' + className + '>';
885  writeDocumentation("Get the list of " + fldName + " objects held by " + fieldVarName,
886  description,
887  policyExt()->getDocToolTag() + "return " + returnVarName + " list of " + fldName + " objects held by " + fieldVarName,
888  stream);
889  stream << indnt << returnVarName << " ";
890  if(!isHeaderMethod)
891  stream << className_ << "::";
892  stream << "get" << fldName << "List ()";
893  if (writeMethodBody) {
894  stream << indnt << " {" << m_endl;
895  m_indentLevel++;
896  stream << indent() << "return " << fieldVarName << ";" << m_endl;
897  m_indentLevel--;
898  stream << indnt << "}" << m_endl;
899  }
900  else {
901  stream << ";" << m_endl;
902  }
903 }
904 
908 void CppWriter::writeSingleAttributeAccessorMethods(
909  const QString& fieldClassName, const QString& fieldVarName,
910  const QString& fieldName, const QString &description,
911  Uml::Changeability::Enum change,
912  bool isHeaderMethod,
913  bool isStatic,
914  bool writeMethodBody,
915  QTextStream &stream)
916 {
917  // DON'T write this IF it is a source method AND writeMethodBody is "false"
918  if (!isHeaderMethod && !writeMethodBody)
919  return;
920 
921  QString className = fixTypeName(fieldClassName);
922  QString fldName = Codegen_Utils::capitalizeFirstLetter(fieldName);
923  QString indnt = indent();
924 
925  // set method
926  if (change == Uml::Changeability::Changeable && !isStatic) {
927  writeDocumentation("Set the value of " + fieldVarName, description, policyExt()->getDocToolTag() + "param new_var the new value of " + fieldVarName, stream);
928  stream << indnt << "void ";
929  if(!isHeaderMethod)
930  stream << className_ << "::";
931  stream << "set" << fldName << " (" << className << " new_var)";
932 
933  if (writeMethodBody) {
934  stream << indnt << " {" << m_endl;
935  m_indentLevel++;
936  stream << indent() << indnt;
937  m_indentLevel--;
938  if (isStatic)
939  stream << className_ << "::";
940  stream << fieldVarName << " = new_var;" << m_endl;
941  stream << indnt << "}" << m_endl;
942  } else
943  stream << ";" << m_endl;
944  }
945 
946  // get method
947  writeDocumentation("Get the value of " + fieldVarName, description, policyExt()->getDocToolTag() + "return the value of " + fieldVarName, stream);
948  stream << indnt << className << " ";
949  if (!isHeaderMethod)
950  stream << className_ << "::";
951  stream << "get" << fldName << " ()";
952 
953  if (writeMethodBody) {
954  stream << indnt << " {" << m_endl;
955  m_indentLevel++;
956  stream << indent() << "return ";
957  m_indentLevel--;
958  if (isStatic)
959  stream << className_ << "::";
960  stream << fieldVarName << ";" << m_endl;
961  stream << indnt << "}";
962  } else
963  stream << ";" << m_endl;
964 
965  writeBlankLine(stream);
966 }
967 
973 void CppWriter::writeConstructorDecls(QTextStream &stream)
974 {
975  const bool generateEmptyConstructors =
976  UMLApp::app()->commonPolicy()->getAutoGenerateConstructors();
977  if (forceDoc() || generateEmptyConstructors)
978  {
979  writeComment("Constructors/Destructors", indent(), stream);
980  writeComment(" ", indent(), stream);
981  writeBlankLine(stream);
982  }
983  if (!generateEmptyConstructors)
984  return;
985 
986  writeDocumentation("", "Empty Constructor", "", stream);
987  stream << indent() << className_ << " ();" << m_endl;
988  writeDocumentation("", "Empty Destructor", "", stream);
989  stream << indent();
990  stream << "virtual ~" << className_ << " ();" << m_endl;
991  writeBlankLine(stream);
992 }
993 
997 void CppWriter::writeInitAttributeDecl(UMLClassifier * c, QTextStream &stream)
998 {
999  if (UMLApp::app()->commonPolicy()->getAutoGenerateConstructors() &&
1000  c->hasAttributes())
1001  stream << indent() << "void initAttributes () ;" << m_endl;
1002 }
1003 
1007 void CppWriter::writeInitAttributeMethod(UMLClassifier * c, QTextStream &stream)
1008 {
1009  // only need to do this under certain conditions
1010  if (!UMLApp::app()->commonPolicy()->getAutoGenerateConstructors() ||
1011  !c->hasAttributes())
1012  return;
1013 
1014  QString indnt = indent();
1015 
1016  stream << indnt << "void " << className_ << "::" << "initAttributes () {" << m_endl;
1017 
1018  m_indentLevel++;
1019  // first, initiation of fields derived from attributes
1020  UMLAttributeList atl = c->getAttributeList();
1021  foreach (UMLAttribute* at, atl) {
1022  if(!at->getInitialValue().isEmpty()) {
1023  QString varName = getAttributeVariableName(at);
1024  stream << indent() << varName << " = " << at->getInitialValue() << ";" << m_endl;
1025  }
1026  }
1027  // Now initialize the association related fields (e.g. vectors)
1028  if (!VECTOR_METHOD_INIT.isEmpty()) {
1029  QStringList::Iterator it;
1030  for(it = VectorFieldVariables.begin(); it != VectorFieldVariables.end(); ++it) {
1031  QString fieldVarName = *it;
1032  QString method = VECTOR_METHOD_INIT;
1033  method.replace(QRegExp("%VARNAME%"), fieldVarName);
1034  method.replace(QRegExp("%VECTORTYPENAME%"), policyExt()->getVectorClassName());
1035  stream << indent() << method << m_endl;
1036  }
1037  }
1038 
1039  if (!OBJECT_METHOD_INIT.isEmpty()) {
1040  QStringList::Iterator it;
1041  for(it = ObjectFieldVariables.begin(); it != ObjectFieldVariables.end(); ++it) {
1042  QString fieldVarName = *it;
1043  it++;
1044  QString fieldClassName = *it;
1045  QString method = OBJECT_METHOD_INIT;
1046  method.replace(QRegExp("%VARNAME%"), fieldVarName);
1047  method.replace(QRegExp("%ITEMCLASS%"), fieldClassName);
1048  stream << indent() << method << m_endl;
1049  }
1050  }
1051 
1052  // clean up
1053  ObjectFieldVariables.clear(); // shouldn't be needed?
1054  VectorFieldVariables.clear(); // shouldn't be needed?
1055 
1056  m_indentLevel--;
1057 
1058  stream << indnt << "}" << m_endl;
1059 }
1060 
1061 // one day, this should print out non-empty constructor operations too.
1062 void CppWriter::writeConstructorMethods(UMLClassifier * c, QTextStream &stream)
1063 {
1064  const bool generateEmptyConstructors =
1065  UMLApp::app()->commonPolicy()->getAutoGenerateConstructors();
1066 
1067  if (forceDoc() || generateEmptyConstructors) {
1068  writeComment("Constructors/Destructors", indent(), stream);
1069  writeComment(" ", indent(), stream);
1070  writeBlankLine(stream);
1071  }
1072  if (!generateEmptyConstructors)
1073  return;
1074 
1075  // empty constructor
1076  QString indnt = indent();
1077  stream << indnt << className_ << "::" << className_ << " () {" << m_endl;
1078  if(c->hasAttributes())
1079  stream << indnt << indnt << "initAttributes();" << m_endl;
1080  stream << indnt << "}" << m_endl;
1081  writeBlankLine(stream);
1082 
1083  // empty destructor
1084  stream << indent() << className_ << "::~" << className_ << " () { }" << m_endl;
1085  writeBlankLine(stream);
1086 }
1087 
1091 QString CppWriter::fixTypeName(const QString &string)
1092 {
1093  if (string.isEmpty())
1094  return "void";
1095  if (string == "string")
1096  return policyExt()->getStringClassName();
1097  return string;
1098 }
1099 
1107 void CppWriter::writeOperations(UMLClassifier *c, bool isHeaderMethod,
1108  Uml::Visibility::Enum permitScope, QTextStream &cpp)
1109 {
1110  UMLOperationList oplist;
1111 
1112  //sort operations by scope first and see if there are abstract methods
1113  UMLOperationList inputlist = c->getOpList();
1114  foreach (UMLOperation* op, inputlist) {
1115  if (op->visibility() == permitScope) {
1116  oplist.append(op);
1117  }
1118  }
1119 
1120  // do people REALLY want these comments? Hmm.
1121  /*
1122  if(forceSections() || oppub.count())
1123  {
1124  writeComment("public operations", indent(), cpp);
1125  writeBlankLine(cpp);
1126  }
1127  */
1128  writeOperations(c, oplist, isHeaderMethod, cpp);
1129 }
1130 
1139 void CppWriter::writeOperations(UMLClassifier *c, UMLOperationList &oplist, bool isHeaderMethod, QTextStream &cpp)
1140 {
1141  const bool generateEmptyConstructors =
1142  UMLApp::app()->commonPolicy()->getAutoGenerateConstructors();
1143 
1144  // generate method decl for each operation given
1145  foreach (UMLOperation* op, oplist) {
1146  QString doc; // buffer for documentation
1147  QString methodReturnType;
1148  UMLAttributeList atl = op->getParmList(); // method parameters
1149 
1150  if (op->isConstructorOperation()) {
1151  if (generateEmptyConstructors && atl.count() == 0)
1152  continue; // it's already been written, see writeConstructor{Decls, Methods}
1153  } else if (op->isDestructorOperation()) {
1154  if (generateEmptyConstructors)
1155  continue; // it's already been written, see writeConstructor{Decls, Methods}
1156  } else {
1157  methodReturnType = fixTypeName(op->getTypeName());
1158  if(methodReturnType != "void")
1159  doc += policyExt()->getDocToolTag() + "return " + methodReturnType + '\n';
1160  }
1161 
1162  QString str;
1163  if (op->isAbstract() || c->isInterface()) {
1164  if (isHeaderMethod) {
1165  // declare abstract method as 'virtual'
1166  str += "virtual ";
1167  }
1168  }
1169 
1170  // static declaration for header file
1171  if (isHeaderMethod && op->isStatic())
1172  str += "static ";
1173 
1174  // returntype of method
1175  str += methodReturnType + ' ';
1176 
1177  if (!isHeaderMethod)
1178  str += className_ + "::";
1179 
1180  str += cleanName(op->name()) + " (";
1181 
1182  // generate parameters
1183  uint j = 0;
1184  for (UMLAttributeListIt atlIt(atl); atlIt.hasNext() ; ++j) {
1185  UMLAttribute* at = atlIt.next();
1186  QString typeName = fixTypeName(at->getTypeName());
1187  QString atName = cleanName(at->name());
1188  str += typeName + ' ' + atName;
1189  const QString initVal = at->getInitialValue();
1190  if (! initVal.isEmpty())
1191  str += " = " + initVal;
1192  if (j < (uint)(atl.count() - 1))
1193  str += ", ";
1194  doc += policyExt()->getDocToolTag() + "param " + atName + ' ' + at->doc() + m_endl;
1195  }
1196  doc = doc.remove(doc.size() - 1, 1); // remove last endl of comment
1197  str += ")";
1198 
1199  if (op->getConst())
1200  str += " const";
1201 
1202  // method body : only gets IF it is not in a header
1203  if (isHeaderMethod && !policyExt()->getOperationsAreInline()) {
1204  str += ';'; // terminate now
1205  }
1206  else {
1207  str += m_endl + indent() + '{' + m_endl;
1208  QString sourceCode = op->getSourceCode();
1209  if (sourceCode.isEmpty()) {
1210  // empty method body - TODO: throw exception
1211  }
1212  else {
1213  str += formatSourceCode(sourceCode, indent() + indent());
1214  }
1215  str += indent() + '}';
1216  }
1217 
1218  // write it out
1219  writeDocumentation("", op->doc(), doc, cpp);
1220  cpp << indent() << str << m_endl;
1221  writeBlankLine(cpp);
1222  }
1223 }
1224 
1233 void CppWriter::printAssociationIncludeDecl(UMLAssociationList list, Uml::ID::Type myId, QTextStream &stream)
1234 {
1235  foreach (UMLAssociation *a, list) {
1236  UMLClassifier *current = NULL;
1237  bool isFirstClass = true;
1238 
1239  // only use OTHER classes (e.g. we don't need to write includes for ourselves!!
1240  // AND only IF the roleName is defined, otherwise, it is not meant to be noticed.
1241  if (a->getObjectId(Uml::RoleType::A) == myId && !a->getRoleName(Uml::RoleType::B).isEmpty()) {
1242  current = dynamic_cast<UMLClassifier*>(a->getObject(Uml::RoleType::B));
1243  } else if (a->getObjectId(Uml::RoleType::B) == myId && !a->getRoleName(Uml::RoleType::A).isEmpty()) {
1244  current = dynamic_cast<UMLClassifier*>(a->getObject(Uml::RoleType::A));
1245  isFirstClass = false;
1246  }
1247 
1248  // as header doc for this method indicates, we need to be a bit sophisticated on
1249  // how to declare some associations.
1250  if (current) {
1251  if(!isFirstClass && !a->getRoleName(Uml::RoleType::A).isEmpty() && !a->getRoleName(Uml::RoleType::B).isEmpty())
1252  stream << "class " << current->name() << ";" << m_endl; // special case: use forward declaration
1253  else
1254  stream << "#include \"" << current->name() << ".h\"" << m_endl; // just the include statement
1255  }
1256  }
1257 }
1258 
1262 QString CppWriter::fixInitialStringDeclValue(const QString &value, const QString &type)
1263 {
1264  QString val = value;
1265  // check for strings only
1266  if (!val.isEmpty() && type == policyExt()->getStringClassName()) {
1267  if (!val.startsWith('"'))
1268  val.prepend('"');
1269  if (!val.endsWith('"'))
1270  val.append('"');
1271  }
1272  return val;
1273 }
1274 
1280 QString CppWriter::umlObjectName(UMLObject *obj)
1281 {
1282  return(obj!=0)?obj->name():QString("NULL");
1283 }
1284 
1288 void CppWriter::writeBlankLine(QTextStream &stream)
1289 {
1290  stream << m_endl;
1291 }
1292 
1297 void CppWriter::printTextAsSeparateLinesWithIndent(const QString &text, const QString &indent, QTextStream &stream)
1298 {
1299  if (text.isEmpty()) {
1300  return;
1301  }
1302 
1303  QStringList lines = text.split('\n');
1304  for (int i= 0; i < lines.count(); ++i) {
1305  stream << indent << lines[i] << m_endl;
1306  }
1307 }
1308 
1312 QString CppWriter::getAttributeVariableName(UMLAttribute *at)
1313 {
1314  QString fieldName = cleanName(at->name());
1315  return fieldName;
1316 }
1317 
1322 QStringList CppWriter::defaultDatatypes()
1323 {
1324  return Codegen_Utils::cppDatatypes();
1325 }
1326 
1331 QStringList CppWriter::reservedKeywords() const
1332 {
1333  return Codegen_Utils::reservedCppKeywords();
1334 }
1335 
CppWriter::defaultDatatypes
QStringList defaultDatatypes()
Add C++ primitives as datatypes.
Definition: cppwriter.cpp:1322
umltemplatelist.h
UMLCanvasObject::getAggregations
UMLAssociationList getAggregations()
Shorthand for getSpecificAssocs(Uml::at_Aggregation)
Definition: umlcanvasobject.cpp:421
UMLAssociation::getObject
UMLObject * getObject(Uml::RoleType::Enum role) const
Returns the UMLObject assigned to the given role.
Definition: association.cpp:476
UMLObject::ot_EnumLiteral
Definition: umlobject.h:60
UMLOperation::getConst
bool getConst() const
Returns whether this operation is a query (C++ "const").
Definition: operation.cpp:411
CPPCodeGenerationPolicy::getVectorClassName
QString getVectorClassName()
Definition: cppcodegenerationpolicy.cpp:172
UMLClassifierListItemList
This sub-class adds copyInto and clone to the QPtrList base class...
Definition: umlclassifierlistitemlist.h:26
UMLClassifier
This class defines the non-graphical information required for a UML Classifier (ie a class or interfa...
Definition: classifier.h:39
UMLCanvasObject::getSpecificAssocs
UMLAssociationList getSpecificAssocs(Uml::AssociationType::Enum assocType)
Return the subset of m_List that matches the given type.
Definition: umlcanvasobject.cpp:61
SimpleCodeGenerator::className_
QString className_
Definition: simplecodegenerator.h:49
UMLClassifierListItem
Classifiers (classes, interfaces) have lists of operations, attributes, templates and others...
Definition: classifierlistitem.h:29
association.h
UMLAssociationList
QList< UMLAssociation * > UMLAssociationList
Definition: umlassociationlist.h:18
UMLAttribute::getInitialValue
QString getInitialValue() const
Returns The initial value of the UMLAttribute.
Definition: attribute.cpp:98
CPPCodeGenerationPolicy::getPackageIsNamespace
bool getPackageIsNamespace()
Get the value of m_packageIsNamespace.
Definition: cppcodegenerationpolicy.cpp:138
Uml::RoleType::A
Definition: basictypes.h:209
Uml::Visibility::Enum
Enum
Definition: basictypes.h:56
Codegen_Utils::cppDatatypes
QStringList cppDatatypes()
Return list of C++ datatypes.
Definition: codegen_utils.cpp:23
UMLObject::visibility
Uml::Visibility::Enum visibility() const
Returns the visibility of the object.
Definition: umlobject.cpp:435
UMLClassifier::getAttributeListStatic
UMLAttributeList getAttributeListStatic(Uml::Visibility::Enum scope) const
Returns the static attributes for the specified scope.
Definition: classifier.cpp:493
UMLClassifier::getFilteredList
virtual UMLClassifierListItemList getFilteredList(UMLObject::ObjectType ot) const
Returns the entries in m_List that are of the requested type.
Definition: classifier.cpp:1019
CppWriter::~CppWriter
virtual ~CppWriter()
Destructor, empty.
Definition: cppwriter.cpp:68
CodeGenerator::openFile
bool openFile(QFile &file, const QString &name)
Opens a file named "name" for writing in the outputDirectory.
Definition: codegenerator.cpp:586
UMLObject::ot_Enum
Definition: umlobject.h:55
CodeGenerator::formatDoc
static QString formatDoc(const QString &text, const QString &linePrefix=" *", int lineWidth=80)
Format documentation for output in source files.
Definition: codegenerator.cpp:772
Uml::ProgrammingLanguage::Cpp
Definition: basictypes.h:244
SimpleCodeGenerator::findFileName
QString findFileName(UMLPackage *concept, const QString &ext)
Determine the file name.
Definition: simplecodegenerator.cpp:89
UMLTemplate::getTypeName
virtual QString getTypeName() const
Overrides method from UMLClassifierListItem.
Definition: template.cpp:72
Uml::ProgrammingLanguage::Enum
Enum
Definition: basictypes.h:241
Uml::RoleType::B
Definition: basictypes.h:210
UMLApp::app
static UMLApp * app()
Get the last created instance of this class.
Definition: uml.cpp:206
Uml::Changeability::Changeable
Definition: basictypes.h:176
UMLAttribute
This class is used to set up information for an attribute.
Definition: attribute.h:27
UMLAttributeList
This sub-class adds copyInto and clone to the QPtrList base class.
Definition: umlattributelist.h:26
UMLTemplate
This class holds information used by template classes, called paramaterised class in UML and a generi...
Definition: template.h:26
CodeGenerator::getHeadingFile
virtual QString getHeadingFile(const QString &file)
Gets the heading file (as a string) to be inserted at the beginning of the generated file...
Definition: codegenerator.cpp:482
classifier.h
model_utils.h
debug_utils.h
description
static const char description[]
Definition: docgenerators/main.cpp:37
codegenerationpolicy.h
Uml::Visibility::Private
Definition: basictypes.h:58
UMLObject
This class is the non-graphical version of UMLWidget.
Definition: umlobject.h:41
UMLApp::policyExt
CodeGenPolicyExt * policyExt() const
Returns the CodeGenPolicyExt object.
Definition: uml.cpp:2148
UMLAssociation
This class contains the non-graphic representation of an association.
Definition: association.h:32
CPPCodeGenerationPolicy::getStringClassName
QString getStringClassName()
Definition: cppcodegenerationpolicy.cpp:162
SimpleCodeGenerator::m_indentLevel
int m_indentLevel
Definition: simplecodegenerator.h:75
CodeGenerator::forceDoc
bool forceDoc() const
Definition: codegenerator.cpp:841
SimpleCodeGenerator::m_endl
QString m_endl
Definition: simplecodegenerator.h:76
Codegen_Utils::capitalizeFirstLetter
QString capitalizeFirstLetter(const QString &string)
Return the input string with the first letter capitalized.
Definition: codegen_utils.cpp:421
UMLClassifier::isInterface
bool isInterface() const
Returns true if this classifier represents an interface.
Definition: classifier.cpp:112
UMLAttributeListIt
QListIterator< UMLAttribute * > UMLAttributeListIt
Definition: umlattributelist.h:20
CPPCodeGenerationPolicy::getAccessorsAreInline
bool getAccessorsAreInline()
Get the value of m_inlineAccessors.
Definition: cppcodegenerationpolicy.cpp:81
CPPCodeGenerationPolicy::getStringClassNameInclude
QString getStringClassNameInclude()
Definition: cppcodegenerationpolicy.cpp:167
CodeGenerator::codeGenerated
void codeGenerated(UMLClassifier *concept, bool generated)
template.h
Uml::Visibility::Implementation
Definition: basictypes.h:60
UMLAssociation::changeability
Uml::Changeability::Enum changeability(Uml::RoleType::Enum role) const
Returns the changeability.
Definition: association.cpp:518
UMLCanvasObject::getCompositions
UMLAssociationList getCompositions()
Shorthand for getSpecificAssocs(Uml::at_Composition)
Definition: umlcanvasobject.cpp:431
UMLTemplateListIt
QListIterator< UMLTemplate * > UMLTemplateListIt
Definition: umltemplatelist.h:21
UMLClassifierList
QList< UMLClassifier * > UMLClassifierList
Definition: umlclassifierlist.h:17
Uml::AssociationType::Association
Definition: basictypes.h:103
UMLClassifier::hasVectorFields
bool hasVectorFields()
Return true if this classifier has vector fields.
Definition: classifier.cpp:1361
UMLClassifier::getUniAssociationToBeImplemented
virtual UMLAssociationList getUniAssociationToBeImplemented()
Return the list of unidirectional association that should show up in the code.
Definition: classifier.cpp:1369
uDebug
#define uDebug()
Definition: debug_utils.h:95
Uml::Visibility::toString
QString toString(Enum item, bool mnemonic)
Convert Visibility item into QString representation.
Definition: basictypes.cpp:99
CodeGenerator::formatSourceCode
static QString formatSourceCode(const QString &code, const QString &indentation)
Format source code for output in source files by adding the correct indentation to every line of code...
Definition: codegenerator.cpp:803
CppWriter::writeClass
virtual void writeClass(UMLClassifier *c)
Call this method to generate cpp code for a UMLClassifier.
Definition: cppwriter.cpp:93
Uml::Changeability::Frozen
Definition: basictypes.h:177
UMLApp::commonPolicy
CodeGenerationPolicy * commonPolicy() const
Returns the default code generation policy.
Definition: uml.cpp:2132
Uml::ID::Type
std::string Type
Definition: basictypes.h:317
CPPCodeGenerationPolicy::getVectorClassNameInclude
QString getVectorClassNameInclude()
Definition: cppcodegenerationpolicy.cpp:177
UMLOperation::isDestructorOperation
bool isDestructorOperation()
Returns whether this operation is a destructor.
Definition: operation.cpp:373
CodeGenerationPolicy::getAutoGenerateConstructors
bool getAutoGenerateConstructors()
Get the value of m_autoGenerateConstructors.
Definition: codegenerationpolicy.cpp:365
Uml::Visibility::Public
Definition: basictypes.h:57
UMLCanvasObject::getSuperClasses
UMLClassifierList getSuperClasses()
Return a list of the superclasses of this concept.
Definition: umlcanvasobject.cpp:359
UMLObject::baseType
ObjectType baseType() const
Returns the type of the object.
Definition: umlobject.cpp:366
Uml::Changeability::Enum
Enum
Definition: basictypes.h:175
CPPCodeGenerationPolicy
Definition: cppcodegenerationpolicy.h:23
UMLObject::isStatic
bool isStatic() const
Returns true if this UMLObject has classifier scope, otherwise false (the default).
Definition: umlobject.cpp:335
UMLClassifier::getAttributeList
UMLAttributeList getAttributeList() const
Returns the attributes for the specified scope.
Definition: classifier.cpp:441
operation.h
umldoc.h
UMLOperation
This class represents an operation in the UML model.
Definition: operation.h:24
CppWriter::reservedKeywords
virtual QStringList reservedKeywords() const
Get list of reserved keywords.
Definition: cppwriter.cpp:1331
CppWriter::CppWriter
CppWriter()
Constructor, initialises a couple of variables.
Definition: cppwriter.cpp:42
CPPCodeGenerationPolicy::getOperationsAreInline
bool getOperationsAreInline()
Get the value of m_inlineOperations.
Definition: cppcodegenerationpolicy.cpp:100
UMLObject::package
QString package(const QString &separator=QString(), bool includeRoot=false)
Return the package(s) in which this UMLObject is contained as a text.
Definition: umlobject.cpp:603
UMLOperation::getSourceCode
QString getSourceCode() const
Returns the source code for this operation.
Definition: operation.cpp:440
classifierlistitem.h
UMLClassifier::getOpList
UMLOperationList getOpList(bool includeInherited=false, UMLClassifierSet *alreadyTraversed=0)
Return a list of operations for the Classifier.
Definition: classifier.cpp:960
UMLClassifier::hasAccessorMethods
bool hasAccessorMethods()
Return true if this classifier has accessor methods.
Definition: classifier.cpp:1333
UMLAssociation::visibility
Uml::Visibility::Enum visibility(Uml::RoleType::Enum role) const
Returns the Visibility of the given role.
Definition: association.cpp:527
UMLAssociation::getRoleDoc
QString getRoleDoc(Uml::RoleType::Enum role) const
Returns the documentation assigned to the given role.
Definition: association.cpp:554
UMLAssociation::getRoleName
QString getRoleName(Uml::RoleType::Enum role) const
Returns the name assigned to the role A.
Definition: association.cpp:545
UMLObject::name
QString name() const
Returns a copy of m_name.
Definition: umlobject.cpp:185
CodeGenerator::forceSections
bool forceSections() const
Definition: codegenerator.cpp:851
SimpleCodeGenerator::fileName_
QString fileName_
Definition: simplecodegenerator.h:50
CodeGenerator::cleanName
static QString cleanName(const QString &name)
Replaces spaces with underscores and capitalises as defined in m_modname.
Definition: codegenerator.cpp:609
umlclassifierlistitemlist.h
UMLAssociation::getObjectId
Uml::ID::Type getObjectId(Uml::RoleType::Enum role) const
Returns the ID of the UMLObject assigned to the given role.
Definition: association.cpp:488
UMLClassifier::hasAttributes
bool hasAttributes()
Return true if this classifier has attributes.
Definition: classifier.cpp:1310
Codegen_Utils::reservedCppKeywords
const QStringList reservedCppKeywords()
Get list of C++ reserved keywords.
Definition: codegen_utils.cpp:43
UMLClassifier::getTemplateList
UMLTemplateList getTemplateList() const
Returns the templates.
Definition: classifier.cpp:1166
UMLObject::isAbstract
bool isAbstract() const
Returns the abstract state of the object.
Definition: umlobject.cpp:312
UMLOperation::isConstructorOperation
bool isConstructorOperation()
Returns whether this operation is a constructor.
Definition: operation.cpp:352
CppWriter::language
virtual Uml::ProgrammingLanguage::Enum language() const
Returns "C++".
Definition: cppwriter.cpp:76
SimpleCodeGenerator::hasAbstractOps
bool hasAbstractOps(UMLClassifier *c)
Check whether classifier has abstract operations.
Definition: simplecodegenerator.cpp:256
UMLClassifier::hasOperationMethods
bool hasOperationMethods()
Return true if this classifier has operation methods.
Definition: classifier.cpp:1341
UMLAssociation::getMultiplicity
QString getMultiplicity(Uml::RoleType::Enum role) const
Returns the multiplicity assigned to the given role.
Definition: association.cpp:536
UMLClassifierListItem::getTypeName
virtual QString getTypeName() const
Returns the type name of the UMLClassifierListItem.
Definition: classifierlistitem.cpp:110
codegen_utils.h
Uml::Visibility::Protected
Definition: basictypes.h:59
cppwriter.h
CPPCodeGenerationPolicy::getDocToolTag
QString getDocToolTag()
Definition: cppcodegenerationpolicy.cpp:212
SimpleCodeGenerator::indent
QString indent()
Returns the current indent string based on m_indentLevel and m_indentation.
Definition: simplecodegenerator.cpp:74
UMLClassifier::hasAssociations
bool hasAssociations()
Return true if this classifier has associations.
Definition: classifier.cpp:1299
uml.h
QList
UMLObject::doc
QString doc() const
Returns the documentation for the object.
Definition: umlobject.cpp:404
UMLOperation::getParmList
UMLAttributeList getParmList() const
Returns a list of parameters.
Definition: operation.cpp:171
UMLObject::id
virtual Uml::ID::Type id() const
Returns the ID of the object.
Definition: umlobject.cpp:394
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:05:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

umbrello/umbrello

Skip menu "umbrello/umbrello"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal