aboutsummaryrefslogtreecommitdiffstats
path: root/include/qdotnetinterface.h
blob: 4dae61c4b99894891d10a1d80fd4ca3eb9656b6b (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
/***************************************************************************************************
 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
***************************************************************************************************/

#pragma once

#include "qdotnetref.h"
#include "qdotnetcallback.h"

#ifdef __GNUC__
#   pragma GCC diagnostic push
#   pragma GCC diagnostic ignored "-Wconversion"
#endif
#include <QList>
#include <QString>
#ifdef __GNUC__
#   pragma GCC diagnostic pop
#endif

class QDotNetInterface : public QDotNetRef
{
public:
    QDotNetInterface(const QString &interfaceName)
        : QDotNetRef(adapter().addInterfaceProxy(interfaceName))
    {}

    template<typename TResult, typename... TArg>
    void setCallback(const QString &methodName, const QList<QDotNetParameter> &params,
        typename QDotNetCallback<TResult, TArg...>::FunctionType function)
    {
        auto *callback = new QDotNetCallback<TResult, TArg...>(function);
        callbacks.append(callback);

        QList<QDotNetParameter> modifiedParams
        {
            params[0],
            UnmanagedType::SysInt,
            UnmanagedType::U8
        };
        for (qsizetype i = 1; i < params.size(); ++i)
            modifiedParams.append(params[i]);

        adapter().setInterfaceMethod(*this, methodName, modifiedParams,
            reinterpret_cast<void *>(callback->delegate()),
            reinterpret_cast<void *>(callback->cleanUp()), callback);
    }

    template<typename TResult, typename... TArg>
    void setCallback(const QString &methodName,
        typename QDotNetCallback<TResult, TArg...>::FunctionType function)
    {
        auto *callback = new QDotNetCallback<TResult, TArg...>(function);
        callbacks.append(callback);

        const QList<QDotNetParameter> parameters
        {
            QDotNetInbound<TResult>::Parameter,
            UnmanagedType::SysInt,
            UnmanagedType::U8,
            QDotNetInbound<TArg>::Parameter...
        };

        adapter().setInterfaceMethod(
            *this, methodName, parameters,
            callback->delegate(), callback->cleanUp(), callback);
    }

    ~QDotNetInterface() override
    {
        for (const QDotNetCallbackBase *callback : callbacks)
            delete callback;
        callbacks.clear();
    }

private:
    QList<QDotNetCallbackBase *> callbacks;
};

template<typename T>
struct QDotNetTypeOf<T, std::enable_if_t<std::is_base_of_v<QDotNetInterface, T>>>
{
    static inline const QString TypeName = T::FullyQualifiedTypeName;
    static inline UnmanagedType MarshalAs = UnmanagedType::ObjectRef;
};