[turbine] Add some type safety to application data
All checks were successful
Build / build (push) Successful in 2m34s

This commit is contained in:
Gary Talent 2024-03-16 18:11:14 -05:00
parent 05d08a7687
commit aa43cb3d8d
5 changed files with 51 additions and 13 deletions

View File

@ -37,15 +37,37 @@ inline ox::FileSystem *rom(Context &ctx) noexcept {
return keelCtx(ctx).rom.get(); return keelCtx(ctx).rom.get();
} }
void setApplicationData(Context &ctx, void *applicationData) noexcept; struct WrapBase {
virtual ~WrapBase() = default;
};
template<typename T>
struct Wrap: public WrapBase {
T *data{};
};
void setApplicationDataRaw(Context &ctx, void *applicationData) noexcept;
template<typename T>
void setApplicationData(Context &ctx, T *applicationData) noexcept {
Wrap<T> w;
w.data = applicationData;
setApplicationDataRaw(ctx, &w);
}
[[nodiscard]] [[nodiscard]]
void *applicationDataRaw(Context &ctx) noexcept; WrapBase *applicationDataRaw(Context &ctx) noexcept;
template<typename T> template<typename T>
[[nodiscard]] [[nodiscard]]
T *applicationData(Context &ctx) noexcept { T *applicationData(Context &ctx) noexcept {
return static_cast<T*>(applicationDataRaw(ctx)); #ifdef OX_BARE_METAL
return static_cast<Wrap<T>*>(applicationDataRaw(ctx))->data;
#else
auto const out = dynamic_cast<Wrap<T>*>(applicationDataRaw(ctx));
oxAssert(out, "Cast failed - wrong type");
return out->data;
#endif
} }
void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept; void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept;

View File

@ -18,12 +18,20 @@ keel::Context &keelCtx(Context &ctx) noexcept {
return ctx.keelCtx; return ctx.keelCtx;
} }
void setApplicationData(Context &ctx, void *applicationData) noexcept { void setApplicationDataRaw(Context &ctx, void const*applicationData) noexcept {
ctx.applicationData = applicationData; if (applicationData) {
ctx.applicationData.emplace();
memcpy(&ctx.applicationData.value(), applicationData, sizeof(Wrap<void>));
} else {
ctx.applicationData.reset();
}
} }
void *applicationDataRaw(Context &ctx) noexcept { WrapBase *applicationDataRaw(Context &ctx) noexcept {
return ctx.applicationData; if (!ctx.applicationData) {
return nullptr;
}
return std::bit_cast<WrapBase*>(&*ctx.applicationData);
} }
} }

View File

@ -17,7 +17,7 @@ class Context {
UpdateHandler updateHandler = [](Context&) -> int {return 0;}; UpdateHandler updateHandler = [](Context&) -> int {return 0;};
keel::Context keelCtx; keel::Context keelCtx;
KeyEventHandler keyEventHandler = nullptr; KeyEventHandler keyEventHandler = nullptr;
void *applicationData = nullptr; ox::Optional<std::array<char, sizeof(WrapBase) + sizeof(uintptr_t)>> applicationData;
// GBA impl data ///////////////////////////////////////////////////////// // GBA impl data /////////////////////////////////////////////////////////
bool running = true; bool running = true;

View File

@ -20,12 +20,20 @@ keel::Context &keelCtx(Context &ctx) noexcept {
return ctx.keelCtx; return ctx.keelCtx;
} }
void setApplicationData(Context &ctx, void *applicationData) noexcept { void setApplicationDataRaw(Context &ctx, void *applicationData) noexcept {
ctx.applicationData = applicationData; if (applicationData) {
ctx.applicationData.emplace();
memcpy(&ctx.applicationData.value(), applicationData, sizeof(Wrap<void>));
} else {
ctx.applicationData.reset();
}
} }
void *applicationDataRaw(Context &ctx) noexcept { WrapBase *applicationDataRaw(Context &ctx) noexcept {
return ctx.applicationData; if (!ctx.applicationData) {
return nullptr;
}
return std::bit_cast<WrapBase*>(&*ctx.applicationData);
} }
} }

View File

@ -16,7 +16,7 @@ class Context {
UpdateHandler updateHandler = [](Context&) -> int {return 0;}; UpdateHandler updateHandler = [](Context&) -> int {return 0;};
keel::Context keelCtx; keel::Context keelCtx;
KeyEventHandler keyEventHandler = nullptr; KeyEventHandler keyEventHandler = nullptr;
void *applicationData = nullptr; ox::Optional<std::array<char, sizeof(WrapBase) + sizeof(uintptr_t)>> applicationData;
// GLFW impl data //////////////////////////////////////////////////////// // GLFW impl data ////////////////////////////////////////////////////////
int uninterruptedRefreshes = 3; int uninterruptedRefreshes = 3;