diff options
author | Juergen Bocklage-Ryannel <juergen.bocklage-ryannel@pelagicore.com> | 2017-01-18 14:24:53 +0100 |
---|---|---|
committer | Juergen Bocklage-Ryannel <juergen.bocklage-ryannel@pelagicore.com> | 2017-01-18 14:25:17 +0100 |
commit | 194ce3249856d1bb03c8d651321ed381fea01007 (patch) | |
tree | 9ad6efea6a9ce92d9e857dd7991a9f6ab7c8b477 | |
parent | 4462c8abbe8ad0e1f6649ccecac543206f3ba9ef (diff) | |
download | qtivi-qface-194ce3249856d1bb03c8d651321ed381fea01007.tar.gz |
struct uses now an explicit-shared data approach. Now it behaves more like a JS object. e.g. a.name = ‘A’; b = a; a.name = ‘X’; a.name === b.name
-rw-r--r-- | builtin/qtcpp/templates/struct.cpp | 74 | ||||
-rw-r--r-- | builtin/qtcpp/templates/struct.h | 22 | ||||
-rw-r--r-- | builtin/qtcpp/templates/structmodel.cpp | 6 |
3 files changed, 94 insertions, 8 deletions
diff --git a/builtin/qtcpp/templates/struct.cpp b/builtin/qtcpp/templates/struct.cpp index 5b132ec..70dd967 100644 --- a/builtin/qtcpp/templates/struct.cpp +++ b/builtin/qtcpp/templates/struct.cpp @@ -9,13 +9,83 @@ #include "{{class|lower}}.h" + +// Shared Data + +class {{class}}Data : public QSharedData +{ +public: + {{class}}Data() + : QSharedData() + {% for field in struct.fields %} + , {{field}}({{field|defaultValue}}) + {% endfor %} + { + } + {{class}}Data(const {{class}}Data &other) + : QSharedData(other) + {% for field in struct.fields %} + , {{field}}(other.{{field}}) + {% endfor %} + { + } + +public: +{% for field in struct.fields %} + {{field|returnType}} {{field}}; +{% endfor %} +}; + +// Class {{struct.comment}} +{{class}}::{{class}}() + : d(new {{class}}Data) +{ +} + +{{class}}::{{class}}(const {{class}} &other) + : d(other.d) +{ +} + +{{class}}::~{{class}}() +{ +} + +{% for field in struct.fields %} +void {{class}}::set{{field|upperfirst}}({{field|parameterType}}) +{ + d->{{field}} = {{field}}; +} +{{field|returnType}} {{class}}::{{field}}() const +{ + return d->{{field}}; +} + +{% endfor %} + + + +{{class}} {{class}}::clone() +{ + {{class}} other(*this); + other.d.detach(); + return other; +} + bool {{class}}::operator==(const {{class}} &other) const { return ( {%- for field in struct.fields %}{{ ampersand() }} - m_{{field}} == other.m_{{field}} - {%- endfor %} ); + {{field}}() == other.{{field}}() + {%- endfor %} + ); +} + +{{class}} &{{class}}::operator=(const {{class}} &other) +{ + d = other.d; + return *this; } diff --git a/builtin/qtcpp/templates/struct.h b/builtin/qtcpp/templates/struct.h index 2e0d6d1..7c6a4e6 100644 --- a/builtin/qtcpp/templates/struct.h +++ b/builtin/qtcpp/templates/struct.h @@ -9,19 +9,35 @@ #include <QtCore> + +class {{class}}Data; + class {{class}} { Q_GADGET {% for field in struct.fields %} - Q_PROPERTY({{field|returnType}} {{field}} MEMBER m_{{field}}) + Q_PROPERTY({{field|returnType}} {{field}} READ {{field}} WRITE set{{field|upperfirst}}) {% endfor %} public: + {{class}}(); + {{class}}(const {{class}} &other); + ~{{class}}(); + + Q_INVOKABLE {{class}} clone(); + + bool operator==(const {{class}} &other) const; + {{class}} &operator=(const {{class}} &other); + {% for field in struct.fields %} - {{field|returnType}} m_{{field}}; + void set{{field|upperfirst}}({{field|parameterType}}); + {{field|returnType}} {{field}}() const; + {% endfor %} - bool operator==(const {{class}} &other) const; + +private: + QExplicitlySharedDataPointer <{{class}}Data> d; }; Q_DECLARE_METATYPE({{class}}) diff --git a/builtin/qtcpp/templates/structmodel.cpp b/builtin/qtcpp/templates/structmodel.cpp index 3f13cb6..2759dae 100644 --- a/builtin/qtcpp/templates/structmodel.cpp +++ b/builtin/qtcpp/templates/structmodel.cpp @@ -40,7 +40,7 @@ QVariant {{class}}::data(const QModelIndex &index, int role) const switch(role) { {% for field in struct.fields %} case Roles::{{field|upperfirst}}: - return QVariant::fromValue({{struct|lower}}.m_{{field}}); + return QVariant::fromValue({{struct|lower}}.{{field}}()); {% endfor %} } return QVariant(); @@ -58,7 +58,7 @@ void {{class}}::insert(int row, const Qml{{struct}} &{{struct|lower}}) row = 0; if (row >= m_data.count()) row = m_data.count(); - + beginInsertRows(QModelIndex(), row, row); m_data.insert(row, {{struct|lower}}); endInsertRows(); @@ -78,7 +78,7 @@ void {{class}}::append(const Qml{{struct}} &{{struct|lower}}) } void {{class}}::update(int row, const Qml{{struct}} &{{struct|lower}}) -{ +{ if(row < 0 || row >= m_data.count()) { return; } |