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);
clearMultiplayerController();
GBPrinterCreate(&m_printer.d);
GBPrinterCreate(&m_printer);
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);
QImage image(GB_VIDEO_HORIZONTAL_PIXELS, height, QImage::Format_Indexed8);
QVector<QRgb> colors;
@ -1021,7 +1021,7 @@ void CoreController::attachPrinter() {
QMetaObject::invokeMethod(qPrinter->parent, "imagePrinted", Q_ARG(const QImage&, image));
};
Interrupter interrupter(this);
GBSIOSetDriver(&gb->sio, &m_printer.d.d);
GBSIOSetDriver(&gb->sio, &m_printer.d);
}
void CoreController::detachPrinter() {
@ -1030,7 +1030,7 @@ void CoreController::detachPrinter() {
}
Interrupter interrupter(this);
GB* gb = static_cast<GB*>(m_threadContext.core->board);
GBPrinterDonePrinting(&m_printer.d);
GBPrinterDonePrinting(&m_printer);
GBSIOSetDriver(&gb->sio, nullptr);
}
@ -1039,7 +1039,7 @@ void CoreController::endPrint() {
return;
}
Interrupter interrupter(this);
GBPrinterDonePrinting(&m_printer.d);
GBPrinterDonePrinting(&m_printer);
}
#endif

View File

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

View File

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

View File

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

View File

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

View File

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