umbrello: CodeGenerator Class Reference

umbrello 2.39.1 Umbrello UML Modeller is a Unified Modelling Language (UML) diagram program based on KDE Technology

- Main Page
- Related Pages
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Types | Public Slots | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
CodeGenerator Class Referenceabstract

`#include <codegenerator.h>`

Inheritance diagram for CodeGenerator:

## Public Types
enum | GenerationState { Generated , Failed , Skipped }

## Public Slots
virtual void | syncCodeToDocument ()

## Signals
void | codeGenerated (UMLClassifier *classifier, bool generated)
void | codeGenerated (UMLClassifier *classifier, CodeGenerator::GenerationState result)
void | showGeneratedFile (const QString &filename)

## Public Member Functions
| CodeGenerator ()
virtual | ~CodeGenerator ()
bool | addCodeDocument (CodeDocument *add_object)
bool | removeCodeDocument (CodeDocument *remove_object)
CodeDocumentList * | getCodeDocumentList ()
QString | getUniqueID (CodeDocument *codeDoc)
virtual void | saveToXMI (QXmlStreamWriter &writer)
CodeDocument * | findCodeDocumentByID (const QString &id)
virtual void | writeCodeToFile ()
virtual void | writeCodeToFile (UMLClassifierList &list)
void | setIncludeHeadings (bool i)
bool | includeHeadings () const
void | setHeadingFileDir (const QString &)
QString | headingFileDir () const
void | setForceDoc (bool f)
bool | forceDoc () const
void | setSectionCommentPolicy (CodeGenerationPolicy::WriteSectionCommentsPolicy f)
bool | forceSections () const
virtual QString | getHeadingFile (const QString &file)
QString | findFileName (CodeDocument *codeDocument)
virtual void | loadFromXMI (QDomElement &element)
virtual Uml::ProgrammingLanguage::Enum | language () const =0
CodeDocument * | findCodeDocumentByClassifier (UMLClassifier *classifier)
virtual QStringList | defaultDatatypes () const
virtual bool | isReservedKeyword (const QString &keyword)
virtual QStringList | reservedKeywords () const
virtual void | createDefaultStereotypes ()
virtual void | initFromParentDocument ()=0

## Static Public Member Functions
static QString | cleanName (const QString &name)
static QString | formatDoc (const QString &text, const QString &linePrefix=QStringLiteral(" *"), int lineWidth=80)
static QString | formatFullDocBlock (const QString &text, const QString &blockHeader=QStringLiteral("/* "), const QString &blockFooter=QStringLiteral(" */"), const QString &linePrefix=QStringLiteral(" *"), int lineWidth=80)
static QString | formatSourceCode (const QString &code, const QString &indentation)
static void | findObjectsRelated (UMLClassifier *c, UMLPackageList &cList)

## Protected Member Functions
QString | overwritableName (const QString &name, const QString &extension)
bool | openFile (QFile &file, const QString &name)
void | writeListedCodeDocsToFile (CodeDocumentList *docs)
virtual void | finalizeRun ()

## Protected Attributes
QHash< QString, CodeDocument * > | m_codeDocumentDictionary
bool | m_applyToAllRemaining
UMLDoc * | m_document

## Private Member Functions
void | loadCodeForOperation (const QString &id, const QDomElement &codeDocElement)

## Private Attributes
CodeDocumentList | m_codedocumentVector
int | m_lastIDIndex

## Detailed Description

This class collects together all of the code documents which form this project, and generates code for them in a given language.

CodeGenerator is the abstract base class for all types of code generators. It provides the interface through which all generators are invoked and provides most of the basic functionality.

If you want to implement a CodeGenerator for some language follow these steps:

Create a class which inherits SimpleCodeGenerator or AdvancedCodeGenerator. This class can have any name, I use names like CppCodeGenerator for the Cpp Generator, JavaCodeGenerator for the Java Generator and so on, but you can use what you want.

The code you generate should be output to getOutputDirectory() of the CodeGenerationPolicy instance and you should respect the OverwritePolicy specified. You should call findFileName(..) to get an appropriate file name, and then you can call openFile() and write the generated content to this file.

## Member Enumeration Documentation

## ◆ GenerationState

enum CodeGenerator::GenerationState

Enumerator
Generated |
Failed |
Skipped |

## Constructor & Destructor Documentation

## ◆ CodeGenerator()

CodeGenerator::CodeGenerator | ( |  | ) |

Constructor for a code generator.

## ◆ ~CodeGenerator()

CodeGenerator::~CodeGenerator | ( |  | ) |
| virtual

Destructor.

## Member Function Documentation

## ◆ addCodeDocument()

bool CodeGenerator::addCodeDocument | ( | CodeDocument * | doc | ) |

Add a CodeDocument object to the m_codedocumentVector List.

Returns
  boolean - will return false if it couldnt add a document

## ◆ cleanName()

QString CodeGenerator::cleanName | ( | const QString & | name | ) |
| static

Replaces spaces with underscores and capitalises as defined in m_modname

Returns
  QString

Parameters

  name |

## ◆ codeGenerated [1/2]

void CodeGenerator::codeGenerated | ( | UMLClassifier * | classifier,
|  | bool | generated )
| signal

## ◆ codeGenerated [2/2]

void CodeGenerator::codeGenerated | ( | UMLClassifier * | classifier,
|  | CodeGenerator::GenerationState | result )
| signal

## ◆ createDefaultStereotypes()

void CodeGenerator::createDefaultStereotypes | ( |  | ) |
| virtual

Create the default stereotypes for your language (constructor, int etc).

Reimplemented in CPPCodeGenerator.

## ◆ defaultDatatypes()

QStringList CodeGenerator::defaultDatatypes | ( |  | ) | const
| virtual

Return the default datatypes for your language (bool, int etc). Default implementation returns empty list.

Reimplemented in AdaWriter, CPPCodeGenerator, CppWriter, CSharpWriter, DCodeGenerator, DWriter, IDLWriter, JavaCodeGenerator, JavaWriter, MySQLWriter, PascalWriter, PerlWriter, PostgreSQLWriter, PythonWriter, SQLWriter, and ValaWriter.

## ◆ finalizeRun()

void CodeGenerator::finalizeRun | ( |  | ) |
| protectedvirtual

A single call to writeCodeToFile() usually entails processing many items (e.g. as classifiers) for which code is generated. This method is called after all code of one call to writeCodeToFile() has been generated. It can be reimplemented by concrete code generators to perform additional cleanups or other actions that can only be performed once all code has been written.

Reimplemented in AdaWriter.

## ◆ findCodeDocumentByClassifier()

CodeDocument * CodeGenerator::findCodeDocumentByClassifier | ( | UMLClassifier * | classifier | ) |

Find a code document by the given classifier. NOTE: FIX, this should be 'protected' or we could have problems with CPP code generator

Returns
  CodeDocument

Parameters

  classifier |

## ◆ findCodeDocumentByID()

CodeDocument * CodeGenerator::findCodeDocumentByID | ( | const QString & | tag | ) |

Find a code document by the given id string.

Returns
  CodeDocument

## ◆ findFileName()

QString CodeGenerator::findFileName | ( | CodeDocument * | codeDocument | ) |

Finds an appropriate file name for the given CodeDocument, taking into account the Overwrite Policy and asking the user what to do if need be (if policy == Ask).

Parameters

  codeDocument | the CodeDocument for which an output file name is desired.

Returns
  the file name that should be used. (with extension) or NULL if none to be used

## ◆ findObjectsRelated()

void CodeGenerator::findObjectsRelated | ( | UMLClassifier * | c,
|  | UMLPackageList & | cList )
| static

Finds all classes in the current document to which objects of class c are in some way related. Possible relations are Associations (generalization, composition, etc) as well as parameters to methods and return values this is useful in deciding which classes/files to import/include in code generation

Parameters

  c | the class for which relations are to be found
  cList | a reference to the list into which return the result

## ◆ forceDoc()

bool CodeGenerator::forceDoc | ( |  | ) | const

## ◆ forceSections()

bool CodeGenerator::forceSections | ( |  | ) | const

## ◆ formatDoc()

QString CodeGenerator::formatDoc | ( | const QString & | text,
|  | const QString & | linePrefix = QStringLiteral(" *"),
|  | int | lineWidth = 80 )
| static

Format documentation for output in source files

Parameters

  text | the documentation which has to be formatted
  linePrefix | the prefix which has to be added in the beginnig of each line
  lineWidth | the line width used for word-wrapping the documentation

Returns
  the formatted documentation text

## ◆ formatFullDocBlock()

QString CodeGenerator::formatFullDocBlock | ( | const QString & | text,
|  | const QString & | blockHeader = QStringLiteral("/* "),
|  | const QString & | blockFooter = QStringLiteral(" */"),
|  | const QString & | linePrefix = QStringLiteral(" *"),
|  | int | lineWidth = 80 )
| static

Format full documentation block for output in source files

Parameters

  text | the documentation which has to be formatted
  blockHeader | the prefix which has to be added in the beginning of each line (instead of the first)
  blockFooter | the prefix which has to be added in the beginning of each line (instead of the first)
  linePrefix | the prefix which has to be added in the beginning of each line (instead of the first)
  lineWidth | the line width used for word-wrapping the documentation

Returns
  the formatted documentation text

## ◆ formatSourceCode()

QString CodeGenerator::formatSourceCode | ( | const QString & | code,
|  | const QString & | indentation )
| static

Format source code for output in source files by adding the correct indentation to every line of code.

Parameters

  code | the source code block which has to be formatted
  indentation | the blanks to indent

## ◆ getCodeDocumentList()

CodeDocumentList * CodeGenerator::getCodeDocumentList | ( |  | ) |

Get the list of CodeDocument objects held by m_codedocumentVector.

Returns
  CodeDocumentList list of CodeDocument objects held by m_codedocumentVector

## ◆ getHeadingFile()

QString CodeGenerator::getHeadingFile | ( | const QString & | file | ) |
| virtual

Gets the heading file (as a string) to be inserted at the beginning of the generated file. you give the file type as parameter and get the string. if fileName starts with a period (.) then fileName is the extension (.cpp, .h, .java) if fileName starts with another character you are requesting a specific file (mylicensefile.txt). The files can have parameters which are denoted by parameter%.

current parameters are author% date% time% filepath%

Returns
  QString

Parameters

  file |

## ◆ getUniqueID()

QString CodeGenerator::getUniqueID | ( | CodeDocument * | codeDoc | ) |

Get a unique id for this codedocument.

Returns
  id for the codedocument

## ◆ headingFileDir()

QString CodeGenerator::headingFileDir | ( |  | ) | const

## ◆ includeHeadings()

bool CodeGenerator::includeHeadings | ( |  | ) | const

## ◆ initFromParentDocument()

virtual void CodeGenerator::initFromParentDocument | ( |  | ) |
| pure virtual

Initialize this code generator from its parent UMLDoc.

Implemented in AdvancedCodeGenerator, CPPCodeGenerator, and SimpleCodeGenerator.

## ◆ isReservedKeyword()

bool CodeGenerator::isReservedKeyword | ( | const QString & | keyword | ) |
| virtual

Check whether the given string is a reserved word for the language of this code generator.

Parameters

  keyword | string to check

Reimplemented in AdaWriter, and PascalWriter.

## ◆ language()

virtual Uml::ProgrammingLanguage::Enum CodeGenerator::language | ( |  | ) | const
| pure virtual

Return the unique language enum that identifies this type of code generator.

Implemented in AdaWriter, ASWriter, CPPCodeGenerator, CppWriter, CSharpWriter, DCodeGenerator, DWriter, IDLWriter, JavaCodeGenerator, JavaWriter, JSWriter, MySQLWriter, PascalWriter, PerlWriter, Php5Writer, PhpWriter, PostgreSQLWriter, PythonWriter, RubyCodeGenerator, RubyWriter, SQLWriter, TclWriter, ValaWriter, and XMLSchemaWriter.

## ◆ loadCodeForOperation()

void CodeGenerator::loadCodeForOperation | ( | const QString & | idStr,
|  | const QDomElement & | codeDocElement )
| private

Extract and load code for operations from xmi section. Probably we have code which was entered in classpropdlg for an operation.

## ◆ loadFromXMI()

void CodeGenerator::loadFromXMI | ( | QDomElement & | qElement | ) |
| virtual

A series of accessor method constructors that we need to define for any particular language.

Load codegenerator data from xmi.

Parameters

  qElement | the element from which to load

## ◆ openFile()

bool CodeGenerator::openFile | ( | QFile & | file,
|  | const QString & | fileName )
| protected

Opens a file named "name" for writing in the outputDirectory. If something goes wrong, it informs the user if this function returns true, you know you can write to the file.

Parameters

  file | file descriptor
  fileName | the name of the file

Returns
  success state

## ◆ overwritableName()

QString CodeGenerator::overwritableName | ( | const QString & | name,
|  | const QString & | extension )
| protected

Returns a name that can be written to in the output directory, respecting the overwrite policy. If a file of the given name and extension does not exist, then just returns the name. If a file of the given name and extension does exist, then opens an overwrite dialog. In this case the name returned may be a modification of the input name. This method is invoked by findFileName().

Parameters

  name | the proposed output file name
  extension | the extension to use

Returns
  the real file name that should be used (including extension) or QString() if none to be used

## ◆ removeCodeDocument()

bool CodeGenerator::removeCodeDocument | ( | CodeDocument * | remove_object | ) |

Remove a CodeDocument object from m_codedocumentVector List.

Returns
  boolean - will return false if it couldnt remove a document

## ◆ reservedKeywords()

QStringList CodeGenerator::reservedKeywords | ( |  | ) | const
| virtual

Get list of reserved keywords.

Reimplemented in AdaWriter, ASWriter, CPPCodeGenerator, CppWriter, CSharpWriter, DCodeGenerator, IDLWriter, JavaCodeGenerator, JSWriter, PascalWriter, PerlWriter, Php5Writer, PhpWriter, PythonWriter, RubyCodeGenerator, RubyWriter, SQLWriter, TclWriter, ValaWriter, and XMLSchemaWriter.

## ◆ saveToXMI()

void CodeGenerator::saveToXMI | ( | QXmlStreamWriter & | writer | ) |
| virtual

Save the XMI representation of this object

Reimplemented in CPPCodeGenerator.

## ◆ setForceDoc()

void CodeGenerator::setForceDoc | ( | bool | f | ) |

## ◆ setHeadingFileDir()

void CodeGenerator::setHeadingFileDir | ( | const QString & |  | ) |

## ◆ setIncludeHeadings()

void CodeGenerator::setIncludeHeadings | ( | bool | i | ) |

## ◆ setSectionCommentPolicy()

void CodeGenerator::setSectionCommentPolicy | ( | CodeGenerationPolicy::WriteSectionCommentsPolicy | f | ) |

## ◆ showGeneratedFile

void CodeGenerator::showGeneratedFile | ( | const QString & | filename | ) |
| signal

## ◆ syncCodeToDocument

void CodeGenerator::syncCodeToDocument | ( |  | ) |
| virtualslot

Force a synchronize of this code generator, and its present contents, to that of the parent UMLDocument. "UserGenerated" code will be preserved, but Autogenerated contents will be updated/replaced or removed as is apppropriate.

## ◆ writeCodeToFile() [1/2]

void CodeGenerator::writeCodeToFile | ( |  | ) |
| virtual

This method is here to provide class wizard the ability to write out only those classes which are selected by the user.

Reimplemented in CPPCodeGenerator, and SimpleCodeGenerator.

## ◆ writeCodeToFile() [2/2]

void CodeGenerator::writeCodeToFile | ( | UMLClassifierList & | concepts | ) |
| virtual

This method is here to provide class wizard the ability to write out only those classes which are selected by the user.

Reimplemented in CPPCodeGenerator, and SimpleCodeGenerator.

## ◆ writeListedCodeDocsToFile()

void CodeGenerator::writeListedCodeDocsToFile | ( | CodeDocumentList * | docs | ) |
| protected

The actual internal routine which writes code documents.

## Member Data Documentation

## ◆ m_applyToAllRemaining

bool CodeGenerator::m_applyToAllRemaining
| protected

Used by OverwriteDialog to know if the apply to all remaining files checkbox should be checked (is by default).

## ◆ m_codeDocumentDictionary

QHash<QString, CodeDocument*> CodeGenerator::m_codeDocumentDictionary
| protected

## ◆ m_codedocumentVector

CodeDocumentList CodeGenerator::m_codedocumentVector
| private

## ◆ m_document

UMLDoc* CodeGenerator::m_document
| protected

The document object.

## ◆ m_lastIDIndex

int CodeGenerator::m_lastIDIndex
| private

The documentation for this class was generated from the following files:

- umbrello/codegenerators/codegenerator.h
- umbrello/codegenerators/codegenerator.cpp