From 98720c9cf86ae44b5b3b6d9bef7f4bd549b083f2 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Fri, 25 Aug 2023 14:24:56 +0100 Subject: [PATCH] cxx-qt-gen: add signalhandler template and include it Related to #595 --- .../cxx-qt-gen/include/cxxqt_signalhandler.h | 52 +++++++++++++++++++ .../src/generator/cpp/externcxxqt.rs | 3 ++ crates/cxx-qt-gen/src/generator/cpp/signal.rs | 10 ++++ crates/cxx-qt-gen/src/lib.rs | 4 ++ crates/cxx-qt-gen/src/writer/cpp/header.rs | 5 +- .../test_outputs/passthrough_and_naming.h | 1 + crates/cxx-qt-gen/test_outputs/properties.h | 1 + crates/cxx-qt-gen/test_outputs/signals.h | 1 + 8 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 crates/cxx-qt-gen/include/cxxqt_signalhandler.h diff --git a/crates/cxx-qt-gen/include/cxxqt_signalhandler.h b/crates/cxx-qt-gen/include/cxxqt_signalhandler.h new file mode 100644 index 000000000..4f28d8324 --- /dev/null +++ b/crates/cxx-qt-gen/include/cxxqt_signalhandler.h @@ -0,0 +1,52 @@ +// clang-format off +// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company +// clang-format on +// SPDX-FileContributor: Andrew Hayzen +// SPDX-FileContributor: Leon Matthes +// +// SPDX-License-Identifier: MIT OR Apache-2.0 +#pragma once + +#include + +#include "rust/cxx.h" + +namespace rust::cxxqtlib1 { + +template +class SignalHandler +{ + +public: + SignalHandler() = delete; + SignalHandler(const SignalHandler&) = delete; + + SignalHandler(SignalHandler&& other) + { + data[0] = other.data[0]; + data[1] = other.data[1]; + other.data[0] = nullptr; + other.data[1] = nullptr; + } + + ~SignalHandler() noexcept; + template + void operator()(Arguments... args); + +private: + void* data[2]; +}; + +} // rust::cxxqtlib1 + +// Define namespace otherwise we hit a GCC bug +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480 +namespace rust { + +template +struct IsRelocatable> + : ::std::true_type +{ +}; + +} // namespace rust diff --git a/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs b/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs index 41a7272e6..f3ba79b69 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs @@ -8,10 +8,13 @@ use crate::{ parser::{externcxxqt::ParsedExternCxxQt, mappings::ParsedCxxMappings}, CppFragment, }; +use std::collections::BTreeSet; use syn::Result; #[derive(Default)] pub struct GeneratedCppExternCxxQtBlocks { + /// List of includes + pub includes: BTreeSet, /// List of methods pub method: CppFragment, /// Namespace of the method block diff --git a/crates/cxx-qt-gen/src/generator/cpp/signal.rs b/crates/cxx-qt-gen/src/generator/cpp/signal.rs index 59af1685a..23dae16e7 100644 --- a/crates/cxx-qt-gen/src/generator/cpp/signal.rs +++ b/crates/cxx-qt-gen/src/generator/cpp/signal.rs @@ -126,6 +126,10 @@ pub fn generate_cpp_free_signal( .get(&signal.qobject_ident.to_string()), ); + generated + .includes + .insert("#include ".to_owned()); + Ok(generated) } @@ -137,6 +141,12 @@ pub fn generate_cpp_signals( let mut generated = GeneratedCppQObjectBlocks::default(); let qobject_ident = qobject_idents.cpp_class.cpp.to_string(); + if !signals.is_empty() { + generated + .includes + .insert("#include ".to_owned()); + } + for signal in signals { // Prepare the idents let idents = QSignalName::from(signal); diff --git a/crates/cxx-qt-gen/src/lib.rs b/crates/cxx-qt-gen/src/lib.rs index a09965e41..d3d6b8e41 100644 --- a/crates/cxx-qt-gen/src/lib.rs +++ b/crates/cxx-qt-gen/src/lib.rs @@ -32,6 +32,10 @@ pub fn write_headers(directory: impl AsRef) { include_str!("../include/cxxqt_maybelockguard.h"), "cxxqt_maybelockguard.h", ), + ( + include_str!("../include/cxxqt_signalhandler.h"), + "cxxqt_signalhandler.h", + ), (include_str!("../include/cxxqt_thread.h"), "cxxqt_thread.h"), ( include_str!("../include/cxxqt_threading.h"), diff --git a/crates/cxx-qt-gen/src/writer/cpp/header.rs b/crates/cxx-qt-gen/src/writer/cpp/header.rs index 3bd20c048..95a9ae2bb 100644 --- a/crates/cxx-qt-gen/src/writer/cpp/header.rs +++ b/crates/cxx-qt-gen/src/writer/cpp/header.rs @@ -131,7 +131,10 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String { .fold(BTreeSet::<&String>::default(), |mut acc, qobject| { acc.extend(qobject.blocks.includes.iter()); acc - }).into_iter().cloned().collect::>().join("\n"), + }).into_iter().chain(generated.extern_cxx_qt.iter().fold(BTreeSet::<&String>::default(), |mut acc, block| { + acc.extend(block.includes.iter()); + acc + })).cloned().collect::>().join("\n"), } } diff --git a/crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h b/crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h index 9b0a63ac0..941c886b1 100644 --- a/crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h +++ b/crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h @@ -2,6 +2,7 @@ #include #include +#include #include namespace cxx_qt::multi_object { diff --git a/crates/cxx-qt-gen/test_outputs/properties.h b/crates/cxx-qt-gen/test_outputs/properties.h index 16a99ff69..6f708d326 100644 --- a/crates/cxx-qt-gen/test_outputs/properties.h +++ b/crates/cxx-qt-gen/test_outputs/properties.h @@ -2,6 +2,7 @@ #include #include +#include #include namespace cxx_qt::my_object { diff --git a/crates/cxx-qt-gen/test_outputs/signals.h b/crates/cxx-qt-gen/test_outputs/signals.h index 8c3babd25..04d54a358 100644 --- a/crates/cxx-qt-gen/test_outputs/signals.h +++ b/crates/cxx-qt-gen/test_outputs/signals.h @@ -2,6 +2,7 @@ #include #include +#include #include namespace cxx_qt::my_object {