aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Krus <mike.krus@kdab.com>2017-05-11 10:30:13 +0100
committerMike Krus <mike.krus@kdab.com>2017-05-23 09:01:27 +0000
commit0e8d471c98f971ac53b2c6fc092e6cc130af6610 (patch)
tree0c70764520f16d769f2f7da68337d4a31a4d7ab5
parent3c170db282b36b3d2f1f6cf2f11a2cfca4c3ac70 (diff)
Add support for generating structs
Also added methods on the generate instances on the module. This was split into an abstract base class to separate out the enums and avoid circular includes. Change-Id: I6f065a1a4032d6570b2b4b42806888e044c18134 Reviewed-by: Dominik Holland <dominik.holland@pelagicore.com>
-rwxr-xr-xsrc/ivicore/qface/generate.py14
-rw-r--r--src/ivicore/qface/templates_frontend.yaml8
-rw-r--r--src/ivicore/qface/templates_frontend/abstractmodule.cpp.tpl58
-rw-r--r--src/ivicore/qface/templates_frontend/abstractmodule.h.tpl77
-rw-r--r--src/ivicore/qface/templates_frontend/interface.h.tpl2
-rw-r--r--src/ivicore/qface/templates_frontend/module.cpp.tpl28
-rw-r--r--src/ivicore/qface/templates_frontend/module.h.tpl23
-rw-r--r--src/ivicore/qface/templates_frontend/module.pri.tpl8
-rw-r--r--src/ivicore/qface/templates_frontend/struct.cpp.tpl96
-rw-r--r--src/ivicore/qface/templates_frontend/struct.h.tpl82
-rw-r--r--tests/manual/qface/org.example.echo.qface9
11 files changed, 382 insertions, 23 deletions
diff --git a/src/ivicore/qface/generate.py b/src/ivicore/qface/generate.py
index 94d868e..0b12972 100755
--- a/src/ivicore/qface/generate.py
+++ b/src/ivicore/qface/generate.py
@@ -226,12 +226,14 @@ def generate(tplconfig, moduleConfig, src, dst):
for rule in gen_config['generate_rules']['interface_rules']:
preserve = rule['preserve'] if 'preserve' in rule else False
generator.write(rule['dest_file'], rule['template_file'], ctx, preserve)
- for struct in module.structs:
- log.debug('generate code for struct %s', struct)
- struct.add_tag('config')
- for rule in gen_config['generate_rules']['struct_rules']:
- preserve = rule['preserve'] if 'preserve' in rule else False
- generator.write(rule['dest_file'], rule['template_file'], ctx, preserve)
+ if 'struct_rules' in gen_config['generate_rules'] and isinstance(gen_config['generate_rules']['struct_rules'], list):
+ for struct in module.structs:
+ log.debug('generate code for struct %s', struct)
+ struct.add_tag('config')
+ ctx.update({'struct': struct})
+ for rule in gen_config['generate_rules']['struct_rules']:
+ preserve = rule['preserve'] if 'preserve' in rule else False
+ generator.write(rule['dest_file'], rule['template_file'], ctx, preserve)
def run(formats, moduleConfig, src, dst):
diff --git a/src/ivicore/qface/templates_frontend.yaml b/src/ivicore/qface/templates_frontend.yaml
index b0911ed..df447b0 100644
--- a/src/ivicore/qface/templates_frontend.yaml
+++ b/src/ivicore/qface/templates_frontend.yaml
@@ -6,6 +6,10 @@ generate_rules:
template_file: "module.h.tpl"
- dest_file: "{{module.module_name|lower}}module.cpp"
template_file: "module.cpp.tpl"
+ - dest_file: "abstract{{module.module_name|lower}}module.h"
+ template_file: "abstractmodule.h.tpl"
+ - dest_file: "abstract{{module.module_name|lower}}module.cpp"
+ template_file: "abstractmodule.cpp.tpl"
- dest_file: "{{module|lower|replace('.', '-')}}.pri"
template_file: "module.pri.tpl"
interface_rules:
@@ -20,3 +24,7 @@ generate_rules:
- dest_file: '{{interface|lower}}.cpp'
template_file: 'interface.cpp.tpl'
struct_rules:
+ - dest_file: '{{struct|lower}}.h'
+ template_file: 'struct.h.tpl'
+ - dest_file: '{{struct|lower}}.cpp'
+ template_file: 'struct.cpp.tpl'
diff --git a/src/ivicore/qface/templates_frontend/abstractmodule.cpp.tpl b/src/ivicore/qface/templates_frontend/abstractmodule.cpp.tpl
new file mode 100644
index 0000000..a35046d
--- /dev/null
+++ b/src/ivicore/qface/templates_frontend/abstractmodule.cpp.tpl
@@ -0,0 +1,58 @@
+{#
+# Copyright (C) 2017 Pelagicore AG.
+# Contact: https://www.qt.io/licensing/
+#
+# This file is part of the QtIvi module of the Qt Toolkit.
+#
+# $QT_BEGIN_LICENSE:LGPL-QTAS$
+# Commercial License Usage
+# Licensees holding valid commercial Qt Automotive Suite licenses may use
+# this file in accordance with the commercial license agreement provided
+# with the Software or, alternatively, in accordance with the terms
+# contained in a written agreement between you and The Qt Company. For
+# licensing terms and conditions see https://www.qt.io/terms-conditions.
+# For further information use the contact form at https://www.qt.io/contact-us.
+#
+# GNU Lesser General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU Lesser
+# General Public License version 3 as published by the Free Software
+# Foundation and appearing in the file LICENSE.LGPL3 included in the
+# packaging of this file. Please review the following information to
+# ensure the GNU Lesser General Public License version 3 requirements
+# will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+#
+# GNU General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU
+# General Public License version 2.0 or (at your option) the GNU General
+# Public license version 3 or any later version approved by the KDE Free
+# Qt Foundation. The licenses are as published by the Free Software
+# Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+# included in the packaging of this file. Please review the following
+# information to ensure the GNU General Public License requirements will
+# be met: https://www.gnu.org/licenses/gpl-2.0.html and
+# https://www.gnu.org/licenses/gpl-3.0.html.
+#
+# $QT_END_LICENSE$
+#
+# SPDX-License-Identifier: LGPL-3.0
+#}
+{% set class = 'Abstract{0}Module'.format(module.module_name) %}
+{% include 'generated_comment.cpp.tpl' %}
+{% import 'utils.tpl' as utils %}
+
+#include "{{class|lower}}.h"
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \class {{class}}
+ \inmodule {{module}}
+
+{{ utils.format_comments(module.comment) }}
+*/
+{{class}}::{{class}}(QObject *parent)
+ : QObject(parent)
+{
+}
+
+QT_END_NAMESPACE
diff --git a/src/ivicore/qface/templates_frontend/abstractmodule.h.tpl b/src/ivicore/qface/templates_frontend/abstractmodule.h.tpl
new file mode 100644
index 0000000..c4265d2
--- /dev/null
+++ b/src/ivicore/qface/templates_frontend/abstractmodule.h.tpl
@@ -0,0 +1,77 @@
+{#
+# Copyright (C) 2017 Pelagicore AG.
+# Contact: https://www.qt.io/licensing/
+#
+# This file is part of the QtIvi module of the Qt Toolkit.
+#
+# $QT_BEGIN_LICENSE:LGPL-QTAS$
+# Commercial License Usage
+# Licensees holding valid commercial Qt Automotive Suite licenses may use
+# this file in accordance with the commercial license agreement provided
+# with the Software or, alternatively, in accordance with the terms
+# contained in a written agreement between you and The Qt Company. For
+# licensing terms and conditions see https://www.qt.io/terms-conditions.
+# For further information use the contact form at https://www.qt.io/contact-us.
+#
+# GNU Lesser General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU Lesser
+# General Public License version 3 as published by the Free Software
+# Foundation and appearing in the file LICENSE.LGPL3 included in the
+# packaging of this file. Please review the following information to
+# ensure the GNU Lesser General Public License version 3 requirements
+# will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+#
+# GNU General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU
+# General Public License version 2.0 or (at your option) the GNU General
+# Public license version 3 or any later version approved by the KDE Free
+# Qt Foundation. The licenses are as published by the Free Software
+# Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+# included in the packaging of this file. Please review the following
+# information to ensure the GNU General Public License requirements will
+# be met: https://www.gnu.org/licenses/gpl-2.0.html and
+# https://www.gnu.org/licenses/gpl-3.0.html.
+#
+# $QT_END_LICENSE$
+#
+# SPDX-License-Identifier: LGPL-3.0
+#}
+{% set exportsymbol = 'Q_QT{0}_EXPORT'.format(module.module_name|upper) %}
+{% set class = 'Abstract{0}Module'.format(module.module_name) %}
+{% set oncedefine = '{0}_H_'.format(class|upper) %}
+{% include 'generated_comment.cpp.tpl' %}
+{% import 'utils.tpl' as utils %}
+
+#ifndef {{oncedefine}}
+#define {{oncedefine}}
+
+#include "{{module.module_name|lower}}global.h"
+#include <QObject>
+
+QT_BEGIN_NAMESPACE
+
+class {{exportsymbol}} {{class}} : public QObject {
+ Q_OBJECT
+public:
+
+{% for enum in module.enums %}
+{% if enum.comment %}
+ /*!
+ {{ utils.format_comments(enum.comment) }}
+ */
+{% endif %}
+ enum {{enum}} {
+ {% for member in enum.members %}
+ {{member.name}} = {{member.value}}, {{member.comment}}
+ {% endfor %}
+ };
+ Q_ENUM({{enum}})
+{% endfor %}
+
+protected:
+ {{class}}(QObject *parent=nullptr);
+};
+
+QT_END_NAMESPACE
+
+#endif // {{oncedefine}}
diff --git a/src/ivicore/qface/templates_frontend/interface.h.tpl b/src/ivicore/qface/templates_frontend/interface.h.tpl
index ac1a800..f1f066a 100644
--- a/src/ivicore/qface/templates_frontend/interface.h.tpl
+++ b/src/ivicore/qface/templates_frontend/interface.h.tpl
@@ -44,7 +44,7 @@
#ifndef {{oncedefine}}
#define {{oncedefine}}
-#include "{{module.module_name|lower}}module.h"
+#include "abstract{{module.module_name|lower}}module.h"
{% if interface.tags.config.zoned %}
#include <QtIviCore/QIviAbstractZonedFeature>
diff --git a/src/ivicore/qface/templates_frontend/module.cpp.tpl b/src/ivicore/qface/templates_frontend/module.cpp.tpl
index d04b52e..1ea6e9f 100644
--- a/src/ivicore/qface/templates_frontend/module.cpp.tpl
+++ b/src/ivicore/qface/templates_frontend/module.cpp.tpl
@@ -63,7 +63,7 @@ QObject* {{class|lower}}_singletontype_provider(QQmlEngine*, QJSEngine*)
{{ utils.format_comments(module.comment) }}
*/
{{class}}::{{class}}(QObject *parent)
- : QObject(parent)
+ : Abstract{{class}}(parent)
{
}
@@ -73,6 +73,9 @@ void {{class}}::registerTypes()
{% for enum in module.enums %}
qRegisterMetaType<{{class}}::{{enum}}>();
{% endfor %}
+{% for struct in module.structs %}
+ qRegisterMetaType<{{struct}}>();
+{% endfor %}
}
/*! \internal */
@@ -86,4 +89,27 @@ void {{class}}::registerQmlTypes(const QString& uri, int majorVersion, int minor
{% endfor %}
}
+{% for struct in module.structs %}
+/*!
+ \brief Generate default instance of {{struct}}.
+
+ \sa {{struct}}
+*/
+{{struct}} {{class}}::{{struct|lowerfirst}}() const
+{
+ return {{struct}}();
+}
+
+/*!
+ \brief Generate instance of {{struct}} using attributes.
+
+ \sa {{struct}}
+*/
+{{struct}} {{class}}::{{struct|lowerfirst}}({% for field in struct.fields %}{% if not loop.first %}, {% endif %}{{field|return_type}} {{field}}{% endfor %}) const
+{
+ return {{struct}}({% for field in struct.fields %}{% if not loop.first %}, {% endif %}{{field}}{% endfor %});
+}
+
+{% endfor %}
+
QT_END_NAMESPACE
diff --git a/src/ivicore/qface/templates_frontend/module.h.tpl b/src/ivicore/qface/templates_frontend/module.h.tpl
index bbc0341..50b4e63 100644
--- a/src/ivicore/qface/templates_frontend/module.h.tpl
+++ b/src/ivicore/qface/templates_frontend/module.h.tpl
@@ -45,28 +45,21 @@
#ifndef {{oncedefine}}
#define {{oncedefine}}
-#include "{{module.module_name|lower}}global.h"
-#include <QObject>
+#include "abstract{{module.module_name|lower}}module.h"
+{% for struct in module.structs %}
+#include "{{struct|lower}}.h"
+{% endfor %}
QT_BEGIN_NAMESPACE
-class {{exportsymbol}} {{class}} : public QObject {
+class {{exportsymbol}} {{class}} : public Abstract{{class}} {
Q_OBJECT
public:
{{class}}(QObject *parent=0);
-{% for enum in module.enums %}
-{% if enum.comment %}
- /*!
- {{ utils.format_comments(enum.comment) }}
- */
-{% endif %}
- enum {{enum}} {
- {% for member in enum.members %}
- {{member.name}} = {{member.value}}, {{member.comment}}
- {% endfor %}
- };
- Q_ENUM({{enum}})
+{% for struct in module.structs %}
+ Q_INVOKABLE {{struct}} {{struct|lowerfirst}}() const;
+ Q_INVOKABLE {{struct}} {{struct|lowerfirst}}({% for field in struct.fields %}{% if not loop.first %}, {% endif %}{{field|return_type}} {{field}}{% endfor %}) const;
{% endfor %}
static void registerTypes();
diff --git a/src/ivicore/qface/templates_frontend/module.pri.tpl b/src/ivicore/qface/templates_frontend/module.pri.tpl
index 0ffc678..63f5ed1 100644
--- a/src/ivicore/qface/templates_frontend/module.pri.tpl
+++ b/src/ivicore/qface/templates_frontend/module.pri.tpl
@@ -47,6 +47,10 @@ HEADERS += \
$$PWD/{{interface|lower}}_p.h \
$$PWD/{{interface|lower}}backendinterface.h \
{% endfor %}
+{% for struct in module.structs %}
+ $$PWD/{{struct|lower}}.h \
+{% endfor %}
+ $$PWD/abstract{{module.module_name|lower}}module.h \
$$PWD/{{module.module_name|lower}}module.h \
$$PWD/{{module.module_name|lower}}global.h
@@ -55,4 +59,8 @@ SOURCES += \
$$PWD/{{interface|lower}}.cpp \
$$PWD/{{interface|lower}}backendinterface.cpp \
{% endfor %}
+{% for struct in module.structs %}
+ $$PWD/{{struct|lower}}.cpp \
+{% endfor %}
+ $$PWD/abstract{{module.module_name|lower}}module.cpp \
$$PWD/{{module.module_name|lower}}module.cpp
diff --git a/src/ivicore/qface/templates_frontend/struct.cpp.tpl b/src/ivicore/qface/templates_frontend/struct.cpp.tpl
new file mode 100644
index 0000000..d1134a4
--- /dev/null
+++ b/src/ivicore/qface/templates_frontend/struct.cpp.tpl
@@ -0,0 +1,96 @@
+{#
+# Copyright (C) 2017 Pelagicore AG.
+# Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB)
+# Contact: https://www.qt.io/licensing/
+#
+# This file is part of the QtIvi module of the Qt Toolkit.
+#
+# $QT_BEGIN_LICENSE:LGPL-QTAS$
+# Commercial License Usage
+# Licensees holding valid commercial Qt Automotive Suite licenses may use
+# this file in accordance with the commercial license agreement provided
+# with the Software or, alternatively, in accordance with the terms
+# contained in a written agreement between you and The Qt Company. For
+# licensing terms and conditions see https://www.qt.io/terms-conditions.
+# For further information use the contact form at https://www.qt.io/contact-us.
+#
+# GNU Lesser General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU Lesser
+# General Public License version 3 as published by the Free Software
+# Foundation and appearing in the file LICENSE.LGPL3 included in the
+# packaging of this file. Please review the following information to
+# ensure the GNU Lesser General Public License version 3 requirements
+# will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+#
+# GNU General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU
+# General Public License version 2.0 or (at your option) the GNU General
+# Public license version 3 or any later version approved by the KDE Free
+# Qt Foundation. The licenses are as published by the Free Software
+# Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+# included in the packaging of this file. Please review the following
+# information to ensure the GNU General Public License requirements will
+# be met: https://www.gnu.org/licenses/gpl-2.0.html and
+# https://www.gnu.org/licenses/gpl-3.0.html.
+#
+# $QT_END_LICENSE$
+#
+# SPDX-License-Identifier: LGPL-3.0
+#}
+{% set class = '{0}'.format(struct) %}
+{% include 'generated_comment.cpp.tpl' %}
+{% import 'utils.tpl' as utils %}
+
+#include "{{class|lower}}.h"
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \class {{struct}}
+ \inmodule {{module}}
+{{ utils.format_comments(struct.comment) }}
+*/
+
+{{class}}::{{class}}()
+{% for field in struct.fields %}
+ {% if loop.first %}:{% else %},{% endif %} m_{{field}}({{field|default_type_value}})
+{% endfor %}
+{
+}
+
+{{class}}::{{class}}({% for field in struct.fields %}{% if not loop.first %}, {% endif %}{{field|return_type}} {{field}}{% endfor %})
+{% for field in struct.fields %}
+ {% if loop.first %}:{% else %},{% endif %} m_{{field}}({{field}})
+{% endfor %}
+{
+}
+
+/*! \internal */
+{{class}}::~{{class}}()
+{
+}
+
+{% for field in struct.fields %}
+
+/*!
+ \property {{class}}::{{field}}
+{{ utils.format_comments(field.comment) }}
+{% if field.const %}
+ \note This property is constant and the value will not change once an instance has been created.
+{% endif %}
+*/
+{{field|return_type}} {{class}}::{{field}}() const
+{
+ return m_{{field}};
+}
+{% if not field.readonly and not field.const %}
+
+void {{class}}::set{{field|upperfirst}}({{ field|parameter_type }})
+{
+ m_{{field}} = {{field}};
+}
+{% endif %}
+
+{% endfor %}
+
+QT_END_NAMESPACE
diff --git a/src/ivicore/qface/templates_frontend/struct.h.tpl b/src/ivicore/qface/templates_frontend/struct.h.tpl
new file mode 100644
index 0000000..c14cf26
--- /dev/null
+++ b/src/ivicore/qface/templates_frontend/struct.h.tpl
@@ -0,0 +1,82 @@
+{#
+# Copyright (C) 2017 Pelagicore AG.
+# Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB)
+# Contact: https://www.qt.io/licensing/
+#
+# This file is part of the QtIvi module of the Qt Toolkit.
+#
+# $QT_BEGIN_LICENSE:LGPL-QTAS$
+# Commercial License Usage
+# Licensees holding valid commercial Qt Automotive Suite licenses may use
+# this file in accordance with the commercial license agreement provided
+# with the Software or, alternatively, in accordance with the terms
+# contained in a written agreement between you and The Qt Company. For
+# licensing terms and conditions see https://www.qt.io/terms-conditions.
+# For further information use the contact form at https://www.qt.io/contact-us.
+#
+# GNU Lesser General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU Lesser
+# General Public License version 3 as published by the Free Software
+# Foundation and appearing in the file LICENSE.LGPL3 included in the
+# packaging of this file. Please review the following information to
+# ensure the GNU Lesser General Public License version 3 requirements
+# will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
+#
+# GNU General Public License Usage
+# Alternatively, this file may be used under the terms of the GNU
+# General Public License version 2.0 or (at your option) the GNU General
+# Public license version 3 or any later version approved by the KDE Free
+# Qt Foundation. The licenses are as published by the Free Software
+# Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+# included in the packaging of this file. Please review the following
+# information to ensure the GNU General Public License requirements will
+# be met: https://www.gnu.org/licenses/gpl-2.0.html and
+# https://www.gnu.org/licenses/gpl-3.0.html.
+#
+# $QT_END_LICENSE$
+#
+# SPDX-License-Identifier: LGPL-3.0
+#}
+{% set class = '{0}'.format(struct) %}
+{% set oncedefine = '{0}_{1}_H_'.format(module.module_name|upper, class|upper) %}
+{% set exportsymbol = 'Q_QT{0}_EXPORT'.format(module.module_name|upper) %}
+{% include 'generated_comment.cpp.tpl' %}
+
+#ifndef {{oncedefine}}
+#define {{oncedefine}}
+
+#include "abstract{{module.module_name|lower}}module.h"
+#include <QObject>
+
+QT_BEGIN_NAMESPACE
+
+class {{exportsymbol}} {{class}}
+{
+ Q_GADGET
+{% for field in struct.fields %}
+ Q_PROPERTY({{field|return_type}} {{field}} READ {{field}}{% if not field.readonly and not field.const %} WRITE set{{field|upperfirst}}{% endif %})
+{% endfor %}
+ Q_CLASSINFO("IviPropertyDomains", "{{ struct.fields|json_domain|replace("\"", "\\\"") }}")
+public:
+ {{class}}();
+ {{class}}({% for field in struct.fields %}{% if not loop.first %}, {% endif %}{{field|return_type}} {{field}}{% endfor %});
+ ~{{class}}();
+
+{% for field in struct.fields %}
+ {{field|return_type}} {{field}}() const;
+{% if not field.readonly and not field.const %}
+ void set{{field|upperfirst}}({{field|parameter_type}});
+{% endif %}
+{% endfor %}
+
+private:
+{% for field in struct.fields %}
+ {{field|return_type}} m_{{field}};
+{% endfor %}
+};
+
+QT_END_NAMESPACE
+
+Q_DECLARE_METATYPE({{class}})
+
+#endif // {{oncedefine}}
diff --git a/tests/manual/qface/org.example.echo.qface b/tests/manual/qface/org.example.echo.qface
index ee67a63..82f248c 100644
--- a/tests/manual/qface/org.example.echo.qface
+++ b/tests/manual/qface/org.example.echo.qface
@@ -72,3 +72,12 @@ flag AirflowDirection {
*/
Floor = 4
}
+
+/**
+ * The contact information
+ */
+struct Contact {
+ string name;
+ int age;
+ bool isMarried;
+}