1 /*
2 This file is part of dia2code. It generates code from an UML Dia Diagram.
3 Copyright (C) 2014-2014 Javier O'Hara - Oliver Kellogg
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef GENERATE_CODE_HPP
20 #define GENERATE_CODE_HPP
21 
22 #include "config.h"
23 
24 #include <fstream>
25 #include <cstdint>
26 
27 #include "DiaGram.hpp"
28 #include "umlClass.hpp"
29 #include "umlAssoc.hpp"
30 
31 /**
32  * Abstract base class for code generators.
33  */
34 class GenerateCode {
35     private:
36         DiaGram &   dia;  ///< Diagram to generate into code
37         std::string license;  ///< License file
38 
39         std::string outdir;   ///< Output directory
40         std::string file_ext;   ///< File extension
41         std::string body_file_ext;  ///< Implementation file extension (for languages where body is generated)
42         std::list <std::ofstream *> file;  ///< Stack of files generated
43         uint8_t     version;  ///< Version of language to generate. For C++: 99 = C++99, 11 = C++11
44         uint8_t     indent : 4;  ///< Number of spaces for one indentation
45         uint8_t     indentlevel : 4;  ///< Current indentation level
46         bool        overwrite : 1;  ///< Overwrite files while generating code
47         bool        buildtree : 1;  ///< Convert package names to a directory tree
48         bool        bOpenBraceOnNewline : 1;   ///< Place opening brace on a new line
49         bool        oneClassOneHeader : 1;     ///< Create separate file for each classifier
50         /**
51          * True if the language handles include package.
52          * If false, include ALL classes inside the package and its children.
53          */
54         bool        handleIncludePackage : 1;
55         bool        noLoopSupport : 1;
56 #ifdef ENABLE_CORBA
57         bool        isCorba : 1;
58 #endif
59 
60         bool passByReference (umlClass &cl) const;
61         void openOutfile (const std::string & filename, declaration & d);
62         void closeOutfile ();
63         void genDecl (declaration &d, bool forceOpen);
64     public:
65         GenerateCode (DiaGram    & diagram,
66                       const char * ext,
67                       uint8_t      version_,
68                       bool         handleIncludePackage_,
69                       bool         noLoopSupport_);
70 
71         DiaGram & getDia ();
72         void generate_code ();
73 
74         const char * getFileExt () const;
75         void         setFileExt (const char * ext);
76 
77         const char * getBodyFileExt () const;
78         void         setBodyFileExt (const char * ext);
79 
80         std::ofstream & getFile ();
81 
82         uint8_t getVersion () const;
83         void    setVersion (uint8_t version_);
84 
85         uint32_t getIndent () const;
86         void     setIndent (uint8_t spaces);
87         void     incIndentLevel ();
88         void     decIndentLevel ();
89 
90         const std::string & getLicense () const;
91         void                setLicense (const char * lic);
92 
93         const char * getOutdir () const;
94         const        std::string * getOutdirS () const;
95         void         setOutdir (const char * dir);
96 
97         bool getOverwrite () const;
98         void setOverwrite (bool over);
99 
100         bool getBuildTree () const;
101         void setBuildTree (bool build);
102 
103         bool getOpenBraceOnNewline () const;
104         void setOpenBraceOnNewline (bool newline);
105 
106         bool getOneClass () const;
107         void setOneClass (bool val);
108 
109         void setHandleIncludePackage (bool val);
110 
111 #ifdef ENABLE_CORBA
112         bool getCorba () const;
113 #endif
114 
115         void genClass (const umlClassNode & node);
116 
117         static const char * cppName (std::string name);
118 
119         const char * fqname (const umlClassNode & node,
120                              bool use_ref_type) const;
121         virtual const char * visibility (std::string desc,
122                                          const Visibility & vis) = 0;
123         std::string & spc () const;
124 
125         virtual std::string strPackage (const char * package) const = 0;
126         virtual std::string strPointer (const std::string & type) const = 0;
127 
128         void writeFile ();
129         static const char * comment (const std::string & comment_,
130                                      const std::string & startFirstLine,
131                                      const std::string & startOtherLines,
132                                      const char * endLastLine);
133         virtual void writeLicense () = 0;
134         virtual void writeStartHeader (std::string & name) = 0;
135         virtual void writeEndHeader () = 0;
136         // return true if at least one include has been written.
137         virtual bool writeInclude (const std::list <std::pair <
138                                      std::list <umlPackage *>,
139                                           const umlClassNode * > > & name) = 0;
140         virtual void writeInclude (const char * name) = 0;
141         virtual void writeBeforeInclude (umlClassNode * node);
142         virtual void writeAfterInclude (umlClassNode * node);
143         virtual void writeFunctionComment (const umlOperation & ope) = 0;
144         virtual void writeFunction (const umlClassNode & node,
145                                     const umlOperation & ope,
146                                     Visibility & curr_visibility) = 0;
147         virtual void writeFunctionGetSet (const umlClassNode & node,
148                                           const umlOperation & ope,
149                                           Visibility & curr_visibility) = 0;
150         virtual void writeComment (const std::string & text) = 0;
151         virtual void writeComment (const char * text) = 0;
152         virtual void writeClassComment (const std::string & com) = 0;
153         virtual void writeClassStart (const umlClassNode & node) = 0;
154         virtual void writeClassEnd () = 0;
155         virtual void writeAttributeComment (const umlAttribute & attr) = 0;
156         virtual void writeAttribute (const umlClassNode & node,
157                                      const umlAttribute & attr,
158                                      Visibility & curr_visibility) = 0;
159         virtual void writeNameSpaceStart (const umlClassNode * name) = 0;
160         virtual void writeNameSpaceEnd (const umlClassNode * node) = 0;
161         virtual void writeEnum (const umlClassNode & node) = 0;
162         virtual void writeStruct (const umlClassNode & node) = 0;
163         virtual void writeTypedef (const umlClassNode & node) = 0;
164         virtual void writeAssociation (const umlClassNode & node,
165                                        const umlassoc & asso,
166                                        Visibility & curr_visibility) = 0;
167         virtual void writeTemplates (
168            const std::list <std::pair <std::string, std::string> > & tmps) = 0;
169 
170         static const char * visibility1 (std::string desc,
171                                          const Visibility & vis);
172         void writeLicense1 (const char * start, const char * end);
173         void writeFunctionGetSet1 (const umlClassNode & node,
174                                    const umlOperation & ope,
175                                    Visibility & curr_visibility);
176         bool writeInclude1 (const std::list <std::pair <
177                                                       std::list <umlPackage *>,
178                             const umlClassNode *> > & name,
179                             const char * startIncludeSystem,
180                             const char * endIncludeSystem,
181                             const char * startIncludeFile,
182                             const char * endIncludeFile,
183                             bool forceExtExtern);
184 
185         virtual ~GenerateCode ();
186 };
187 
188 #endif
189 
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
191