aboutsummaryrefslogtreecommitdiffstats
path: root/include/qdotnetinterface.h
blob: c6850e34253e1d7522d0a08bd05d637df4f7b7a5 (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
/***************************************************************************************************
 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, void *data = nullptr, void *cleanUp = nullptr)
        : QDotNetRef(adapter().addInterfaceProxy(interfaceName, data, cleanUp))
    {}

    QDotNetInterface(const void *objectRef = nullptr)
        : QDotNetRef(objectRef)
    {}

    QDotNetInterface(const QDotNetInterface &cpySrc)
        : QDotNetRef(cpySrc)
    {}

    QDotNetInterface &operator =(const QDotNetInterface &cpySrc)
    {
        QDotNetRef::operator=(cpySrc);
        return *this;
    }

    QDotNetInterface(QDotNetInterface &&movSrc) noexcept
        : QDotNetRef(std::move(movSrc))
    {}

    QDotNetInterface &operator=(QDotNetInterface &&movSrc) noexcept
    {
        QDotNetRef::operator=(std::move(movSrc));
        return *this;
    }

    template<typename T>
    T *dataAs()
    {
        if (!fnDataPtr.isValid()) {
            const QList<QDotNetParameter> parameters
            {
                QDotNetInbound<void *>::Parameter
            };
            fnDataPtr = adapter().resolveInstanceMethod(*this, "get_Data", parameters);
        }
        return reinterpret_cast<T *>(fnDataPtr());
    }

    virtual ~QDotNetInterface() override
    {
        if (!isValid())
            return;
        for (const QDotNetCallbackBase *callback : callbacks)
            delete callback;
        callbacks.clear();
    }

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

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

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

private:
    QList<QDotNetCallbackBase *> callbacks;
    QDotNetFunction<void *> fnDataPtr = nullptr;
};

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

template<typename T>
struct QDotNetNativeInterface : public QDotNetInterface
{
    QDotNetNativeInterface(const void *objectRef = nullptr)
        : QDotNetInterface(objectRef)
    {
    }

    QDotNetNativeInterface(const QString &interfaceName, T *data, bool doCleanUp = true)
        : QDotNetInterface(interfaceName, data, doCleanUp ? cleanUp : nullptr)
    {
    }

    T *data()
    {
        return dataAs<T>();
    }

    operator T&()
    {
        return *data();
    }

    static void QDOTNETFUNCTION_CALLTYPE cleanUp(void *data)
    {
        if (data)
            delete reinterpret_cast<T *>(data);
    }
};