diff options
author | Martin Gräßlin <mgraesslin@kde.org> | 2016-11-16 14:59:28 +0100 |
---|---|---|
committer | Martin Gräßlin <mgraesslin@kde.org> | 2017-01-06 09:52:45 +0100 |
commit | e0db2422362260a2fea3a8e1de4c64011d2f183d (patch) | |
tree | 59b5ba0e74c5d24d27156e51e54eb1fc0eb02623 | |
parent | 7d53de59b6e65b0cbdf6eaad4f5dbcd341307003 (diff) | |
download | kconfig-e0db2422362260a2fea3a8e1de4c64011d2f183d.tar.gz kconfig-e0db2422362260a2fea3a8e1de4c64011d2f183d.tar.bz2 |
Generate an instance with KSharedConfig::Ptr for singleton and arg
Summary:
In case a kcfg with arg="true" was used and singleton the static
instance method only accepted a QString config name. This made it
impossible to combine a singleton config with an already existing and
open KSharedConfig::Ptr.
With this change an overloaded instance method is added which takes a
KSharedConfig::Ptr as argument. The private ctor, though, only takes a
KSharedConfig::Ptr and the instance method taking a QString argument
uses KSharedConfig::openConfig on the config file name.
The change is source-incompatible in the following situation:
* kcfgfile arg="true"
* Singleton = true
* Inherits is specified
In this situation the previous revision created an instance method with
a QString argument and passed that to the parent constructor. This is
not in accordance with the documentation. Any user of this behavior was
relying on a bug. With this change now the call to the parent
constructor carries a KSharedConfigPtr.
Test Plan:
kconfigcompiler tests still pass and a config with singleton
and arg="true" generates the code as I need it
Reviewers: #frameworks, dfaure, mdawson
Differential Revision: https://phabricator.kde.org/D3690
-rw-r--r-- | autotests/kconfig_compiler/CMakeLists.txt | 1 | ||||
-rw-r--r-- | autotests/kconfig_compiler/kconfigcompiler_test.cpp | 1 | ||||
-rw-r--r-- | autotests/kconfig_compiler/test8c.cpp.ref | 65 | ||||
-rw-r--r-- | autotests/kconfig_compiler/test8c.h.ref | 70 | ||||
-rw-r--r-- | autotests/kconfig_compiler/test8c.kcfg | 17 | ||||
-rw-r--r-- | autotests/kconfig_compiler/test8c.kcfgc | 4 | ||||
-rw-r--r-- | autotests/kconfig_compiler/test8main.cpp | 3 | ||||
-rw-r--r-- | src/kconfig_compiler/kconfig_compiler.cpp | 33 |
8 files changed, 183 insertions, 11 deletions
diff --git a/autotests/kconfig_compiler/CMakeLists.txt b/autotests/kconfig_compiler/CMakeLists.txt index 0971471b..590c7700 100644 --- a/autotests/kconfig_compiler/CMakeLists.txt +++ b/autotests/kconfig_compiler/CMakeLists.txt @@ -112,6 +112,7 @@ set(test8_SRCS test8main.cpp ) gen_kcfg_test_source(test8a test8_SRCS) gen_kcfg_test_source(test8b test8_SRCS) +gen_kcfg_test_source(test8c test8_SRCS) ecm_add_test(TEST_NAME test8 ${test8_SRCS}) target_link_libraries(test8 KF5::ConfigGui) diff --git a/autotests/kconfig_compiler/kconfigcompiler_test.cpp b/autotests/kconfig_compiler/kconfigcompiler_test.cpp index 88b462ef..01efd602 100644 --- a/autotests/kconfig_compiler/kconfigcompiler_test.cpp +++ b/autotests/kconfig_compiler/kconfigcompiler_test.cpp @@ -38,6 +38,7 @@ static CompilerTestSet testCases = { "test7.cpp", "test7.h", "test8a.cpp", "test8a.h", "test8b.cpp", "test8b.h", + "test8c.cpp", "test8c.h", "test9.h", "test9.cpp", "test10.h", "test10.cpp", "test11.h", "test11.cpp", diff --git a/autotests/kconfig_compiler/test8c.cpp.ref b/autotests/kconfig_compiler/test8c.cpp.ref new file mode 100644 index 00000000..90305c1f --- /dev/null +++ b/autotests/kconfig_compiler/test8c.cpp.ref @@ -0,0 +1,65 @@ +// This file is generated by kconfig_compiler_kf5 from test8c.kcfg. +// All changes you do to this file will be lost. + +#include "test8c.h" + +#include <qglobal.h> +#include <QtCore/QFile> + +#include <QDebug> + +class Test8cHelper +{ + public: + Test8cHelper() : q(0) {} + ~Test8cHelper() { delete q; } + Test8c *q; +}; +Q_GLOBAL_STATIC(Test8cHelper, s_globalTest8c) +Test8c *Test8c::self() +{ + if (!s_globalTest8c()->q) + qFatal("you need to call Test8c::instance before using"); + return s_globalTest8c()->q; +} + +void Test8c::instance(const QString& cfgfilename) +{ + if (s_globalTest8c()->q) { + qDebug() << "Test8c::instance called after the first use - ignoring"; + return; + } + new Test8c(KSharedConfig::openConfig(cfgfilename)); + s_globalTest8c()->q->read(); +} + +void Test8c::instance(KSharedConfig::Ptr config) +{ + if (s_globalTest8c()->q) { + qDebug() << "Test8c::instance called after the first use - ignoring"; + return; + } + new Test8c(config); + s_globalTest8c()->q->read(); +} + +Test8c::Test8c( KSharedConfig::Ptr config ) + : KConfigSkeleton( config ) +{ + Q_ASSERT(!s_globalTest8c()->q); + s_globalTest8c()->q = this; + setCurrentGroup( QStringLiteral( "Group" ) ); + + KConfigSkeleton::ItemFont *itemFont; + itemFont = new KConfigSkeleton::ItemFont( currentGroup(), QStringLiteral( "Font" ), mFont, QFont() ); + addItem( itemFont, QStringLiteral( "Font" ) ); + KConfigSkeleton::ItemFont *itemTitleFont; + itemTitleFont = new KConfigSkeleton::ItemFont( currentGroup(), QStringLiteral( "TitleFont" ), mTitleFont, QFont() ); + addItem( itemTitleFont, QStringLiteral( "TitleFont" ) ); +} + +Test8c::~Test8c() +{ + s_globalTest8c()->q = 0; +} + diff --git a/autotests/kconfig_compiler/test8c.h.ref b/autotests/kconfig_compiler/test8c.h.ref new file mode 100644 index 00000000..8ed9962c --- /dev/null +++ b/autotests/kconfig_compiler/test8c.h.ref @@ -0,0 +1,70 @@ +// This file is generated by kconfig_compiler_kf5 from test8c.kcfg. +// All changes you do to this file will be lost. +#ifndef TEST8C_H +#define TEST8C_H + +#include <kconfigskeleton.h> +#include <QCoreApplication> +#include <QDebug> + +class Test8c : public KConfigSkeleton +{ + public: + + static Test8c *self(); + static void instance(const QString& cfgfilename); + static void instance(KSharedConfig::Ptr config); + ~Test8c(); + + /** + Set Font + */ + static + void setFont( const QFont & v ) + { + if (!self()->isImmutable( QStringLiteral( "Font" ) )) + self()->mFont = v; + } + + /** + Get Font + */ + static + QFont font() + { + return self()->mFont; + } + + /** + Set TitleFont + */ + static + void setTitleFont( const QFont & v ) + { + if (!self()->isImmutable( QStringLiteral( "TitleFont" ) )) + self()->mTitleFont = v; + } + + /** + Get TitleFont + */ + static + QFont titleFont() + { + return self()->mTitleFont; + } + + protected: + Test8c(KSharedConfig::Ptr config); + friend class Test8cHelper; + + + // Group + QFont mFont; + QFont mTitleFont; + + private: +}; + +#endif + diff --git a/autotests/kconfig_compiler/test8c.kcfg b/autotests/kconfig_compiler/test8c.kcfg new file mode 100644 index 00000000..24038a69 --- /dev/null +++ b/autotests/kconfig_compiler/test8c.kcfg @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0 + http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" > + <kcfgfile arg="true"/> + + <group name="Group"> + <entry name="Font" type="Font"> + <default code="true">QFont()</default> + </entry> + + <entry name="TitleFont" type="Font"> + <default code="true">QFont()</default> + </entry> + </group> +</kcfg> diff --git a/autotests/kconfig_compiler/test8c.kcfgc b/autotests/kconfig_compiler/test8c.kcfgc new file mode 100644 index 00000000..e53e5a04 --- /dev/null +++ b/autotests/kconfig_compiler/test8c.kcfgc @@ -0,0 +1,4 @@ +File=test8c.kcfg +ClassName=Test8c +Mutators=true +Singleton=true diff --git a/autotests/kconfig_compiler/test8main.cpp b/autotests/kconfig_compiler/test8main.cpp index b8fcc492..9843df72 100644 --- a/autotests/kconfig_compiler/test8main.cpp +++ b/autotests/kconfig_compiler/test8main.cpp @@ -21,6 +21,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include "test8a.h" #include "test8b.h" +#include "test8c.h" #include <QGuiApplication> int main(int argc, char **argv) @@ -30,6 +31,8 @@ int main(int argc, char **argv) Test8a *config1 = new Test8a(KSharedConfig::openConfig(QString())); Test8a *config2 = new Test8a(); Test8b::self(); + Test8c::instance(KSharedConfig::openConfig(QString())); + Test8c::self(); delete config1; delete config2; return 0; diff --git a/src/kconfig_compiler/kconfig_compiler.cpp b/src/kconfig_compiler/kconfig_compiler.cpp index 5c3f478f..c836252f 100644 --- a/src/kconfig_compiler/kconfig_compiler.cpp +++ b/src/kconfig_compiler/kconfig_compiler.cpp @@ -1893,6 +1893,7 @@ int main(int argc, char **argv) h << " static " << cfg.className << " *self();" << endl; if (cfgFileNameArg) { h << " static void instance(const QString& cfgfilename);" << endl; + h << " static void instance(KSharedConfig::Ptr config);" << endl; } } @@ -2115,7 +2116,7 @@ int main(int argc, char **argv) if (cfg.singleton) { h << " " << cfg.className << "("; if (cfgFileNameArg) { - h << "const QString& arg"; + h << "KSharedConfig::Ptr config"; } h << ");" << endl; h << " friend class " << cfg.className << "Helper;" << endl << endl; @@ -2320,15 +2321,25 @@ int main(int argc, char **argv) cpp << "}" << endl << endl; if (cfgFileNameArg) { - cpp << "void " << cfg.className << "::instance(const QString& cfgfilename)" << endl; - cpp << "{" << endl; - cpp << " if (s_global" << cfg.className << "()->q) {" << endl; - cpp << " qDebug() << \"" << cfg.className << "::instance called after the first use - ignoring\";" << endl; - cpp << " return;" << endl; - cpp << " }" << endl; - cpp << " new " << cfg.className << "(cfgfilename);" << endl; - cpp << " s_global" << cfg.className << "()->q->read();" << endl; - cpp << "}" << endl << endl; + auto instance = [&cfg, &cpp] (const QString &type, const QString arg, bool wrap) { + cpp << "void " << cfg.className << "::instance(" << type << " " << arg << ")" << endl; + cpp << "{" << endl; + cpp << " if (s_global" << cfg.className << "()->q) {" << endl; + cpp << " qDebug() << \"" << cfg.className << "::instance called after the first use - ignoring\";" << endl; + cpp << " return;" << endl; + cpp << " }" << endl; + cpp << " new " << cfg.className << "("; + if (wrap) { + cpp << "KSharedConfig::openConfig(" << arg << ")"; + } else { + cpp << arg; + } + cpp << ");" << endl; + cpp << " s_global" << cfg.className << "()->q->read();" << endl; + cpp << "}" << endl << endl; + }; + instance(QStringLiteral("const QString&"), QStringLiteral("cfgfilename"), true); + instance(QStringLiteral("KSharedConfig::Ptr"), QStringLiteral("config"), false); } } @@ -2339,7 +2350,7 @@ int main(int argc, char **argv) // Constructor cpp << cfg.className << "::" << cfg.className << "( "; if (cfgFileNameArg) { - if (!cfg.singleton && ! cfg.forceStringFilename) { + if (! cfg.forceStringFilename) { cpp << " KSharedConfig::Ptr config"; } else { cpp << " const QString& config"; |