Changeset View
Changeset View
Standalone View
Standalone View
src/kconfig_compiler/KConfigCodeGeneratorBase.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of KDE. | ||||
3 | | ||||
4 | Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org) | ||||
5 | Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> | ||||
6 | Copyright (c) 2003 Waldo Bastian <bastian@kde.org> | ||||
7 | Copyright (c) 2003 Zack Rusin <zack@kde.org> | ||||
8 | Copyright (c) 2006 Michaël Larouche <michael.larouche@kdemail.net> | ||||
9 | Copyright (c) 2008 Allen Winter <winter@kde.org> | ||||
10 | Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org) | ||||
11 | | ||||
12 | This library is free software; you can redistribute it and/or | ||||
13 | modify it under the terms of the GNU Library General Public | ||||
14 | License as published by the Free Software Foundation; either | ||||
15 | version 2 of the License, or (at your option) any later version. | ||||
16 | | ||||
17 | This library is distributed in the hope that it will be useful, | ||||
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
20 | Library General Public License for more details. | ||||
21 | | ||||
22 | You should have received a copy of the GNU Library General Public License | ||||
23 | along with this library; see the file COPYING.LIB. If not, write to | ||||
24 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
25 | Boston, MA 02110-1301, USA. | ||||
26 | */ | ||||
27 | | ||||
28 | #ifndef KCONFIGCODEGENERATORBASE_H | ||||
29 | #define KCONFIGCODEGENERATORBASE_H | ||||
30 | | ||||
31 | #include <QString> | ||||
32 | #include <QTextStream> | ||||
33 | #include <QFile> | ||||
34 | #include <QVector> | ||||
35 | | ||||
36 | #include "KConfigXTParameters.h" | ||||
37 | | ||||
38 | class CfgEntry; | ||||
39 | struct ParseResult; | ||||
40 | | ||||
41 | /* This class manages the base of writing a C - Based code */ | ||||
42 | class KConfigCodeGeneratorBase { | ||||
43 | public: | ||||
44 | enum ScopeFinalizer {None, Semicolon}; | ||||
45 | | ||||
46 | KConfigCodeGeneratorBase( | ||||
47 | const QString& inputFileName, // The kcfg file | ||||
48 | const QString& baseDir, // where we should store the generated file | ||||
49 | const QString& fileName, // the name of the generated file | ||||
50 | const KConfigXTParameters ¶meters, // parameters passed to the generator | ||||
51 | ParseResult &parseResult // The pre processed configuration entries | ||||
52 | ); | ||||
53 | virtual ~KConfigCodeGeneratorBase(); | ||||
54 | | ||||
55 | // iterates over the header list adding an #include directive. | ||||
56 | void addHeaders(const QStringList& header); | ||||
57 | | ||||
58 | // Create all the namespace indentation levels based on the parsed result and parameters */ | ||||
59 | void beginNamespaces(); | ||||
60 | | ||||
61 | // Closes all the namespaces adding lines with single '}' | ||||
62 | void endNamespaces(); | ||||
63 | | ||||
64 | // Add the correct amount of whitespace in the code. | ||||
65 | QString whitespace(); | ||||
66 | | ||||
67 | // start a block scope `{` and increase indentation level. | ||||
68 | void endScope(ScopeFinalizer finalizer = None); | ||||
69 | | ||||
70 | // end a block scope `}` and decrease indentation level. | ||||
71 | void startScope(); | ||||
72 | | ||||
73 | // start writing to the output file | ||||
74 | virtual void start(); | ||||
75 | | ||||
76 | // save the result on the disk | ||||
77 | void save(); | ||||
78 | | ||||
79 | // Code Implementations | ||||
80 | // Implements the `Get` methods for the CfgEntry | ||||
81 | // TODO: write to the stream directly without returning a QString. | ||||
82 | QString memberAccessorBody(const CfgEntry *e, bool globalEnums) const; | ||||
83 | | ||||
84 | // Implements the `Set` methods for the CfgEntry | ||||
85 | void memberMutatorBody(const CfgEntry *e); | ||||
86 | | ||||
87 | // This is the code that creates the logic for the Setter / Mutator. | ||||
88 | // It *just* creates the if test, no body. The reason is that just | ||||
89 | // the if test was more than 20 lines of code and hard to understand | ||||
90 | // what was happening in a bigger function. | ||||
91 | void createIfSetLogic(const CfgEntry *e, const QString &varExpression); | ||||
92 | | ||||
93 | protected: | ||||
94 | /* advance the number of spaces for the indentation level */ | ||||
95 | void indent(); | ||||
96 | | ||||
97 | /* reduce the number of spaces for the indentation level */ | ||||
98 | void unindent(); | ||||
99 | | ||||
100 | QString inputFile; // the base file name, input file is based on this. | ||||
101 | | ||||
102 | QString baseDir; // Where we are going to save the file | ||||
103 | QString fileName; // The file name | ||||
104 | | ||||
105 | const KConfigXTParameters &cfg; // The parameters passed via the kcfgc file | ||||
106 | ParseResult &parseResult; // the result of the parsed kcfg file | ||||
107 | QTextStream stream; // the stream that operates in the file to write data. | ||||
108 | QFile file; // The file handler. | ||||
109 | | ||||
110 | // Special access to `this->` and `const` thru the code. | ||||
111 | QString This; | ||||
112 | QString Const; | ||||
113 | | ||||
114 | private: | ||||
115 | int indentLevel = 0; | ||||
116 | }; | ||||
117 | | ||||
118 | #endif |