/*************************************************************************************************** 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 struct FooPrivate final : QDotNetEventHandler { Foo *q; FooPrivate(Foo *q) : q(q) {} QDotNetFunction ctor = nullptr; QDotNetFunction bar; QDotNetFunction setBar; QDotNetFunction fnFooField = nullptr; QDotNetFunction 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(); if (!args.type().equals(typePropertyEvent)) return; const auto propertyChangedEvent = args.cast(); 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>(); *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 fnFieldGet = nullptr; static int fieldValue; if (!fnFieldGet.isValid()) { fieldValue = QDotNetType::typeOf() .staticFieldGet("FooNumber", fnFieldGet) .invoke(nullptr); } return fieldValue; } QString Foo::fooStringConst() { static QDotNetFunction fnFieldGet = nullptr; static QString fieldValue; if (!fnFieldGet.isValid()) { fieldValue = QDotNetType::typeOf() .staticFieldGet("FooString", fnFieldGet) .invoke(nullptr); } return fieldValue; } int Foo::fooStaticField() { static QDotNetFunction fnFieldGet = nullptr; QDotNetType::staticFieldGet(AssemblyQualifiedName, "FooStaticField", fnFieldGet); return fnFieldGet(); } void Foo::setFooStaticField(int value) { static QDotNetFunction fnFieldSet = nullptr; QDotNetType::staticFieldSet(AssemblyQualifiedName, "FooStaticField", fnFieldSet); fnFieldSet(value); } int Foo::fooField() { return fieldGet("FooField", d->fnFooField).invoke(nullptr, *this); } void Foo::setFooField(int value) { return fieldSet("FooField", d->fnSetFooField).invoke(nullptr, *this, value); } IBarTransformation::IBarTransformation() : QDotNetInterface(AssemblyQualifiedName, nullptr) { setCallback("Transform", [this](void *, const QString &bar) { return transform(bar); }); setCallback("GetUri", [this](void *, int n) { return getUri(n); }); setCallback("SetUri", [this](void *, Uri uri) { setUri(uri); }); setCallback("GetNumber", [this](void *) { return getNumber(); }); }