summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/codegenerator.h
blob: aa8a9188e82b5198a00ee591dd9ad5e8e238f35b (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/***************************************************************************
 *                                                                         *
 *   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) 2004-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

/*  This code generated by:
 *      Author : thomas
 *      Date   : Thu Jun 19 2003
 *  Bugs and comments to uml-devel@lists.sf.net or http://bugs.kde.org
 */


#ifndef CODEGENERATOR_H
#define CODEGENERATOR_H

#include <tqdict.h>
#include <tqdir.h>

#include "codegenerators/codegenpolicyext.h"
#include "codegenerationpolicy.h"
#include "umlpackagelist.h"
#include "umlclassifierlist.h"
#include "codedocumentlist.h"
#include "codeviewerstate.h"
#include "umlnamespace.h"

class UMLAttribute;
class UMLDoc;
class UMLObject;
class UMLRole;
class UMLOperation;

class ClassifierCodeDocument;
class CodeAccessorMethod;
class CodeBlock;
class CodeBlockWithComments;
class CodeClassField;
class CodeClassFieldDeclarationBlock;
class CodeComment;
class CodeDocument;
class CodeOperation;
class CodeViewerDialog;

class TDEConfig;

/**
  * class CodeGenerator
  * 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 base class for all CodeGenerators. It
 * provides the interface through which all Generators are invoked and
 * the all the basic functionality. The only thing it doesn't do is to
 * generate code =)
 *
 * If you want to implement a CodeGenerator for some language follow
 * these steps:
 *
 * Create a class which inherits CodeGenerator. 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 "outputDirectory" and you
 * should respect the OverwritePolicy specified. You should call
 * findFileName(..) to get an appropriate file name, and then you can
 * call openFile if you want, but if you want to do it yourself you
 *
 * Finally put your generator in a library which can be dlopened
 * together with a factory class (see below) and you are ready to go.
 */


class CodeGenerator : public TQObject {
    Q_OBJECT
  

public:

    // Constructors/Destructors
    //

    /**
     * Constructors
     */

    /**
     * Build a code generator.
     * @return      CodeGenerator
     */
    CodeGenerator ();

    /**
     * Build a code generator and then initialize it from an XMI element.
     * @return      CodeGenerator
     * @param       element an element from an XMI document
     */
    CodeGenerator (TQDomElement & element );

    /**
     * Empty Destructor
     */
    virtual ~CodeGenerator ( );

    // Public attribute accessor methods
    //

    /**
     * Add a CodeDocument object to the m_codedocumentVector List
     * @return boolean - will return false if it couldnt add a document.
     */
    bool addCodeDocument ( CodeDocument * add_object );

    /**
     * Replace (or possibly add a new) CodeDocument object to the m_codedocumentVector List.
     * As names must be unique and each code document must have a name.
     * @return  boolean value which will be true if the passed document was able to replace some
     *    other document OR was added(no prior document existed..only when addIfPriorDocumentNotPresent is true).
     *    The document which was replaced will be deleted IF deleteReplacedDocument is true.
     */
    // bool replaceCodeDocument (  CodeDocument * replace_doc=0, bool addIfPriorDocumentNotPresent=true,
    //                                bool deleteReplacedDocument=true );

    /**
     * Remove a CodeDocument object from m_codedocumentVector List
     * @return boolean - will return false if it couldnt remove a document.
     */
    bool removeCodeDocument ( CodeDocument * remove_object );

    /**
     * Get the list of CodeDocument objects held by m_codedocumentVector
     * @return CodeDocumentList list of CodeDocument objects held by
     * m_codedocumentVector
     */
    CodeDocumentList * getCodeDocumentList ( );

    // get a unique id for this codedocument
    TQString getUniqueID ( CodeDocument * codeDoc );

    /**
     * Save the XMI representation of this object
     */
    virtual void saveToXMI ( TQDomDocument & doc, TQDomElement & root );

    /**
     * Find a code document by the given id string.
     * @return  CodeDocument
         */
    CodeDocument * findCodeDocumentByID (const TQString &id );

    /**
     * Write out all code documents to file as appropriate.
     */
    virtual void writeCodeToFile ( );

    // this method is here to provide class wizard the
    // ability to write out only those classes which
    // are selected by the user.
    virtual void writeCodeToFile(UMLClassifierList &list);

    // these are utility methods for accessing the default
    // code gen policy object and *perhaps* should go away when we
    // finally implement the CodeGenDialog class -b.t.

    void setModifyNamePolicy(CodeGenerationPolicy::ModifyNamePolicy p);
    CodeGenerationPolicy::ModifyNamePolicy modifyNamePolicy()const;

    void setIncludeHeadings(bool i);
    bool includeHeadings() const;

    void setHeadingFileDir(const TQString &);
    TQString headingFileDir() const;

    void setForceDoc(bool f);
    bool forceDoc() const;

    void setForceSections(bool f);
    bool forceSections() const;


    /**
     *  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%
     *
     * @return      TQString
     * @param       file
     */
    virtual TQString getHeadingFile (const TQString &file );

    /**
     * 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).
     *
     * @param doc  the CodeDocument for which an output file name is desired.
     * @return the file name that should be used. (with extension) or
     *      NULL if none to be used
     */
    virtual TQString findFileName(CodeDocument * doc);

    /**
     * Replaces spaces with underscores and capitalises as defined in m_modname
     * @return      TQString
     * @param       name
     */
    static TQString cleanName ( const TQString &name );

    /** Format documentation for output in source files
    *
    * @param text the documentation which has to be formatted
    * @param linePrefix the prefix which has to be added in the beginnig of each line
    * @param lineWidth the line width used for word-wrapping the documentation
    *
    * @return the formatted documentation text
    */
    TQString formatDoc (const TQString & text, const TQString & linePrefix = " *", int lineWidth = 80 );

    /**
    * 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
    * @param c the class for which relations are to be found
    * @param cList a reference to the list into which return the result
    */
    static void findObjectsRelated(UMLClassifier *c, UMLPackageList &cList);

    // a series of accessor method constructors that we need to define
    // for any particular language.
    virtual CodeDocument * newClassifierCodeDocument (UMLClassifier * classifier ) = 0;

    /**
     * @param       element
     */
    virtual void loadFromXMI (TQDomElement & element );

    /**
         * Create a new Code document belonging to this package.
     * @return CodeDocument pointer to new code document.
     */
    virtual CodeDocument * newCodeDocument ( );

    /**
     * Return the unique language enum that identifies this type of code generator
     */
    virtual Uml::Programming_Language getLanguage() = 0;

    /**
     * Find a code document by the given classifier.
     * @return      CodeDocument
     * @param       classifier
     */
    //FIX
    // NOTE: this should be 'protected' or we could have problems with CPP code generator
    CodeDocument * findCodeDocumentByClassifier (UMLClassifier * classifier );

    /**
     * Return the default datatypes for your language (bool, int etc)
     * Default implementation returns empty list.
     */
    virtual TQStringList defaultDatatypes();

    /** Get the editing dialog for this code document
     */
    virtual CodeViewerDialog * getCodeViewerDialog( TQWidget* parent, CodeDocument * doc,
            Settings::CodeViewerState state);

    /**
     * Check whether the given string is a reserved word for the
     * language of this code generator
     *
     * @param rPossiblyReservedKeyword is the string to check
     *
     */
    virtual bool isReservedKeyword(const TQString & rPossiblyReservedKeyword);

    /**
     * get list of reserved keywords
     */
    virtual const TQStringList reservedKeywords() const;

    /**
     * Create the default stereotypes for your language (constructor, int etc)
     */
    virtual void createDefaultStereotypes ();

    /**
    * Initialize this code generator from its parent UMLDoc. When this is called,
    * it will (re-)generate the list of code documents for this project (generator)
    * by checking for new objects/attributes which have been added or changed in the
    * document. One or more CodeDocuments will be created/overwritten/amended as is
    * appropriate for the given language.
    */
    virtual void initFromParentDocument( );

    /**
     * Connect additional slots.
     * Only required for Advanced Code Generators.
     * To be called after constructing the code generator (see CodeGenFactory)
     */
    void connect_newcodegen_slots();

protected:

    /**
     * Remove (and possibly delete) all AutoGenerated content type CodeDocuments but leave
     * the UserGenerated (and any other type) documents in this generator alone.
     */
    // void removeAndDeleteAllAutoGeneratedCodeDocuments ( bool deleteRemovedDocs=true );

    /**
     * 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().
     *
     * @param name the proposed output file name
     * @param extension the extension to use
     * @return the real file name that should be used (including extension) or
     *      TQString() if none to be used
     */
    TQString overwritableName (const TQString& name, const TQString &extension );

    /** 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
     * @return      bool
     * @param       file
     * @param       name
     */
    bool openFile (TQFile& file, const TQString &name);

    /** the actual internal routine which writes code documents
     */
    void writeListedCodeDocsToFile(CodeDocumentList * docs);

    static const char * hierarchicalCodeBlockNodeName;

    // map of what code documents we currently have in this generator.
    TQDict<CodeDocument> m_codeDocumentDictionary;

    /**
     * used by overwriteDialogue to know if the apply to all
     * remaining files checkbox should be checked (is by default)
     */
    bool m_applyToAllRemaining;

    /**
     * The document object
     */
    UMLDoc* m_document;

private:

    /**
     * Maps CodeDocuments to filenames. Used for finding out which file
     * each class was written to.
     */
    // this seems silly and overkill now. -b.t.
    // TQMap<CodeDocument*,TQString> *m_fileMap;

    CodeDocumentList m_codedocumentVector;
    int lastIDIndex;

    void initFields() ;

public slots:

    /** These 2 functions check for adding or removing objects to the UMLDocument */
    virtual void checkAddUMLObject (UMLObject * obj);
    virtual void checkRemoveUMLObject (UMLObject * obj);

    /**
     * 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.
     */
    virtual void syncCodeToDocument ( );

signals:

    /**
     * This signal is emitted when code for a UMLClassifier has been
     * generated. Its only really used by the codegenerationwizard to
     * update its progress.
     * @param concept    The concept which was processed
     * @param generated  Flag, set to true if generation was successful
     */
    void codeGenerated(UMLClassifier* concept, bool generated);

};

#endif // CODEGENERATOR_H