summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/codegenerators/dwriter.h
blob: 38828359bcbb28cb7fa79432c0d2c6c8a3788bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2007 Jari-Matti Mäkelä <jmjm@iki.fi>                    *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

/***************************************************************************
    This is the "old" code generator that does not support code editing
    in the Modeller but uses significantly less file space because the
    source code is not replicated in the XMI file.
 ***************************************************************************/

#ifndef DWRITER_H
#define DWRITER_H

#include "simplecodegenerator.h"
#include "../umloperationlist.h"
#include "../umlattributelist.h"
#include "../umlassociationlist.h"

class UMLOperation;

/**
  * class DWriter is a code generator for UMLClassifier objects.
  * Create an instance of this class, and feed it a UMLClassifier when
  * calling writeClass and it will generate a d source file for
  * that concept
  */
class DWriter : public SimpleCodeGenerator {
public:

    /**
     * Constructor, initialises a couple of variables
     */
    DWriter();

    /**
     * Destructor, empty
     */
    virtual ~DWriter();

    /**
     * call this method to generate d code for a UMLClassifier
     * @param c the class to generate code for
     */
    virtual void writeClass(UMLClassifier *c);

    /**
     * returns "D"
     */
    virtual Uml::Programming_Language getLanguage();

    /**
     * Overrides method from class CodeGenerator
     */
    QStringList defaultDatatypes();

private:

    /**
     * Writes the module declaration.
     */
    void writeModuleDecl(UMLClassifier *c, QTextStream &d);

    /**
     * Writes the module imports.
     */
    void writeModuleImports(UMLClassifier *c, QTextStream &d);

    /**
     * Writes class's documentation then the class header
     * public abstract class Foo extents {
     */
    void writeClassDecl(UMLClassifier *c, QTextStream &d);

    /**
     * Writes the comment and class constructor
     */
    void writeConstructor(UMLClassifier *c, QTextStream &d);

    /**
     * return true if the two operations have the same name and the same parameters
     * @param op1 first operation to be compared
     * @param op2 second operation to be compared
     */
    static bool compareDMethod(UMLOperation *op1, UMLOperation *op2);

    /**
     * return true if the operation is in the list
     * @param umlOp operation to be searched
     * @param opl list of operations
     */
    static bool dMethodInList(UMLOperation *umlOp, UMLOperationList &opl);

    /**
     * get all operations which a given class inherit from all its super interfaces and get all operations
     * which this given class inherit from all its super classes
     * @param c the class for which we are generating code
     * @param yetImplementedOpList the list of yet implemented operations
     * @param toBeImplementedOpList the list of to be implemented operations
     * @param noClassInPath tells if there is a class between the base class and the current interface
     */
    void getSuperImplementedOperations(UMLClassifier *c, UMLOperationList &yetImplementedOpList ,UMLOperationList &toBeImplementedOpList, bool noClassInPath = true);

    /**
     * get all operations which a given class inherit from all its super interfaces and that should be implemented
     * @param c the class for which we are generating code
     * @param opl the list of operations used to append the operations
     */
    void getInterfacesOperationsToBeImplemented(UMLClassifier *c, UMLOperationList &opl);

     /**
     * write all operations for a given class
     * @param c the class for which we are generating code
     * @param j the stream associated with the output file
     */
    void writeOperations(UMLClassifier *c, QTextStream &j);

    /**
     * write a list of operations for a given class
     * @param list the list of operations you want to write
     * @param j the stream associated with the output file
     */
    void writeOperations(UMLOperationList &list, QTextStream &j);

    /**
     * write all attributes for a given class
     * @param c the class for which we are generating code
     * @param j the stream associated with the output file
     */
    void writeAttributes(UMLClassifier *c, QTextStream &j);

    /**
     * Writes the protection modifier line.
     * @param visibility protection modifier
     * @param d text stream
     */
    void writeProtectionMod(Uml::Visibility visibility, QTextStream &d);
    
    /**
     * Writes attribute declarations with a specific
     * protection modifier.
     * @param prot the protection modifier
     * @param atlist attribute list
     * @param d text stream
     */
    void writeAttributeDecl(Uml::Visibility visibility, UMLAttributeList &atlist, QTextStream &d);

    /**
     * writes the Attribute declarations
     * @param atpub List of public attributes
     * @param atprot list of protected attributes
     * @param atpriv list of private attributes
     * @param d text stream
     */
    void writeAttributeDecls(UMLAttributeList &atpub, UMLAttributeList &atprot,
                             UMLAttributeList &atpriv, QTextStream &d );

    /**
     * Searches a list of associations for appropriate ones to write out as attributes
     */
    void writeAssociationDecls(UMLAssociationList associations, Uml::IDType id, QTextStream &d);

    /**
     * Writes out an association as an attribute using Vector
     */
    void writeAssociationRoleDecl(QString fieldClassName, QString roleName, QString multi,
                                  QString doc, Uml::Visibility visib, QTextStream &d);

    /**
     * calls @ref writeSingleAttributeAccessorMethods() on each of the attributes in atpub
     */
    void writeAttributeMethods(UMLAttributeList &atpub, Uml::Visibility visibility, QTextStream &d);

    /**
     * calls @ref writeAssociationRoleMethod() on each of the associations in the given list
     */
    void writeAssociationMethods(UMLAssociationList associations, UMLClassifier *thisClass,
                                 QTextStream &d);

    /**
     * calls @ref writeSingleAttributeAccessorMethods() or @ref
     * writeVectorAttributeAccessorMethods() on the assocaition
     * role
     */
    void writeAssociationRoleMethod(QString fieldClassName, QString roleName, QString multi,
                                    QString description, Uml::Visibility visib, Uml::Changeability_Type change,
                                    QTextStream &d);

    /**
     * Writes getFoo() and setFoo() accessor methods for the attribute
     */
    void writeSingleAttributeAccessorMethods(QString fieldClassName, QString fieldVarName,
            QString fieldName, QString description,
            Uml::Visibility visibility, Uml::Changeability_Type change,
            bool isFinal, QTextStream &d);

    /**
     * Writes addFoo() and removeFoo() accessor methods for the Vector attribute
     */
    void writeVectorAttributeAccessorMethods(QString fieldClassName, QString fieldVarName,
            QString fieldName, QString description,
            Uml::Visibility visibility, Uml::Changeability_Type change,
            QTextStream &d);

    /**
     * Writes a // style comment
     */
    void writeComment(const QString &text, const QString &indent, QTextStream &d, bool dDocStyle=false);

    /**
     * Writes a documentation comment
     */
    void writeDocumentation(QString header, QString body, QString end, QString indent, QTextStream &d);

    /**
     * Returns the name of the given object (if it exists)
     */
    QString getUMLObjectName(UMLObject *obj);

    /**
     * Lowers the case of the first letter in the given string
     */
    QString deCapitaliseFirstLetter(QString string);

    /**
     * Returns the plural form of a subject.
     */
    QString pluralize(QString string);
 
    /**
     * Returns the non-plural form of a subject.
     */
    QString unPluralize(QString string);
    
    /**
     * Replaces `string' with `String' and `bool' with `boolean'
     */
    QString fixTypeName(const QString& string);

    /**
     * check that initial values of strings have quotes around them
     */
    QString fixInitialStringDeclValue(QString value, QString type);

    /**
     * Write a blank line
     */
    void writeBlankLine(QTextStream& d);

    /**
     * a little method for converting scope to string value
     */
    QString scopeToDDecl(Uml::Visibility scope);

    /**
     * A \n, used at the end of each line
     */
    QString startline;

    /**
     * Whether or not this concept is an interface.
     */
    bool isInterface;

};


#endif // DWRITER_H