hard_cast to fix type punning warnings

master
zetaPRIME 2022-03-19 21:19:03 -04:00
parent 4db24fb188
commit 3137c5e699
4 changed files with 18 additions and 6 deletions

View File

@ -28,6 +28,7 @@ using namespace Xybrid::UI;
#include "ui/patchboard/nodeuiscene.h"
#include "uisocket.h"
#include "util/ext.h"
#include "util/strings.h"
#include <cmath>
@ -66,7 +67,7 @@ void BeatPad::init() {
addPort(Port::Output, Port::Audio, 0);
core.onNoteOn = [this](Note& note) {
auto& data = *reinterpret_cast<NoteData*>(&note.scratch);
auto& data = *hard_cast<NoteData*>(&note.scratch);
new (&data) NoteData(); // construct in-place
// look up config for note
@ -79,7 +80,7 @@ void BeatPad::init() {
};
core.onDeleteNote = [](Note& note) {
auto& data = *reinterpret_cast<NoteData*>(&note.scratch);
auto& data = *hard_cast<NoteData*>(&note.scratch);
data.~NoteData(); // destroy
};
@ -89,7 +90,7 @@ void BeatPad::init() {
};*/
core.processNote = [this](Note& note, AudioPort* p) {
auto& data = *reinterpret_cast<NoteData*>(&note.scratch);
auto& data = *hard_cast<NoteData*>(&note.scratch);
if (!data.config) return core.deleteNote(note);
auto smp = data.config->smp.lock();
if (!smp) return core.deleteNote(note);

View File

@ -28,6 +28,7 @@ using namespace Xybrid::UI;
#include "ui/patchboard/nodeuiscene.h"
#include "uisocket.h"
#include "util/ext.h"
#include "util/strings.h"
#include <cmath>
@ -64,19 +65,19 @@ void Capaxitor::init() {
addPort(Port::Output, Port::Audio, 0);
core.onNoteOn = [this](Note& note) {
auto& data = *reinterpret_cast<NoteData*>(&note.scratch);
auto& data = *hard_cast<NoteData*>(&note.scratch);
new (&data) NoteData(); // construct in-place
note.adsr = adsr;
};
core.onDeleteNote = [](Note& note) {
auto& data = *reinterpret_cast<NoteData*>(&note.scratch);
auto& data = *hard_cast<NoteData*>(&note.scratch);
data.~NoteData(); // destroy
};
core.processNote = [this](Note& note, AudioPort* p) {
auto& data = *reinterpret_cast<NoteData*>(&note.scratch);
auto& data = *hard_cast<NoteData*>(&note.scratch);
auto smp = this->smp.lock();
if (!smp) return core.deleteNote(note);

9
xybrid/util/ext.h Normal file
View File

@ -0,0 +1,9 @@
// miscellaneous "language extensions"
#pragma once
#include <type_traits>
template<typename TO, typename FROM> inline constexpr TO hard_cast(FROM f) {
static_assert(std::is_pointer_v<TO> && std::is_pointer_v<FROM>);
return reinterpret_cast<TO>( reinterpret_cast<void*>(f) );
}

View File

@ -22,6 +22,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x050F00
DISTFILES += ../.astylerc