Qt: Cleaner type punning

This commit is contained in:
Vicki Pfau 2023-04-20 02:43:06 -07:00
parent 5f6948351b
commit 420da428bd
6 changed files with 41 additions and 43 deletions

View File

@ -998,9 +998,9 @@ void CoreController::attachPrinter() {
} }
GB* gb = static_cast<GB*>(m_threadContext.core->board); GB* gb = static_cast<GB*>(m_threadContext.core->board);
clearMultiplayerController(); clearMultiplayerController();
GBPrinterCreate(&m_printer.d); GBPrinterCreate(&m_printer);
m_printer.parent = this; m_printer.parent = this;
m_printer.d.print = [](GBPrinter* printer, int height, const uint8_t* data) { m_printer.print = [](GBPrinter* printer, int height, const uint8_t* data) {
QGBPrinter* qPrinter = reinterpret_cast<QGBPrinter*>(printer); QGBPrinter* qPrinter = reinterpret_cast<QGBPrinter*>(printer);
QImage image(GB_VIDEO_HORIZONTAL_PIXELS, height, QImage::Format_Indexed8); QImage image(GB_VIDEO_HORIZONTAL_PIXELS, height, QImage::Format_Indexed8);
QVector<QRgb> colors; QVector<QRgb> colors;
@ -1021,7 +1021,7 @@ void CoreController::attachPrinter() {
QMetaObject::invokeMethod(qPrinter->parent, "imagePrinted", Q_ARG(const QImage&, image)); QMetaObject::invokeMethod(qPrinter->parent, "imagePrinted", Q_ARG(const QImage&, image));
}; };
Interrupter interrupter(this); Interrupter interrupter(this);
GBSIOSetDriver(&gb->sio, &m_printer.d.d); GBSIOSetDriver(&gb->sio, &m_printer.d);
} }
void CoreController::detachPrinter() { void CoreController::detachPrinter() {
@ -1030,7 +1030,7 @@ void CoreController::detachPrinter() {
} }
Interrupter interrupter(this); Interrupter interrupter(this);
GB* gb = static_cast<GB*>(m_threadContext.core->board); GB* gb = static_cast<GB*>(m_threadContext.core->board);
GBPrinterDonePrinting(&m_printer.d); GBPrinterDonePrinting(&m_printer);
GBSIOSetDriver(&gb->sio, nullptr); GBSIOSetDriver(&gb->sio, nullptr);
} }
@ -1039,7 +1039,7 @@ void CoreController::endPrint() {
return; return;
} }
Interrupter interrupter(this); Interrupter interrupter(this);
GBPrinterDonePrinting(&m_printer.d); GBPrinterDonePrinting(&m_printer);
} }
#endif #endif

View File

@ -314,8 +314,7 @@ private:
VFile* m_vlVf = nullptr; VFile* m_vlVf = nullptr;
#ifdef M_CORE_GB #ifdef M_CORE_GB
struct QGBPrinter { struct QGBPrinter : public GBPrinter {
GBPrinter d;
CoreController* parent; CoreController* parent;
} m_printer; } m_printer;
#endif #endif

View File

@ -19,18 +19,18 @@ using namespace QGBA;
DebuggerConsoleController::DebuggerConsoleController(QObject* parent) DebuggerConsoleController::DebuggerConsoleController(QObject* parent)
: DebuggerController(&m_cliDebugger.d, parent) : DebuggerController(&m_cliDebugger.d, parent)
{ {
m_backend.d.printf = printf; m_backend.printf = printf;
m_backend.d.init = init; m_backend.init = init;
m_backend.d.deinit = deinit; m_backend.deinit = deinit;
m_backend.d.readline = readLine; m_backend.readline = readLine;
m_backend.d.lineAppend = lineAppend; m_backend.lineAppend = lineAppend;
m_backend.d.historyLast = historyLast; m_backend.historyLast = historyLast;
m_backend.d.historyAppend = historyAppend; m_backend.historyAppend = historyAppend;
m_backend.d.interrupt = interrupt; m_backend.interrupt = interrupt;
m_backend.self = this; m_backend.self = this;
CLIDebuggerCreate(&m_cliDebugger); CLIDebuggerCreate(&m_cliDebugger);
CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend.d); CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend);
} }
void DebuggerConsoleController::enterLine(const QString& line) { void DebuggerConsoleController::enterLine(const QString& line) {
@ -60,7 +60,7 @@ void DebuggerConsoleController::attachInternal() {
CoreController::Interrupter interrupter(m_gameController); CoreController::Interrupter interrupter(m_gameController);
QMutexLocker lock(&m_mutex); QMutexLocker lock(&m_mutex);
mCore* core = m_gameController->thread()->core; mCore* core = m_gameController->thread()->core;
CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend.d); CLIDebuggerAttachBackend(&m_cliDebugger, &m_backend);
CLIDebuggerAttachSystem(&m_cliDebugger, core->cliDebuggerSystem(core)); CLIDebuggerAttachSystem(&m_cliDebugger, core->cliDebuggerSystem(core));
} }

View File

@ -56,8 +56,7 @@ private:
QStringList m_lines; QStringList m_lines;
QByteArray m_last; QByteArray m_last;
struct Backend { struct Backend : public CLIDebuggerBackend {
CLIDebuggerBackend d;
DebuggerConsoleController* self; DebuggerConsoleController* self;
} m_backend; } m_backend;
}; };

View File

@ -12,39 +12,40 @@
using namespace QGBA; using namespace QGBA;
VideoProxy::VideoProxy() { VideoProxy::VideoProxy() {
mVideoLoggerRendererCreate(&m_logger.d, false); mVideoLoggerRendererCreate(&m_logger, false);
m_logger.d.block = true; m_logger.p = this;
m_logger.d.waitOnFlush = true; m_logger.block = true;
m_logger.waitOnFlush = true;
m_logger.d.init = &cbind<&VideoProxy::init>; m_logger.init = &cbind<&VideoProxy::init>;
m_logger.d.reset = &cbind<&VideoProxy::reset>; m_logger.reset = &cbind<&VideoProxy::reset>;
m_logger.d.deinit = &cbind<&VideoProxy::deinit>; m_logger.deinit = &cbind<&VideoProxy::deinit>;
m_logger.d.lock = &cbind<&VideoProxy::lock>; m_logger.lock = &cbind<&VideoProxy::lock>;
m_logger.d.unlock = &cbind<&VideoProxy::unlock>; m_logger.unlock = &cbind<&VideoProxy::unlock>;
m_logger.d.wait = &cbind<&VideoProxy::wait>; m_logger.wait = &cbind<&VideoProxy::wait>;
m_logger.d.wake = &callback<void, int>::func<&VideoProxy::wake>; m_logger.wake = &callback<void, int>::func<&VideoProxy::wake>;
m_logger.d.writeData = &callback<bool, const void*, size_t>::func<&VideoProxy::writeData>; m_logger.writeData = &callback<bool, const void*, size_t>::func<&VideoProxy::writeData>;
m_logger.d.readData = &callback<bool, void*, size_t, bool>::func<&VideoProxy::readData>; m_logger.readData = &callback<bool, void*, size_t, bool>::func<&VideoProxy::readData>;
m_logger.d.postEvent = &callback<void, enum mVideoLoggerEvent>::func<&VideoProxy::postEvent>; m_logger.postEvent = &callback<void, enum mVideoLoggerEvent>::func<&VideoProxy::postEvent>;
connect(this, &VideoProxy::dataAvailable, this, &VideoProxy::processData); connect(this, &VideoProxy::dataAvailable, this, &VideoProxy::processData);
} }
void VideoProxy::attach(CoreController* controller) { void VideoProxy::attach(CoreController* controller) {
CoreController::Interrupter interrupter(controller); CoreController::Interrupter interrupter(controller);
controller->thread()->core->videoLogger = &m_logger.d; controller->thread()->core->videoLogger = &m_logger;
} }
void VideoProxy::detach(CoreController* controller) { void VideoProxy::detach(CoreController* controller) {
CoreController::Interrupter interrupter(controller); CoreController::Interrupter interrupter(controller);
if (controller->thread()->core->videoLogger == &m_logger.d) { if (controller->thread()->core->videoLogger == &m_logger) {
controller->thread()->core->videoLogger = nullptr; controller->thread()->core->videoLogger = nullptr;
} }
} }
void VideoProxy::processData() { void VideoProxy::processData() {
mVideoLoggerRendererRun(&m_logger.d, false); mVideoLoggerRendererRun(&m_logger, false);
m_fromThreadCond.wakeAll(); m_fromThreadCond.wakeAll();
} }
@ -67,7 +68,7 @@ bool VideoProxy::writeData(const void* data, size_t length) {
while (!RingFIFOWrite(&m_dirtyQueue, data, length)) { while (!RingFIFOWrite(&m_dirtyQueue, data, length)) {
if (QThread::currentThread() == thread()) { if (QThread::currentThread() == thread()) {
// We're on the main thread // We're on the main thread
mVideoLoggerRendererRun(&m_logger.d, false); mVideoLoggerRendererRun(&m_logger, false);
} else { } else {
emit dataAvailable(); emit dataAvailable();
m_mutex.lock(); m_mutex.lock();
@ -105,7 +106,7 @@ void VideoProxy::postEvent(enum mVideoLoggerEvent event) {
void VideoProxy::handleEvent(int event) { void VideoProxy::handleEvent(int event) {
m_mutex.lock(); m_mutex.lock();
m_logger.d.handleEvent(&m_logger.d, static_cast<enum mVideoLoggerEvent>(event)); m_logger.handleEvent(&m_logger, static_cast<enum mVideoLoggerEvent>(event));
m_mutex.unlock(); m_mutex.unlock();
} }
@ -122,7 +123,7 @@ void VideoProxy::wait() {
while (RingFIFOSize(&m_dirtyQueue)) { while (RingFIFOSize(&m_dirtyQueue)) {
if (QThread::currentThread() == thread()) { if (QThread::currentThread() == thread()) {
// We're on the main thread // We're on the main thread
mVideoLoggerRendererRun(&m_logger.d, false); mVideoLoggerRendererRun(&m_logger, false);
} else { } else {
emit dataAvailable(); emit dataAvailable();
m_toThreadCond.wakeAll(); m_toThreadCond.wakeAll();

View File

@ -24,7 +24,7 @@ public:
void attach(CoreController*); void attach(CoreController*);
void detach(CoreController*); void detach(CoreController*);
void setBlocking(bool block) { m_logger.d.waitOnFlush = block; } void setBlocking(bool block) { m_logger.waitOnFlush = block; }
signals: signals:
void dataAvailable(); void dataAvailable();
@ -51,17 +51,16 @@ private:
using type = T (VideoProxy::*)(A...); using type = T (VideoProxy::*)(A...);
template<type F> static T func(mVideoLogger* logger, A... args) { template<type F> static T func(mVideoLogger* logger, A... args) {
VideoProxy* proxy = reinterpret_cast<Logger*>(logger)->p; VideoProxy* proxy = static_cast<Logger*>(logger)->p;
return (proxy->*F)(args...); return (proxy->*F)(args...);
} }
}; };
template<void (VideoProxy::*F)()> static void cbind(mVideoLogger* logger) { callback<void>::func<F>(logger); } template<void (VideoProxy::*F)()> static void cbind(mVideoLogger* logger) { callback<void>::func<F>(logger); }
struct Logger { struct Logger : public mVideoLogger {
mVideoLogger d;
VideoProxy* p; VideoProxy* p;
} m_logger = {{}, this}; } m_logger;
RingFIFO m_dirtyQueue; RingFIFO m_dirtyQueue;
QMutex m_mutex; QMutex m_mutex;