File indexing completed on 2024-05-12 03:54:29

0001 /*
0002     This file is part of KDE.
0003 
0004     SPDX-FileCopyrightText: 2003 Cornelius Schumacher <schumacher@kde.org>
0005     SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
0006     SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org>
0007     SPDX-FileCopyrightText: 2006 Michaƫl Larouche <michael.larouche@kdemail.net>
0008     SPDX-FileCopyrightText: 2008 Allen Winter <winter@kde.org>
0009     SPDX-FileCopyrightText: 2020 Tomaz Cananbrava <tcanabrava@kde.org>
0010 
0011     SPDX-License-Identifier: LGPL-2.0-or-later
0012 */
0013 
0014 #ifndef KCONFIGCODEGENERATORBASE_H
0015 #define KCONFIGCODEGENERATORBASE_H
0016 
0017 #include <QFile>
0018 #include <QList>
0019 #include <QString>
0020 #include <QTextStream>
0021 
0022 #include "KConfigCommonStructs.h"
0023 #include "KConfigParameters.h"
0024 
0025 class CfgEntry;
0026 struct ParseResult;
0027 
0028 /* This class manages the base of writing a C - Based code */
0029 class KConfigCodeGeneratorBase
0030 {
0031 public:
0032     enum ScopeFinalizer {
0033         None,
0034         Semicolon,
0035     };
0036 
0037     KConfigCodeGeneratorBase(const QString &inputFileName, // The kcfg file
0038                              const QString &baseDir, // where we should store the generated file
0039                              const QString &fileName, // the name of the generated file
0040                              const KConfigParameters &parameters, // parameters passed to the generator
0041                              ParseResult &parseResult // The pre processed configuration entries
0042     );
0043     virtual ~KConfigCodeGeneratorBase();
0044 
0045     // iterates over the header list adding an #include directive.
0046     void addHeaders(const QStringList &header);
0047 
0048     // Create all the namespace indentation levels based on the parsed result and parameters */
0049     void beginNamespaces();
0050 
0051     // Closes all the namespaces adding lines with single '}'
0052     void endNamespaces();
0053 
0054     // Add the correct amount of whitespace in the code.
0055     QString whitespace() const;
0056 
0057     // start a block scope `{` and increase indentation level.
0058     void endScope(ScopeFinalizer finalizer = None);
0059 
0060     // end a block scope `}` and decrease indentation level.
0061     void startScope();
0062 
0063     // start writing to the output file
0064     virtual void start();
0065 
0066     // save the result on the disk
0067     void save();
0068 
0069     // Code Implementations
0070     // Implements the `Get` methods for the CfgEntry
0071     // TODO: write to the stream directly without returning a QString.
0072     QString memberAccessorBody(const CfgEntry *e, bool globalEnums) const;
0073 
0074     // Implements the is<Param>Immutable for the CfgEntry
0075     void memberImmutableBody(const CfgEntry *e, bool globalEnums);
0076 
0077     // Implements the `Set` methods for the CfgEntry
0078     void memberMutatorBody(const CfgEntry *e);
0079 
0080     // This is the code that creates the logic for the Setter / Mutator.
0081     // It *just* creates the if test, no body. The reason is that just
0082     // the if test was more than 20 lines of code and hard to understand
0083     // what was happening in a bigger function.
0084     void createIfSetLogic(const CfgEntry *e, const QString &varExpression);
0085 
0086 protected:
0087     /* advance the number of spaces for the indentation level */
0088     void indent();
0089 
0090     /* reduce the number of spaces for the indentation level */
0091     void unindent();
0092 
0093     QString inputFile() const
0094     {
0095         return m_inputFile;
0096     }
0097     QString fileName() const
0098     {
0099         return m_fileName;
0100     }
0101     QString baseDir() const
0102     {
0103         return m_baseDir;
0104     }
0105     QString This() const
0106     {
0107         return m_this;
0108     }
0109     QString Const() const
0110     {
0111         return m_const;
0112     }
0113     KConfigParameters cfg() const
0114     {
0115         return m_cfg;
0116     }
0117 
0118     // Can't be const.
0119     QTextStream &stream()
0120     {
0121         return m_stream;
0122     }
0123 
0124     // HACK: This needs to be accessible because the HeaderGenerator actually modifies
0125     // it while running. Considering that this is a the result of the xml Parse, and not
0126     // the result of generating code, I consider this to be quite wrong - but moving the
0127     // changes away from the header generator only created more problems and I have to
0128     // investigate more.
0129     ParseResult &parseResult; // the result of the parsed kcfg file
0130 
0131 private:
0132     QString m_inputFile; // the base file name, input file is based on this.
0133 
0134     QString m_baseDir; // Where we are going to save the file
0135     QString m_fileName; // The file name
0136 
0137     KConfigParameters m_cfg; // The parameters passed via the kcfgc file
0138     QTextStream m_stream; // the stream that operates in the file to write data.
0139     QFile m_file; // The file handler.
0140 
0141     // Special access to `this->` and `const` thru the code.
0142     QString m_this;
0143     QString m_const;
0144     int m_indentLevel = 0;
0145 };
0146 
0147 #endif