Skip to main content

qtbridge_runtime/
qmetatypeget.rs

1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use qtbridge_type_lib::{
5    QJsonArray, QJsonObject, QJsonValue,
6    QList, QList_bool, QList_QString, QList_f32, QList_f64, QList_i8, QList_i16, QList_i32, QList_i64,
7    QList_QObjectMutPtr, QList_u8, QList_u16, QList_u32, QList_u64,
8    QMetaType, QObject, QString
9};
10
11#[cxx::bridge]
12mod ffi {
13    unsafe extern "C++" {
14        include!("qtbridge-runtime/src/cpp/qmetatypeget.h");
15        include!("qtbridge-type-lib/src/core/qjsonarray/cpp/qjsonarray.h");
16        include!("qtbridge-type-lib/src/core/qjsonobject/cpp/qjsonobject.h");
17        include!("qtbridge-type-lib/src/core/qjsonvalue/cpp/qjsonvalue.h");
18        include!("qtbridge-type-lib/src/core/qmetatype/cpp/qmetatype.h");
19        include!("qtbridge-type-lib/src/core/qobject/cpp/qobject.h");
20        include!("qtbridge-type-lib/src/core/qstring/cpp/qstring.h");
21        include!("qtbridge-type-lib/src/core/qlist/cpp/qlist.h");
22
23        type QJsonArray = super::QJsonArray;
24        type QJsonObject = super::QJsonObject;
25        type QJsonValue = super::QJsonValue;
26
27        type QMetaType = super::QMetaType;
28        type QObject = super::QObject;
29        type QString = super::QString;
30
31        type QList_bool = super::QList_bool;
32        type QList_i8 = super::QList_i8;
33        type QList_u8 = super::QList_u8;
34        type QList_i16 = super::QList_i16;
35        type QList_u16 = super::QList_u16;
36        type QList_i32 = super::QList_i32;
37        type QList_u32 = super::QList_u32;
38        type QList_i64 = super::QList_i64;
39        type QList_u64 = super::QList_u64;
40        type QList_f32 = super::QList_f32;
41        type QList_f64 = super::QList_f64;
42        type QList_QString = super::QList_QString;
43        type QList_QObjectMutPtr = super::QList_QObjectMutPtr;
44    }
45
46    #[namespace = "rust::bridge::qmetatypeget"]
47    unsafe extern "C++" {
48        // Simple types
49
50        #[rust_name = "get_qmeta_type_bool"]
51        unsafe fn QMetaTypeForType(_: *const bool) -> QMetaType;
52
53        #[rust_name = "get_qmeta_type_i8"]
54        unsafe fn QMetaTypeForType(_: *const i8) -> QMetaType;
55        #[rust_name = "get_qmeta_type_u8"]
56        unsafe fn QMetaTypeForType(_: *const u8) -> QMetaType;
57
58        #[rust_name = "get_qmeta_type_i16"]
59        unsafe fn QMetaTypeForType(_: *const i16) -> QMetaType;
60        #[rust_name = "get_qmeta_type_u16"]
61        unsafe fn QMetaTypeForType(_: *const u16) -> QMetaType;
62
63        #[rust_name = "get_qmeta_type_i32"]
64        unsafe fn QMetaTypeForType(_: *const i32) -> QMetaType;
65        #[rust_name = "get_qmeta_type_u32"]
66        unsafe fn QMetaTypeForType(_: *const u32) -> QMetaType;
67
68        #[rust_name = "get_qmeta_type_i64"]
69        unsafe fn QMetaTypeForType(_: *const i64) -> QMetaType;
70        #[rust_name = "get_qmeta_type_u64"]
71        unsafe fn QMetaTypeForType(_: *const u64) -> QMetaType;
72
73        #[rust_name = "get_qmeta_type_f32"]
74        unsafe fn QMetaTypeForType(_: *const f32) -> QMetaType;
75        #[rust_name = "get_qmeta_type_f64"]
76        unsafe fn QMetaTypeForType(_: *const f64) -> QMetaType;
77
78        #[rust_name = "get_qmeta_type_qstring"]
79        unsafe fn QMetaTypeForType(_: *const QString) -> QMetaType;
80
81        #[rust_name = "get_qmeta_type_qobject"]
82        unsafe fn QMetaTypeForType(_: *const *mut QObject) -> QMetaType;
83
84        #[rust_name = "get_qmeta_type_qjson_array"]
85        unsafe fn QMetaTypeForType(_: *const QJsonArray) -> QMetaType;
86        #[rust_name = "get_qmeta_type_qjson_object"]
87        unsafe fn QMetaTypeForType(_: *const QJsonObject) -> QMetaType;
88        #[rust_name = "get_qmeta_type_qjson_value"]
89        unsafe fn QMetaTypeForType(_: *const QJsonValue) -> QMetaType;
90
91        // QList types
92        #[rust_name = "get_qmeta_type_qlist_bool"]
93        unsafe fn QMetaTypeForType(_: *const QList_bool) -> QMetaType;
94
95        #[rust_name = "get_qmeta_type_qlist_i8"]
96        unsafe fn QMetaTypeForType(_: *const QList_i8) -> QMetaType;
97        #[rust_name = "get_qmeta_type_qlist_u8"]
98        unsafe fn QMetaTypeForType(_: *const QList_u8) -> QMetaType;
99
100        #[rust_name = "get_qmeta_type_qlist_i16"]
101        unsafe fn QMetaTypeForType(_: *const QList_i16) -> QMetaType;
102        #[rust_name = "get_qmeta_type_qlist_u16"]
103        unsafe fn QMetaTypeForType(_: *const QList_u16) -> QMetaType;
104
105        #[rust_name = "get_qmeta_type_qlist_i32"]
106        unsafe fn QMetaTypeForType(_: *const QList_i32) -> QMetaType;
107        #[rust_name = "get_qmeta_type_qlist_u32"]
108        unsafe fn QMetaTypeForType(_: *const QList_u32) -> QMetaType;
109
110        #[rust_name = "get_qmeta_type_qlist_i64"]
111        unsafe fn QMetaTypeForType(_: *const QList_i64) -> QMetaType;
112        #[rust_name = "get_qmeta_type_qlist_u64"]
113        unsafe fn QMetaTypeForType(_: *const QList_u64) -> QMetaType;
114
115        #[rust_name = "get_qmeta_type_qlist_f32"]
116        unsafe fn QMetaTypeForType(_: *const QList_f32) -> QMetaType;
117        #[rust_name = "get_qmeta_type_qlist_f64"]
118        unsafe fn QMetaTypeForType(_: *const QList_f64) -> QMetaType;
119
120        #[rust_name = "get_qmeta_type_qlist_qstring"]
121        unsafe fn QMetaTypeForType(_: *const QList_QString) -> QMetaType;
122
123        #[rust_name = "get_qmeta_type_qlist_qobject"]
124        unsafe fn QMetaTypeForType(_: *const QList_QObjectMutPtr) -> QMetaType;
125    }
126}
127
128#[doc(hidden)]
129pub trait QMetaTypeGet {
130    fn get_qmetatype() -> QMetaType;
131}
132
133macro_rules! impl_qmetatype_get {
134    ($($t:ty => $ffi_fn:ident),*) => {
135        $(
136            impl QMetaTypeGet for $t {
137                fn get_qmetatype() -> QMetaType {
138                    unsafe { ffi::$ffi_fn(std::ptr::null::<$t>()) }
139                }
140            }
141        )*
142    }
143}
144
145macro_rules! impl_qmetatype_get_qlist {
146        ($($t:ty => $ffi_fn_qlist:ident),*) => {
147        $(
148            impl QMetaTypeGet for QList<$t> {
149                fn get_qmetatype() -> QMetaType {
150                    unsafe { ffi::$ffi_fn_qlist(std::ptr::null::<QList<$t>>()) }
151                }
152            }
153        )*
154    }
155}
156
157impl_qmetatype_get! (
158    bool         => get_qmeta_type_bool,
159    i8           => get_qmeta_type_i8,
160    u8           => get_qmeta_type_u8,
161    i16          => get_qmeta_type_i16,
162    u16          => get_qmeta_type_u16,
163    i32          => get_qmeta_type_i32,
164    u32          => get_qmeta_type_u32,
165    i64          => get_qmeta_type_i64,
166    u64          => get_qmeta_type_u64,
167    f32          => get_qmeta_type_f32,
168    f64          => get_qmeta_type_f64,
169    QString      => get_qmeta_type_qstring,
170    *mut QObject => get_qmeta_type_qobject,
171    QJsonArray   => get_qmeta_type_qjson_array,
172    QJsonObject  => get_qmeta_type_qjson_object,
173    QJsonValue   => get_qmeta_type_qjson_value
174);
175
176impl_qmetatype_get_qlist! (
177    bool         => get_qmeta_type_qlist_bool,
178    i8           => get_qmeta_type_qlist_i8,
179    u8           => get_qmeta_type_qlist_u8,
180    i16          => get_qmeta_type_qlist_i16,
181    u16          => get_qmeta_type_qlist_u16,
182    i32          => get_qmeta_type_qlist_i32,
183    u32          => get_qmeta_type_qlist_u32,
184    i64          => get_qmeta_type_qlist_i64,
185    u64          => get_qmeta_type_qlist_u64,
186    f32          => get_qmeta_type_qlist_f32,
187    f64          => get_qmeta_type_qlist_f64,
188    QString      => get_qmeta_type_qlist_qstring
189);
190
191impl QMetaTypeGet for usize {
192    #[cfg(target_pointer_width = "64")]
193    fn get_qmetatype() -> QMetaType {
194        <u64 as QMetaTypeGet>::get_qmetatype()
195    }
196
197    #[cfg(target_pointer_width = "32")]
198    fn get_qmetatype() -> QMetaType {
199        <u32 as QMetaTypeGet>::get_qmetatype()
200    }
201}
202
203impl QMetaTypeGet for isize {
204    #[cfg(target_pointer_width = "64")]
205    fn get_qmetatype() -> QMetaType {
206        <i64 as QMetaTypeGet>::get_qmetatype()
207    }
208
209    #[cfg(target_pointer_width = "32")]
210    fn get_qmetatype() -> QMetaType {
211        <i32 as QMetaTypeGet>::get_qmetatype()
212    }
213}
214
215impl QMetaTypeGet for QList_QObjectMutPtr {
216    fn get_qmetatype() -> QMetaType {
217        unsafe { ffi::get_qmeta_type_qlist_qobject(std::ptr::null()) }
218    }
219}