KConfig

KConfigCodeGeneratorBase.h
1 /*
2  This file is part of KDE.
3 
4  SPDX-FileCopyrightText: 2003 Cornelius Schumacher <[email protected]>
5  SPDX-FileCopyrightText: 2003 Waldo Bastian <[email protected]>
6  SPDX-FileCopyrightText: 2003 Zack Rusin <[email protected]>
7  SPDX-FileCopyrightText: 2006 MichaĆ«l Larouche <[email protected]>
8  SPDX-FileCopyrightText: 2008 Allen Winter <[email protected]>
9  SPDX-FileCopyrightText: 2020 Tomaz Cananbrava <[email protected]>
10 
11  SPDX-License-Identifier: LGPL-2.0-or-later
12 */
13 
14 #ifndef KCONFIGCODEGENERATORBASE_H
15 #define KCONFIGCODEGENERATORBASE_H
16 
17 #include <QString>
18 #include <QTextStream>
19 #include <QFile>
20 #include <QVector>
21 
22 #include "KConfigParameters.h"
23 #include "KConfigCommonStructs.h"
24 
25 class CfgEntry;
26 struct ParseResult;
27 
28 /* This class manages the base of writing a C - Based code */
29 class KConfigCodeGeneratorBase {
30 public:
31  enum ScopeFinalizer {None, Semicolon};
32 
33  KConfigCodeGeneratorBase(
34  const QString &inputFileName, // The kcfg file
35  const QString &baseDir, // where we should store the generated file
36  const QString &fileName, // the name of the generated file
37  const KConfigParameters &parameters, // parameters passed to the generator
38  ParseResult &parseResult // The pre processed configuration entries
39  );
40  virtual ~KConfigCodeGeneratorBase();
41 
42  // iterates over the header list adding an #include directive.
43  void addHeaders(const QStringList &header);
44 
45  // Create all the namespace indentation levels based on the parsed result and parameters */
46  void beginNamespaces();
47 
48  // Closes all the namespaces adding lines with single '}'
49  void endNamespaces();
50 
51  // Add the correct amount of whitespace in the code.
52  QString whitespace() const;
53 
54  // start a block scope `{` and increase indentation level.
55  void endScope(ScopeFinalizer finalizer = None);
56 
57  // end a block scope `}` and decrease indentation level.
58  void startScope();
59 
60  // start writing to the output file
61  virtual void start();
62 
63  // save the result on the disk
64  void save();
65 
66  // Code Implementations
67  // Implements the `Get` methods for the CfgEntry
68  // TODO: write to the stream directly without returning a QString.
69  QString memberAccessorBody(const CfgEntry *e, bool globalEnums) const;
70 
71  // Implements the is<Param>Immutable for the CfgEntry
72  void memberImmutableBody(const CfgEntry *e, bool globalEnums);
73 
74  // Implements the `Set` methods for the CfgEntry
75  void memberMutatorBody(const CfgEntry *e);
76 
77  // This is the code that creates the logic for the Setter / Mutator.
78  // It *just* creates the if test, no body. The reason is that just
79  // the if test was more than 20 lines of code and hard to understand
80  // what was happening in a bigger function.
81  void createIfSetLogic(const CfgEntry *e, const QString &varExpression);
82 
83 protected:
84  /* advance the number of spaces for the indentation level */
85  void indent();
86 
87  /* reduce the number of spaces for the indentation level */
88  void unindent();
89 
90  QString inputFile() const { return m_inputFile; }
91  QString fileName() const { return m_fileName; }
92  QString baseDir() const { return m_baseDir; }
93  QString This() const { return m_this; }
94  QString Const() const { return m_const; }
95  KConfigParameters cfg() const { return m_cfg; }
96 
97  // Can't be const.
98  QTextStream& stream() { return m_stream; }
99 
100  // HACK: This needs to be accesible because the HeaderGenerator actually modifies
101  // it while running. Considering that this is a the result of the xml Parse, and not
102  // the result of generating code, I consider this to be quite wrong - but moving the
103  // changes away from the header generator only created more problems and I have to
104  // investigate more.
105  ParseResult &parseResult; // the result of the parsed kcfg file
106 
107 private:
108  QString m_inputFile; // the base file name, input file is based on this.
109 
110  QString m_baseDir; // Where we are going to save the file
111  QString m_fileName; // The file name
112 
113  KConfigParameters m_cfg; // The parameters passed via the kcfgc file
114  QTextStream m_stream; // the stream that operates in the file to write data.
115  QFile m_file; // The file handler.
116 
117  // Special access to `this->` and `const` thru the code.
118  QString m_this;
119  QString m_const;
120  int m_indentLevel = 0;
121 };
122 
123 #endif
KGuiItem save()
Configuration Compiler Configuration.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 11 2020 22:47:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.