Skip to content

Commit

Permalink
cxx-qt-gen: add signalhandler template and include it
Browse files Browse the repository at this point in the history
Related to #595
  • Loading branch information
ahayzen-kdab committed Oct 3, 2023
1 parent f968600 commit 7e3243d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 1 deletion.
52 changes: 52 additions & 0 deletions crates/cxx-qt-gen/include/cxxqt_signalhandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
// SPDX-FileContributor: Leon Matthes <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include <type_traits>

#include "rust/cxx.h"

namespace rust::cxxqtlib1 {

template<typename CXXArguments>
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<typename... Arguments>
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<typename CXXArguments>
struct IsRelocatable<rust::cxxqtlib1::SignalHandler<CXXArguments>>
: ::std::true_type
{
};

} // namespace rust
3 changes: 3 additions & 0 deletions crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// List of methods
pub method: CppFragment,
/// Namespace of the method block
Expand Down
10 changes: 10 additions & 0 deletions crates/cxx-qt-gen/src/generator/cpp/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ pub fn generate_cpp_free_signal(
.get(&signal.qobject_ident.to_string()),
);

generated
.includes
.insert("#include <cxx-qt-common/cxxqt_signalhandler.h>".to_owned());

Ok(generated)
}

Expand All @@ -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 <cxx-qt-common/cxxqt_signalhandler.h>".to_owned());
}

for signal in signals {
// Prepare the idents
let idents = QSignalName::from(signal);
Expand Down
4 changes: 4 additions & 0 deletions crates/cxx-qt-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub fn write_headers(directory: impl AsRef<Path>) {
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"),
Expand Down
7 changes: 6 additions & 1 deletion crates/cxx-qt-gen/src/writer/cpp/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::collections::BTreeSet;

use crate::generator::cpp::{fragment::CppFragment, GeneratedCppBlocks};
use crate::writer::cpp::namespaced;
use indoc::formatdoc;
Expand Down Expand Up @@ -130,7 +132,10 @@ pub fn write_cpp_header(generated: &GeneratedCppBlocks) -> String {
.fold(generated.includes.clone(), |mut acc, qobject| {
acc.extend(qobject.blocks.includes.iter().cloned());
acc
}).into_iter().collect::<Vec<String>>().join("\n"),
}).into_iter().chain(generated.extern_cxx_qt.iter().fold(BTreeSet::<String>::default(), |mut acc, block| {
acc.extend(block.includes.iter().cloned());
acc
})).collect::<Vec<String>>().join("\n"),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-gen/test_outputs/passthrough_and_naming.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cxx-qt-common/cxxqt_locking.h>
#include <cxx-qt-common/cxxqt_maybelockguard.h>
#include <cxx-qt-common/cxxqt_signalhandler.h>
#include <cxx-qt-common/cxxqt_type.h>

namespace cxx_qt::multi_object {
Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-gen/test_outputs/properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cxx-qt-common/cxxqt_locking.h>
#include <cxx-qt-common/cxxqt_maybelockguard.h>
#include <cxx-qt-common/cxxqt_signalhandler.h>
#include <cxx-qt-common/cxxqt_type.h>

namespace cxx_qt::my_object {
Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-gen/test_outputs/signals.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cxx-qt-common/cxxqt_locking.h>
#include <cxx-qt-common/cxxqt_maybelockguard.h>
#include <cxx-qt-common/cxxqt_signalhandler.h>
#include <cxx-qt-common/cxxqt_type.h>

namespace cxx_qt::my_object {
Expand Down

0 comments on commit 7e3243d

Please sign in to comment.