Skip to main content

qtbridge_type_lib/core/qmetatype/
mod.rs

1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use crate::QMetaTypeInterface;
5use std::mem::MaybeUninit;
6
7#[cxx::bridge]
8mod ffi {
9    unsafe extern "C++" {
10        include!("qtbridge-type-lib/src/core/qmetatype/cpp/qmetatype.h");
11        type QMetaType = super::QMetaType;
12        include!("qtbridge-type-lib/src/core/qmetatypeinterface/cpp/qmetatypeinterface.h");
13        #[namespace = "QtPrivate"]
14        type QMetaTypeInterface = crate::QMetaTypeInterface;
15    }
16
17    #[namespace = "rust::bridge::qmetatype"]
18    unsafe extern "C++" {
19        #[rust_name = "default_qmetatype"]
20        fn defaultQMetaType() -> QMetaType;
21        #[rust_name = "new_with_interface"]
22        fn newWithInterface(iface: &QMetaTypeInterface) -> QMetaType;
23        fn eq(lhs: &QMetaType, rhs: &QMetaType) -> bool;
24        fn id(obj: &QMetaType) -> i32;
25        fn name(obj: &QMetaType) -> String;
26        #[rust_name = "register_type"]
27        fn registerType(obj: &QMetaType);
28    }
29}
30
31/// The QMetaType struct manages named types in the meta-object system.
32///
33/// See also: [QMetaType documentation](https://doc.qt.io/qt-6/qmetatype.html).
34#[derive(Debug)]
35#[repr(C)]
36pub struct QMetaType {
37    _d_ptr: MaybeUninit<usize>,
38}
39
40unsafe impl cxx::ExternType for QMetaType {
41    type Id = cxx::type_id!("QMetaType");
42    type Kind = cxx::kind::Trivial;
43}
44
45impl Default for QMetaType {
46    fn default() -> Self {
47        ffi::default_qmetatype()
48    }
49}
50
51impl PartialEq for QMetaType {
52    fn eq(&self, other: &Self) -> bool {
53        ffi::eq(self, other)
54    }
55}
56
57#[doc(hidden)]
58pub enum QMetaTypeFlag {
59    NeedsConstruction = 0x1,
60    NeedsDestruction = 0x2,
61    RelocatableType = 0x4,
62    PointerToQObject = 0x8,
63    IsEnumeration = 0x10,
64    SharedPointerToQObject = 0x20,
65    WeakPointerToQObject = 0x40,
66    TrackingPointerToQObject = 0x80,
67    IsUnsignedEnumeration = 0x100,
68    IsGadget = 0x200,
69    PointerToGadget = 0x400,
70    IsPointer = 0x800,
71    IsQmlList = 0x1000,
72    IsConst = 0x2000,
73    NeedsCopyConstruction = 0x4000,
74    NeedsMoveConstruction = 0x8000,
75}
76
77impl QMetaType {
78    /// Creates a `QMetaType` instance from the specified `QMetaTypeInterface`.
79    pub fn new_with_interface(iface: &'static QMetaTypeInterface) -> Self {
80        ffi::new_with_interface(iface)
81    }
82
83    /// Returns id type held by this QMetaType instance.
84    pub fn id(&self) -> i32 {
85        ffi::id(self)
86    }
87
88    /// Returns the type name associated with this QMetaType, or an empty string if type is not valid.
89    pub fn name(&self) -> String {
90        ffi::name(self)
91    }
92
93    /// Registers this QMetaType with the type registry so it can be found by name, using QMetaType::fromName().
94    pub fn register_type(&self) {
95        ffi::register_type(self)
96    }
97}