Commit 7e45316b authored by Martin Flöser's avatar Martin Flöser

[tools/generator] Generate enum FooInterfaceVersion on server side

With this change the generator is able to detect whether an interface
follows the unstable semantics. In that case the header file on server
side looks different. An enum needs to be generated containing the
interface version. Each of the generated classes has a new method
interfaceVersion returning that enum. The ctor of the class is protected
instead of private.

So far only the header side is adjusted. The implementation currently
generates not matching code.
parent a1bdd1c5
......@@ -33,6 +33,8 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#include <QDebug>
#include <functional>
namespace KWayland
{
namespace Tools
......@@ -423,6 +425,27 @@ void Generator::startGenerateServerHeaderFile()
generateHeaderIncludes();
generateStartNamespace();
generateNamespaceForwardDeclarations();
if (std::any_of(m_interfaces.constBegin(), m_interfaces.constEnd(), [] (const Interface &i) { return i.isUnstableInterface(); })) {
// generate the unstable semantic version
auto it = std::find_if(m_interfaces.constBegin(), m_interfaces.constEnd(), [] (const Interface &i) { return i.isGlobal(); });
if (it != m_interfaces.constEnd()) {
const QString templateString = QStringLiteral(
"/**\n"
" * Enum describing the interface versions the %1 can support.\n"
" *\n"
" * @since 5.XX\n"
" **/\n"
"enum class %1Version {\n"
" /**\n"
" * %2\n"
" **/\n"
" UnstableV%3\n"
"};\n\n");
*m_stream.localData() << templateString.arg((*it).kwaylandServerName())
.arg((*it).name())
.arg((*it).name().mid((*it).name().lastIndexOf(QStringLiteral("_v")) + 2));
}
}
for (auto it = m_interfaces.constBegin(); it != m_interfaces.constEnd(); ++it) {
generateClass(*it);
}
......@@ -656,6 +679,10 @@ void Generator::generateClientResourceClass(const Interface &interface)
void Generator::generateServerGlobalClass(const Interface &interface)
{
if (interface.isUnstableInterface()) {
generateServerGlobalClassUnstable(interface);
return;
}
const QString templateString = QStringLiteral(
"class KWAYLANDSERVER_EXPORT %1 : public Global\n"
"{\n"
......@@ -672,8 +699,37 @@ void Generator::generateServerGlobalClass(const Interface &interface)
*m_stream.localData() << templateString.arg(interface.kwaylandServerName());
}
void Generator::generateServerGlobalClassUnstable(const Interface &interface)
{
const QString templateString = QStringLiteral(
"class KWAYLANDSERVER_EXPORT %1 : public Global\n"
"{\n"
" Q_OBJECT\n"
"public:\n"
" virtual ~%1();\n"
"\n"
" /**\n"
" * @returns The interface version used by this %1\n"
" **/\n"
" %1Version interfaceVersion() const;\n"
"\n"
"protected:\n"
" class Private;\n"
" explicit %1(Private *d, QObject *parent = nullptr);\n"
"\n"
"private:\n"
" Private *d_func() const;\n"
"};\n"
"\n");
*m_stream.localData() << templateString.arg(interface.kwaylandServerName());
}
void Generator::generateServerResourceClass(const Interface &interface)
{
if (interface.factory()->isUnstableInterface()) {
generateServerResourceClassUnstable(interface);
return;
}
const QString templateString = QStringLiteral(
"class KWAYLANDSERVER_EXPORT %1 : public Resource\n"
"{\n"
......@@ -692,6 +748,32 @@ void Generator::generateServerResourceClass(const Interface &interface)
*m_stream.localData() << templateString.arg(interface.kwaylandServerName()).arg(interface.factory()->kwaylandServerName());
}
void Generator::generateServerResourceClassUnstable(const Interface &interface)
{
const QString templateString = QStringLiteral(
"class KWAYLANDSERVER_EXPORT %1 : public Resource\n"
"{\n"
" Q_OBJECT\n"
"public:\n"
"\n"
" virtual ~%1();\n"
"\n"
" /**\n"
" * @returns The interface version used by this %1\n"
" **/\n"
" %2Version interfaceVersion() const;\n"
"\n"
"protected:\n"
" class Private;\n"
" explicit %1(Private *p, QObject *parent = nullptr);\n"
"\n"
"private:\n"
" Private *d_func() const;\n"
"};\n"
"\n");
*m_stream.localData() << templateString.arg(interface.kwaylandServerName()).arg(interface.factory()->kwaylandServerName());
}
void Generator::generatePrivateClass(const Interface &interface)
{
switch (m_project.localData()) {
......
......@@ -182,6 +182,10 @@ public:
return m_factory;
}
bool isUnstableInterface() const {
return m_name.startsWith(QLatin1String("zwp"));
}
private:
QString m_name;
QString m_clientName;
......@@ -232,7 +236,9 @@ private:
void generateClientGlobalClass(const Interface &interface);
void generateClientResourceClass(const Interface &interface);
void generateServerGlobalClass(const Interface &interface);
void generateServerGlobalClassUnstable(const Interface &interface);
void generateServerResourceClass(const Interface &interface);
void generateServerResourceClassUnstable(const Interface &interface);
void generateClientClassQObjectDerived(const Interface &interface);
void generateClientGlobalClassDoxy(const Interface &interface);
void generateClientGlobalClassCtor(const Interface &interface);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment