[Uml-devel] [Bug 53368] C# Code Generation and export
Oliver Kellogg
okellogg at users.sourceforge.net
Wed Feb 21 18:48:21 UTC 2007
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
http://bugs.kde.org/show_bug.cgi?id=53368
------- Additional Comments From okellogg users sourceforge net 2007-02-21 19:48 -------
SVN commit 636046 by okellogg:
Apply attachment 19770 from Ferenc Veres - without visibilityToText(), we can
use Uml::Visibility::toString().
CCBUG:53368
M +84 -122 csharpwriter.cpp
M +36 -8 csharpwriter.h
--- branches/KDE/3.5/kdesdk/umbrello/umbrello/codegenerators/csharpwriter.cpp #636045:636046
@ -223,6 +223,7 @
UMLAssociationList realizations = c->getRealizations();
UMLAssociation *a;
bool isInterface = c->isInterface();
+ m_unnamedRoles = 1;
cs << m_container_indent << "public ";
@ -261,56 +262,15 @
//associations
if (forceSections() || !aggregations.isEmpty()) {
- cs << m_endl << m_container_indent << m_indentation << "#region Aggregations" << m_endl;
- for (a = aggregations.first(); a; a = aggregations.next()) {
- if (c != a->getObject(Uml::A)) // we need to be at the A side
- continue;
- cs << m_endl;
- //maybe we should parse the string here and take multiplicity into account to decide
- //which container to use.
- UMLObject *o = a->getObject(Uml::B);
- if (o == NULL) {
- kError() << "aggregation role B object is NULL" << endl;
- continue;
- }
- QString roleName = cleanName(a->getRoleName(Uml::B));
- if (roleName.isEmpty())
- continue; // CHECK: what should be done if no role name is set?
- QString typeName = cleanName(o->getName());
- if (a->getMulti(Uml::B).isEmpty()) {
- cs << m_container_indent << m_indentation << "//" << typeName
- << " " << roleName << ';' << m_endl;
- } else {
- cs << m_container_indent << m_indentation << "//" << typeName
- << " " << roleName << "Vector = array();" << m_endl;
- }
- }//end for
+ cs << m_endl << m_container_indent << m_indentation << "#region Aggregations" << m_endl << m_endl;
+ writeAssociatedAttributes(aggregations, c, cs);
cs << m_endl << m_container_indent << m_indentation << "#endregion" << m_endl;
}
+ //compositions
if (forceSections() || !compositions.isEmpty()) {
- cs << m_endl << m_container_indent << m_indentation << "#region Compositions" << m_endl;
- for (a = compositions.first(); a ; a = compositions.next()) {
- if (c != a->getObject(Uml::A)) // we need to be at the A side
- continue;
- // see comment on Aggregation about multiplicity...
- UMLObject *o = a->getObject(Uml::B);
- if (o == NULL) {
- kError() << "composition role B object is NULL" << endl;
- continue;
- }
- QString roleName = cleanName(a->getRoleName(Uml::B));
- if (roleName.isEmpty())
- continue; // CHECK: what should be done if no role name is set?
- QString typeName = cleanName(o->getName());
- if (a->getMulti(Uml::B).isEmpty()) {
- cs << m_container_indent << m_indentation << "//" << typeName
- << " " << roleName << ';' << m_endl;
- } else {
- cs << m_container_indent << m_indentation << "//" << typeName
- << " " << roleName << "Vector = array();" << m_endl;
- }
- }
+ cs << m_endl << m_container_indent << m_indentation << "#region Compositions" << m_endl << m_endl;
+ writeAssociatedAttributes(compositions, c, cs);
cs << m_endl << m_container_indent << m_indentation << "#endregion" << m_endl;
}
@ -482,19 +442,7 @
// method visibility
cs << m_container_indent << m_indentation;
if (op->getAbstract()) cs << "abstract ";
- switch (op->getVisibility()) {
- case Uml::Visibility::Public:
- cs << "public ";
- break;
- case Uml::Visibility::Protected:
- cs << "protected ";
- break;
- case Uml::Visibility::Private:
- cs << "private ";
- break;
- default:
- break;
- }
+ cs << op->getVisibility().toString() << " ";
if (op->getStatic()) cs << "static ";
// return type
@ -594,83 +542,97 @
void CSharpWriter::writeAttributes(UMLAttributeList &atList, QTextStream &cs) {
+
for (UMLAttribute *at = atList.first(); at ; at = atList.next()) {
- bool isStatic = at->getStatic();
- if (forceDoc() || !at->getDoc().isEmpty()) {
- cs << m_container_indent << m_indentation << "/// <summary>" << m_endl;
- cs << formatDoc(at->getDoc(), m_container_indent + m_indentation + "/// ");
- cs << m_container_indent << m_indentation << "/// </summary>" << m_endl;
+ bool asProperty = true;
+ if (at->getVisibility() == Uml::Visibility::Private) {
+ asProperty = false;
+ }
+ writeAttribute(at->getDoc(), at->getVisibility(), at->getStatic(),
+ makeLocalTypeName(at), at->getName(), at->getInitialValue(), asProperty, cs);
-/* if (isStatic) cs << m_indentation << " * static" << m_endl;
- switch (at->getVisibility()) {
- case Uml::Visibility::Public:
- cs << m_indentation << " * access public" << m_endl;
- break;
- case Uml::Visibility::Protected:
- cs << m_indentation << " * access protected" << m_endl;
- break;
- case Uml::Visibility::Private:
- cs << m_indentation << " * access private" << m_endl;
- break;
- default:
- break;
- }
- */
- // cs << m_indentation << " */" << m_endl;
+ cs << m_endl;
+ } // end for
+ return;
+}
+void CSharpWriter::writeAssociatedAttributes(UMLAssociationList &associated, UMLClassifier *c, QTextStream &cs) {
+
+ UMLAssociation *a;
+ for (a = associated.first(); a ; a = associated.next()) {
+ if (c != a->getObject(Uml::A)) // we need to be at the A side
+ continue;
+
+ UMLObject *o = a->getObject(Uml::B);
+ if (o == NULL) {
+ kError() << "composition role B object is NULL" << endl;
+ continue;
}
- cs << m_container_indent << m_indentation;
- switch (at->getVisibility()) {
- case Uml::Visibility::Public:
- cs << "public ";
- break;
- case Uml::Visibility::Protected:
- cs << "protected ";
- break;
- case Uml::Visibility::Private:
- cs << "private ";
- break;
- default:
- break;
+ // Take name and documentaton from Role, take type name from the referenced object
+ QString roleName = cleanName(a->getRoleName(Uml::B));
+ QString typeName = cleanName(o->getName());
+ if (roleName.isEmpty()) {
+ roleName = QString("UnnamedRoleB_%1").arg(m_unnamedRoles++);
}
- if (isStatic) cs << "static ";
+ QString roleDoc = a->getRoleDoc(Uml::B);
- //Variable type with/without namespace path
- cs << makeLocalTypeName(at) << " ";
+ //FIXME:maybe we should parse the string here and take multiplicity into account to decide
+ //which container to use.
+ if (a->getMulti(Uml::B).isEmpty()) {
+ writeAttribute(roleDoc, a->getVisibility(Uml::B), false, typeName, roleName, "", ( a->getVisibility(Uml::B) != Uml::Visibility::Private), cs);
+ } else {
+ // FIXME:not updated for C# yet
+ cs << m_container_indent << m_indentation << "//" << typeName
+ << " " << roleName << "Vector = array();" << m_endl;
+ }
+ }
+}
- cs << cleanName(at->getName());
+void CSharpWriter::writeAttribute(QString doc, Uml::Visibility visibility, bool isStatic, QString typeName, QString name, QString initialValue, bool asProperty, QTextStream &cs) {
- // FIXME: may need a GUI switch to not generate as Property?
+ if (forceDoc() || !doc.isEmpty()) {
- // Generate as Property if not private
- if (at->getVisibility() != Uml::Visibility::Private)
- {
- cs << m_endl;
- cs << m_container_indent << m_indentation << "{" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << "get" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << "{" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << m_indentation << "return m_" << cleanName(at->getName()) << ";" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << "}" << m_endl;
+ cs << m_container_indent << m_indentation << "/// <summary>" << m_endl;
+ cs << formatDoc(doc, m_container_indent + m_indentation + "/// ");
+ cs << m_container_indent << m_indentation << "/// </summary>" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << "set" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << "{" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << m_indentation << "m_" << cleanName(at->getName()) << " = value;" << m_endl;
- cs << m_container_indent << m_indentation << m_indentation << "}" << m_endl;
- cs << m_container_indent << m_indentation << "}" << m_endl;
- cs << m_container_indent << m_indentation << "private " << makeLocalTypeName(at) << " m_" << cleanName(at->getName()) << ";" << m_endl;
- }
- else
- {
- cs << ";" << m_endl;
- }
+ }
+ cs << m_container_indent << m_indentation;
+ cs << visibility.toString() << " ";
+ if (isStatic) cs << "static ";
-// if (!at->getInitialValue().isEmpty())
-// cs << " = " << at->getInitialValue();
+ //Variable type with/without namespace path
+ cs << typeName << " ";
+ cs << cleanName(name);
+
+ // FIXME: may need a GUI switch to not generate as Property?
+
+ // Generate as Property if not private
+ if (asProperty)
+ {
cs << m_endl;
- } // end for
- return;
+ cs << m_container_indent << m_indentation << "{" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << "get" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << "{" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << m_indentation << "return m_" << cleanName(name) << ";" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << "}" << m_endl;
+
+ cs << m_container_indent << m_indentation << m_indentation << "set" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << "{" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << m_indentation << "m_" << cleanName(name) << " = value;" << m_endl;
+ cs << m_container_indent << m_indentation << m_indentation << "}" << m_endl;
+ cs << m_container_indent << m_indentation << "}" << m_endl;
+ cs << m_container_indent << m_indentation << "private ";
+ if (isStatic) cs << "static ";
+ cs << typeName << " m_" << cleanName(name);
+ }
+
+ if (!initialValue.isEmpty())
+ cs << " = " << initialValue;
+
+ cs << ";" << m_endl << m_endl;
}
QString CSharpWriter::makeLocalTypeName(UMLClassifierListItem *cl) {
--- branches/KDE/3.5/kdesdk/umbrello/umbrello/codegenerators/csharpwriter.h #636045:636046
@ -21,7 +21,9 @
#include "../umlattributelist.h"
#include "../umloperationlist.h"
#include "../classifierlistitem.h"
+#include "../umlassociationlist.h"
+
/**
* class CSharpWriter is a C# code generator for UMLClassifier objects
* Just call writeClass and feed it a UMLClassifier;
@ -70,39 +72,65 @
* Collection of included namespaces, to skip them from variable types.
*/
UMLPackageList m_seenIncludes;
+
+ /**
+ * Counts associations without a role name for giving a default name.
+ */
+ int m_unnamedRoles;
/**
* write all operations for a given class
*
* param c the concept we are generating code for
- * param php output stream for the PHP file
+ * param cs output stream
*/
- void writeOperations(UMLClassifier *c, QTextStream &php);
+ void writeOperations(UMLClassifier *c, QTextStream &cs);
/**
* write a list of class operations
*
* param classname the name of the class
* param opList the list of operations
- * param php output stream for the PHP file
+ * param cs output stream
* param interface indicates if the operation is an interface member
*/
void writeOperations(const QString &classname, UMLOperationList &opList,
- QTextStream &php,
+ QTextStream &cs,
bool interface = false, bool generateErrorStub = false);
/** write all the attributes of a class
* param c the class we are generating code for
- * param php output stream for the PHP file
+ * param cs output stream
*/
- void writeAttributes(UMLClassifier *c, QTextStream &php);
+ void writeAttributes(UMLClassifier *c, QTextStream &cs);
/** write a list of class attributes
* param atList the list of attributes
- * param php output stream for the PHP file
+ * param cs output stream
*/
- void writeAttributes(UMLAttributeList &atList, QTextStream &php);
+ void writeAttributes(UMLAttributeList &atList, QTextStream &cs);
+ /**
+ * write attributes from associated objects (compositions, aggregations)
+ * param associated list of associated objects
+ * param c currently written class, to see association direction
+ * param cs output stream
+ */
+ void writeAssociatedAttributes(UMLAssociationList &associated, UMLClassifier *c, QTextStream &cs);
+
+ /**
+ * write a single attribute to the output stream
+ * param doc attribute documentation
+ * param visibility attribute visibility
+ * param isStatic static attribute
+ * param typeName class/type of the attribute
+ * param name name of the attribute
+ * param initialValue initial value given to the attribute at declaration
+ * param asProperty true writes as property (get/set), false writes single line variable
+ * param cs output stream
+ */
+ void writeAttribute(QString doc, Uml::Visibility visibility, bool isStatic, QString typeName, QString name, QString initialValue, bool asProperty, QTextStream &cs);
+
/** find the type in used namespaces, if namespace found return short name, complete otherwise.
*
* param at Operation or Attribute to check type
More information about the umbrello-devel
mailing list