aboutsummaryrefslogtreecommitdiffstats
path: root/tests/tst_qtdotnet/foo.cpp
blob: 2ede26cf880fbd4210a77f86202e3c732e51ba94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************************************************************
 Copyright (C) 2023 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

#include "foo.h"

#include <qdotnetevent.h>

struct FooPrivate final : QDotNetEventHandler
{
    Foo *q;
    FooPrivate(Foo *q) : q(q) {}

    QDotNetFunction<Foo, IBarTransformation> ctor = nullptr;

    QDotNetFunction<QString> bar;
    QDotNetFunction<void, QString> setBar;

    QDotNetFunction<int, QDotNetRef> fnFooField = nullptr;
    QDotNetFunction<void, QDotNetRef, int> fnSetFooField = nullptr;

    QDotNetType typePropertyEvent = nullptr;

    void handleEvent(const QString &eventName, QDotNetObject &sender, QDotNetObject &args) override
    {
        if (eventName != "PropertyChanged")
            return;

        if (!typePropertyEvent.isValid())
            typePropertyEvent = QDotNetType::typeOf<QDotNetPropertyEvent>();
        if (!args.type().equals(typePropertyEvent))
            return;

        const auto propertyChangedEvent = args.cast<QDotNetPropertyEvent>();
        if (propertyChangedEvent.propertyName() == "Bar")
            emit q->barChanged();
    }
};

Q_DOTNET_OBJECT_IMPL(Foo, Q_DOTNET_OBJECT_INIT(d(new FooPrivate(this))));

Foo::Foo() : d(new FooPrivate(this))
{
    const auto ctor = constructor<Foo, Null<IBarTransformation>>();
    *this = ctor(nullptr);
    subscribe("PropertyChanged", d);
}

Foo::Foo(const IBarTransformation &transformation) : d(new FooPrivate(this))
{
    *this = constructor(d->ctor).invoke(*this, transformation);
    subscribe("PropertyChanged", d);
}

Foo::~Foo()
{
    delete d;
}

QString Foo::bar() const
{
    return method("get_Bar", d->bar).invoke(*this);
}

void Foo::setBar(const QString &value)
{
    method("set_Bar", d->setBar).invoke(*this, value);
}

int Foo::fooNumberConst()
{
    static QDotNetFunction<int> fnFieldGet = nullptr;
    static int fieldValue;
    if (!fnFieldGet.isValid()) {
        fieldValue = QDotNetType::typeOf<Foo>()
            .staticFieldGet("FooNumber", fnFieldGet)
            .invoke(nullptr);
    }
    return fieldValue;
}

QString Foo::fooStringConst()
{
    static QDotNetFunction<QString> fnFieldGet = nullptr;
    static QString fieldValue;
    if (!fnFieldGet.isValid()) {
        fieldValue = QDotNetType::typeOf<Foo>()
            .staticFieldGet("FooString", fnFieldGet)
            .invoke(nullptr);
    }
    return fieldValue;
}

int Foo::fooStaticField()
{
    static QDotNetFunction<int> fnFieldGet = nullptr;
    QDotNetType::staticFieldGet(AssemblyQualifiedName, "FooStaticField", fnFieldGet);
    return fnFieldGet();
}

void Foo::setFooStaticField(int value)
{
    static QDotNetFunction<void, int> fnFieldSet = nullptr;
    QDotNetType::staticFieldSet(AssemblyQualifiedName, "FooStaticField", fnFieldSet);
    fnFieldSet(value);
}

int Foo::fooField()
{
    return fieldGet<int>("FooField", d->fnFooField).invoke(nullptr, *this);
}

void Foo::setFooField(int value)
{
    return fieldSet<int>("FooField", d->fnSetFooField).invoke(nullptr, *this, value);
}

QModelIndex Foo::findIndex()
{
    return QtDotNet::call<QModelIndex>(AssemblyQualifiedName, "FindIndex");
}

QString Foo::dataAt(const QModelIndex &idx)
{
    return QtDotNet::call<QString, QModelIndex>(AssemblyQualifiedName, "DataAt", idx);
}

QDateTime Foo::getDateTime()
{
    return QtDotNet::call<QDateTime>(AssemblyQualifiedName, "GetDateTime");
}

QString Foo::printDateTime(const QDateTime &t)
{
    return QtDotNet::call<QString, QDateTime>(AssemblyQualifiedName, "PrintDateTime", t);
}

QUrl Foo::getUri()
{
    return QtDotNet::call<QUrl>(AssemblyQualifiedName, "GetUri");
}

QString Foo::printUri(const QUrl url)
{
    return QtDotNet::call<QString, QUrl>(AssemblyQualifiedName, "PrintUri", url);
}

IBarTransformation::IBarTransformation() : QDotNetInterface(AssemblyQualifiedName, nullptr)
{
    setCallback<QString, QString>("Transform",
        [this](void *, const QString &bar) {
            return transform(bar);
        });

    setCallback<Uri, int>("GetUri",
        [this](void *, int n)
        {
            return getUri(n);
        });

    setCallback<void, Uri>("SetUri",
        [this](void *, Uri uri)
        {
            setUri(uri);
        });

    setCallback<int>("GetNumber",
        [this](void *)
        {
            return getNumber();
        });
}