aboutsummaryrefslogtreecommitdiffstats
path: root/include/qdotnetparameter.h
blob: 5c86dd7a6e13c8d770dacf7fa22eb741c9982413 (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
/***************************************************************************************************
 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 "qdotnethostfxr.h"

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

// Format of parameter info field
// 63......................31...........15............0
// |------ ARRAYLENGTH -----|--- FLAGS --|--- TYPE ---|

constexpr auto TYPE_OFFSET = 0;
constexpr auto TYPE_SIZE = 16;

constexpr auto FLAGS_OFFSET = TYPE_OFFSET + TYPE_SIZE;
constexpr auto FLAGS_SIZE = 16;
constexpr auto FLAGS_IN_BIT = 0;
constexpr auto FLAGS_OUT_BIT = 1;
constexpr auto FLAGS_ARRAY_BIT = 2;
constexpr auto FLAGS_FIXEDLENGTH_BIT = 3;
constexpr auto FLAGS_WEAKREF_BIT = 4;

constexpr auto ARRAYLENGTH_OFFSET = FLAGS_OFFSET + FLAGS_SIZE;
constexpr auto ARRAYLENGTH_SIZE = 32;

template<typename T1, typename T2>
constexpr auto  MASK(T1 value, T2 size)
{
    return static_cast<quint64>(value) & ((static_cast<quint64>(1) << size) - 1);
}
template <typename T1, typename T2>
constexpr auto FLAG(T1 condition, T2 offset)
{
    return (condition ? static_cast<quint64>(1) : static_cast<quint64>(0)) << offset;
}

struct QDotNetParameter
{
    const QChar* typeName = nullptr;
    quint64 paramInfo;

    enum class Template
    {
        ObjectRef =  MASK(UnmanagedType::ObjectRef, TYPE_SIZE) << TYPE_OFFSET
            | FLAG(false, FLAGS_WEAKREF_BIT) << FLAGS_OFFSET,
        ObjectWeakRef =  MASK(UnmanagedType::ObjectRef, TYPE_SIZE) << TYPE_OFFSET
            | FLAG(true, FLAGS_WEAKREF_BIT) << FLAGS_OFFSET,
    };

    QDotNetParameter(const char *typeName, Template paramTemplate)
        : QDotNetParameter(QString(typeName), paramTemplate)
    {}

    QDotNetParameter(const QString &typeName, Template paramTemplate)
        : QDotNetParameter(typeName)
    {
        paramInfo = static_cast<quint64>(paramTemplate);
    }

    QDotNetParameter(UnmanagedType unmanagedType = UnmanagedType::ObjectRef,
            bool isIn = false, bool isOut = false, bool isArray = false,
            bool isFixedLength = false, qint32 arrayLength = 0, bool isWeakRef = false)
        : QDotNetParameter(QString{}, unmanagedType, isIn, isOut, isArray, isFixedLength,
            arrayLength, isWeakRef)
    {}

    QDotNetParameter(const char *typeName, UnmanagedType unmanagedType = UnmanagedType::ObjectRef,
            bool isIn = false, bool isOut = false, bool isArray = false,
            bool isFixedLength = false, qint32 arrayLength = 0, bool isWeakRef = false)
        : QDotNetParameter(QString(typeName),
            unmanagedType, isIn, isOut, isArray, isFixedLength, arrayLength, isWeakRef)
    {}

    QDotNetParameter(const QString &typeName,
        UnmanagedType unmanagedType = UnmanagedType::ObjectRef,bool isIn = false,
        bool isOut = false, bool isArray = false, bool isFixedLength = false,
        qint32 arrayLength = 0, bool isWeakRef = false)
    {
        this->typeName = typeName.constData();
        paramInfo = MASK(unmanagedType, TYPE_SIZE) << TYPE_OFFSET;
        paramInfo |= FLAG(isIn, FLAGS_IN_BIT) << FLAGS_OFFSET;
        paramInfo |= FLAG(isOut, FLAGS_OUT_BIT) << FLAGS_OFFSET;
        paramInfo |= FLAG(isArray, FLAGS_ARRAY_BIT) << FLAGS_OFFSET;
        paramInfo |= FLAG(isFixedLength, FLAGS_FIXEDLENGTH_BIT) << FLAGS_OFFSET;
        paramInfo |= FLAG(isWeakRef, FLAGS_WEAKREF_BIT) << FLAGS_OFFSET;
        paramInfo |= MASK(arrayLength, ARRAYLENGTH_SIZE) << ARRAYLENGTH_OFFSET;
    }

    static const QDotNetParameter &String;
};

inline const QDotNetParameter &QDotNetParameter::String = QDotNetParameter(
    QStringLiteral("Qt.DotNet.StringMarshaler, Qt.DotNet.Adapter, CleanUp=false"),
    UnmanagedType::CustomMarshaler);