aboutsummaryrefslogtreecommitdiffstats
path: root/include/qdotnetmarshal.h
blob: a8d27dd53dbaba8096f59e829ea34d1ead83aa15 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/***************************************************************************************************
 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 "qdotnetparameter.h"

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

#include <type_traits>

class QDotNetException;
class QDotNetRef;
class QDotNetType;

namespace QtDotNet::TypeNames::System
{
    static constexpr char Void[]{ "System.Void" };
    static constexpr char Boolean[]{ "System.Boolean" };
    static constexpr char Single[]{ "System.Single" };
    static constexpr char Double[]{ "System.Double" };
    static constexpr char Byte[]{ "System.Byte" };
    static constexpr char SByte[]{ "System.SByte" };
    static constexpr char Int16[]{ "System.Int16" };
    static constexpr char UInt16[]{ "System.UInt16" };
    static constexpr char Int32[]{ "System.Int32" };
    static constexpr char UInt32[]{ "System.UInt32" };
    static constexpr char Int64[]{ "System.Int64" };
    static constexpr char UInt64[]{ "System.UInt64" };
    static constexpr char IntPtr[]{ "System.IntPtr" };
    static constexpr char UIntPtr[]{ "System.UIntPtr" };
    static constexpr char Char[]{ "System.Char" };
    static constexpr char String[]{ "System.String" };
    static constexpr char Object[]{ "System.Object" };
    static constexpr char Type[]{ "System.Type" };
}

template<typename T, typename Enable = void>
struct QDotNetTypeOf;

template<typename T, typename Enable = void>
struct QDotNetOutbound;

template<typename T, typename Enable = void>
struct QDotNetInbound;

template<typename T, typename Enable = void>
struct QDotNetNull
{
    static T value() { return T(); }
    static bool isNull(const T &obj) { return obj == value(); }
};

template<typename T>
struct QDotNetOutbound<QDotNetNull<T>>
{
    using SourceType = typename QDotNetOutbound<T>::SourceType;
    using OutboundType = typename QDotNetOutbound<T>::OutboundType;
    static inline const QDotNetParameter Parameter = QDotNetOutbound<T>::Parameter;
    static OutboundType convert(SourceType sourceValue)
    {
        return QDotNetNull<OutboundType>::value();
    }
};

template<typename T>
struct QDotNetInbound<QDotNetNull<T>>
{
    using InboundType = typename QDotNetInbound<T>::InboundType;
    using TargetType = typename QDotNetInbound<T>::TargetType;
    static inline const QDotNetParameter Parameter = QDotNetInbound<T>::Parameter;
    static TargetType convert(InboundType inboundValue)
    {
        return QDotNetNull<InboundType>::value();
    }
};

#define Q_DOTNET_TYPEOF(T,typeName,marshalAs)\
template<>\
struct QDotNetTypeOf<T>\
{\
    static inline const QString TypeName = QString(QtDotNet::TypeNames::typeName);\
    static inline UnmanagedType MarshalAs = marshalAs;\
}

Q_DOTNET_TYPEOF(void, System::Void, UnmanagedType::Void);
Q_DOTNET_TYPEOF(bool, System::Boolean, UnmanagedType::Bool);
Q_DOTNET_TYPEOF(qint8, System::SByte, UnmanagedType::I1);
Q_DOTNET_TYPEOF(quint8, System::Byte, UnmanagedType::U1);
Q_DOTNET_TYPEOF(qint16, System::Int16, UnmanagedType::I2);
Q_DOTNET_TYPEOF(quint16, System::UInt16, UnmanagedType::U2);
Q_DOTNET_TYPEOF(qint32, System::Int32, UnmanagedType::I4);
Q_DOTNET_TYPEOF(quint32, System::UInt32, UnmanagedType::U4);
Q_DOTNET_TYPEOF(qint64, System::Int64, UnmanagedType::I8);
Q_DOTNET_TYPEOF(quint64, System::UInt64, UnmanagedType::U8);
Q_DOTNET_TYPEOF(void *, System::IntPtr, UnmanagedType::SysInt);
Q_DOTNET_TYPEOF(nullptr_t, System::IntPtr, UnmanagedType::SysInt);
Q_DOTNET_TYPEOF(float, System::Single, UnmanagedType::R4);
Q_DOTNET_TYPEOF(double, System::Double, UnmanagedType::R8);

template<typename T>
struct QDotNetOutbound<T, std::enable_if_t<std::is_fundamental_v<T>>>
{
    using SourceType = T;
    using OutboundType = T;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<T>::TypeName, QDotNetTypeOf<T>::MarshalAs);
    static OutboundType convert(SourceType sourceValue)
    {
        return sourceValue;
    }
};

template<typename T>
struct QDotNetInbound<T, std::enable_if_t<std::is_fundamental_v<T>>>
{
    using InboundType = T;
    using TargetType = T;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<T>::TypeName, QDotNetTypeOf<T>::MarshalAs);
    static TargetType convert(InboundType inboundValue)
    {
        return inboundValue;
    }
};

template<typename T>
struct QDotNetNull<T, std::enable_if_t<std::is_fundamental_v<T>>>
{
    static T value() { return T(0); }
    static bool isNull(const T &obj) { return obj == value(); }
};

template<typename T>
struct QDotNetOutbound<T, std::enable_if_t<std::is_pointer_v<T>>>
{
    using SourceType = T;
    using OutboundType = void*;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<void *>::TypeName, QDotNetTypeOf<void *>::MarshalAs);
    static OutboundType convert(SourceType srvValue)
    {
        return reinterpret_cast<void *>(srvValue);
    }
};

template<typename T>
struct QDotNetInbound<T, std::enable_if_t<std::is_pointer_v<T>>>
{
    using InboundType = T;
    using TargetType = T;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<T>::TypeName, QDotNetTypeOf<T>::MarshalAs);
    static TargetType convert(InboundType inboundValue)
    {
        return reinterpret_cast<T>(inboundValue);
    }
};

template<typename T>
struct QDotNetNull<T, std::enable_if_t<std::is_pointer_v<T>>>
{
    static T value() { return nullptr; }
    static bool isNull(const T &ptr) { return ptr == value(); }
};

template<>
struct QDotNetOutbound<void>
{
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<void>::TypeName, QDotNetTypeOf<void>::MarshalAs);
};

template<>
struct QDotNetInbound<void>
{
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<void>::TypeName, QDotNetTypeOf<void>::MarshalAs);
};

template<>
struct QDotNetTypeOf<QString>
{
    static inline const QString TypeName = QStringLiteral("System.String");
    static inline UnmanagedType MarshalAs = UnmanagedType::LPWStr;
};

template<>
struct QDotNetOutbound<QString>
{
    using SourceType = const QString&;
    using OutboundType = const QChar*;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<QString>::TypeName, QDotNetTypeOf<QString>::MarshalAs);
    static OutboundType convert(SourceType sourceValue)
    {
        return sourceValue.data();
    }
};

template<>
struct QDotNetInbound<QString>
{
    using InboundType = const QChar*;
    using TargetType = QString;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<QString>::TypeName, QDotNetTypeOf<QString>::MarshalAs);
    static TargetType convert(InboundType inboundValue)
    {
        return QString(inboundValue);
    }
};

template<>
struct QDotNetNull<QString>
{
    static QString value() { return {}; }
    static bool isNull(const QString &str) { return str.isNull() || str.isEmpty(); }
};

template<typename T>
struct QDotNetOutbound<QList<T>, void>
{
    using SourceType = const QList<T>&;
    using OutboundType = const T*;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(UnmanagedType::LPArray);
    static OutboundType convert(SourceType srvValue)
    {
        return srvValue.constData();
    }
};

template<>
struct QDotNetTypeOf<QDotNetRef>
{
    static inline const QString TypeName = QStringLiteral("System.Object");
    static inline UnmanagedType MarshalAs = UnmanagedType::ObjectRef;
};

template<>
struct QDotNetTypeOf<QDotNetType>
{
    static inline const QString TypeName = QStringLiteral("System.Type");
    static inline UnmanagedType MarshalAs = UnmanagedType::ObjectRef;
};

template<>
struct QDotNetTypeOf<QDotNetException>
{
    static inline const QString TypeName = QStringLiteral("System.Exception");
    static inline UnmanagedType MarshalAs = UnmanagedType::ObjectRef;
};

template<typename T>
struct QDotNetOutbound<T, std::enable_if_t<std::is_base_of_v<QDotNetRef, T>>>
{
    using SourceType = const T&;
    using OutboundType = const void*;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<T>::TypeName, QDotNetTypeOf<T>::MarshalAs);
    static OutboundType convert(SourceType dotNetObj)
    {
        return dotNetObj.gcHandle();
    }
};

template<typename T>
struct QDotNetInbound<T, std::enable_if_t<std::is_base_of_v<QDotNetRef, T>>>
{
    using InboundType = const void*;
    using TargetType = T;
    static inline const QDotNetParameter Parameter =
        QDotNetParameter(QDotNetTypeOf<T>::TypeName, QDotNetTypeOf<T>::MarshalAs);
    static TargetType convert(InboundType gcHandle)
    {
        return T(gcHandle);
    }
};

template<typename T>
struct QDotNetNull<T, std::enable_if_t<std::is_base_of_v<QDotNetRef, T>>>
{
    static T value() { return T(nullptr); }
    static bool isNull(const T &obj) { return !obj.isValid(); }
};

namespace QtDotNet
{
    using Null = QDotNetNull<QDotNetRef>;

    template<typename T>
    bool isNull(const T &obj) { return QDotNetNull<T>::isNull(obj); }

    template<typename T>
    T null() { return QDotNetNull<T>::value(); }
}