diff --git a/src/olympic/turbine/include/turbine/context.hpp b/src/olympic/turbine/include/turbine/context.hpp index 557e7b95..14a6dc2e 100644 --- a/src/olympic/turbine/include/turbine/context.hpp +++ b/src/olympic/turbine/include/turbine/context.hpp @@ -37,15 +37,37 @@ inline ox::FileSystem *rom(Context &ctx) noexcept { return keelCtx(ctx).rom.get(); } -void setApplicationData(Context &ctx, void *applicationData) noexcept; +struct WrapBase { + virtual ~WrapBase() = default; +}; + +template +struct Wrap: public WrapBase { + T *data{}; +}; + +void setApplicationDataRaw(Context &ctx, void *applicationData) noexcept; + +template +void setApplicationData(Context &ctx, T *applicationData) noexcept { + Wrap w; + w.data = applicationData; + setApplicationDataRaw(ctx, &w); +} [[nodiscard]] -void *applicationDataRaw(Context &ctx) noexcept; +WrapBase *applicationDataRaw(Context &ctx) noexcept; template [[nodiscard]] T *applicationData(Context &ctx) noexcept { - return static_cast(applicationDataRaw(ctx)); +#ifdef OX_BARE_METAL + return static_cast*>(applicationDataRaw(ctx))->data; +#else + auto const out = dynamic_cast*>(applicationDataRaw(ctx)); + oxAssert(out, "Cast failed - wrong type"); + return out->data; +#endif } void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept; diff --git a/src/olympic/turbine/src/gba/context.cpp b/src/olympic/turbine/src/gba/context.cpp index c1ce23c3..38c79d51 100644 --- a/src/olympic/turbine/src/gba/context.cpp +++ b/src/olympic/turbine/src/gba/context.cpp @@ -18,12 +18,20 @@ keel::Context &keelCtx(Context &ctx) noexcept { return ctx.keelCtx; } -void setApplicationData(Context &ctx, void *applicationData) noexcept { - ctx.applicationData = applicationData; +void setApplicationDataRaw(Context &ctx, void const*applicationData) noexcept { + if (applicationData) { + ctx.applicationData.emplace(); + memcpy(&ctx.applicationData.value(), applicationData, sizeof(Wrap)); + } else { + ctx.applicationData.reset(); + } } -void *applicationDataRaw(Context &ctx) noexcept { - return ctx.applicationData; +WrapBase *applicationDataRaw(Context &ctx) noexcept { + if (!ctx.applicationData) { + return nullptr; + } + return std::bit_cast(&*ctx.applicationData); } } diff --git a/src/olympic/turbine/src/gba/context.hpp b/src/olympic/turbine/src/gba/context.hpp index 64a1caaa..1dda711e 100644 --- a/src/olympic/turbine/src/gba/context.hpp +++ b/src/olympic/turbine/src/gba/context.hpp @@ -17,7 +17,7 @@ class Context { UpdateHandler updateHandler = [](Context&) -> int {return 0;}; keel::Context keelCtx; KeyEventHandler keyEventHandler = nullptr; - void *applicationData = nullptr; + ox::Optional> applicationData; // GBA impl data ///////////////////////////////////////////////////////// bool running = true; diff --git a/src/olympic/turbine/src/glfw/context.cpp b/src/olympic/turbine/src/glfw/context.cpp index ded53880..ea051dd9 100644 --- a/src/olympic/turbine/src/glfw/context.cpp +++ b/src/olympic/turbine/src/glfw/context.cpp @@ -20,12 +20,20 @@ keel::Context &keelCtx(Context &ctx) noexcept { return ctx.keelCtx; } -void setApplicationData(Context &ctx, void *applicationData) noexcept { - ctx.applicationData = applicationData; +void setApplicationDataRaw(Context &ctx, void *applicationData) noexcept { + if (applicationData) { + ctx.applicationData.emplace(); + memcpy(&ctx.applicationData.value(), applicationData, sizeof(Wrap)); + } else { + ctx.applicationData.reset(); + } } -void *applicationDataRaw(Context &ctx) noexcept { - return ctx.applicationData; +WrapBase *applicationDataRaw(Context &ctx) noexcept { + if (!ctx.applicationData) { + return nullptr; + } + return std::bit_cast(&*ctx.applicationData); } } diff --git a/src/olympic/turbine/src/glfw/context.hpp b/src/olympic/turbine/src/glfw/context.hpp index 6075cf8e..19716242 100644 --- a/src/olympic/turbine/src/glfw/context.hpp +++ b/src/olympic/turbine/src/glfw/context.hpp @@ -16,7 +16,7 @@ class Context { UpdateHandler updateHandler = [](Context&) -> int {return 0;}; keel::Context keelCtx; KeyEventHandler keyEventHandler = nullptr; - void *applicationData = nullptr; + ox::Optional> applicationData; // GLFW impl data //////////////////////////////////////////////////////// int uninterruptedRefreshes = 3;