[turbine] Replace WrapPtr with ox::AnyPtr
All checks were successful
Build / build (push) Successful in 2m33s
All checks were successful
Build / build (push) Successful in 2m33s
This commit is contained in:
parent
3fa247e3d4
commit
1616ca7018
@ -37,78 +37,21 @@ inline ox::FileSystem *rom(Context &ctx) noexcept {
|
||||
return keelCtx(ctx).rom.get();
|
||||
}
|
||||
|
||||
struct WrapBase {
|
||||
virtual ~WrapBase() = default;
|
||||
virtual WrapBase *copyTo(ox::Span<char> s) noexcept = 0;
|
||||
virtual operator bool() const noexcept = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Wrap: public WrapBase {
|
||||
T *data{};
|
||||
Wrap(T *pData) noexcept: data(pData) {
|
||||
}
|
||||
WrapBase *copyTo(ox::Span<char> s) noexcept override {
|
||||
oxAssert(s.size() >= sizeof(Wrap), "too small buffer");
|
||||
return new(s.data()) Wrap{data};
|
||||
}
|
||||
operator bool() const noexcept override {
|
||||
return data != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
class WrapPtr {
|
||||
private:
|
||||
WrapBase *m_wrapPtr{};
|
||||
ox::Array<char, sizeof(Wrap<void*>)> m_wrapData;
|
||||
public:
|
||||
template<typename T>
|
||||
inline WrapPtr &operator=(T *ptr) noexcept {
|
||||
m_wrapPtr = new(m_wrapData.data()) Wrap(ptr);
|
||||
return *this;
|
||||
}
|
||||
inline WrapPtr &operator=(WrapBase &ptr) noexcept {
|
||||
if (ptr) {
|
||||
m_wrapPtr = ptr.copyTo(m_wrapData);
|
||||
} else {
|
||||
m_wrapPtr = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
inline operator bool() const noexcept {
|
||||
return m_wrapPtr && *m_wrapPtr;
|
||||
}
|
||||
[[nodiscard]]
|
||||
inline WrapBase *getWrapBase() const noexcept {
|
||||
return m_wrapPtr;
|
||||
}
|
||||
};
|
||||
|
||||
void setApplicationDataRaw(Context &ctx, WrapBase &applicationData) noexcept;
|
||||
void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexcept;
|
||||
|
||||
template<typename T>
|
||||
void setApplicationData(Context &ctx, T *applicationData) noexcept {
|
||||
Wrap w(applicationData);
|
||||
setApplicationDataRaw(ctx, w);
|
||||
setApplicationDataRaw(ctx, applicationData);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
WrapBase *applicationDataRaw(Context &ctx) noexcept;
|
||||
ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept;
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]]
|
||||
T *applicationData(Context &ctx) noexcept {
|
||||
auto const raw = applicationDataRaw(ctx);
|
||||
if (!raw) [[unlikely]] {
|
||||
return nullptr;
|
||||
}
|
||||
#ifdef OX_BARE_METAL
|
||||
auto const out = static_cast<Wrap<T>*>(raw);
|
||||
#else
|
||||
auto const out = dynamic_cast<Wrap<T>*>(raw);
|
||||
#endif
|
||||
oxAssert(out, "Cast failed - wrong type");
|
||||
return out->data;
|
||||
auto const ptr = applicationDataRaw(ctx);
|
||||
return ptr.get<T>();
|
||||
}
|
||||
|
||||
void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept;
|
||||
|
@ -18,12 +18,12 @@ keel::Context &keelCtx(Context &ctx) noexcept {
|
||||
return ctx.keelCtx;
|
||||
}
|
||||
|
||||
void setApplicationDataRaw(Context &ctx, WrapBase &applicationData) noexcept {
|
||||
void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexcept {
|
||||
ctx.applicationData = applicationData;
|
||||
}
|
||||
|
||||
WrapBase *applicationDataRaw(Context &ctx) noexcept {
|
||||
return ctx.applicationData.getWrapBase();
|
||||
ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept {
|
||||
return ctx.applicationData;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ class Context {
|
||||
UpdateHandler updateHandler = [](Context&) -> int {return 0;};
|
||||
keel::Context keelCtx;
|
||||
KeyEventHandler keyEventHandler = nullptr;
|
||||
WrapPtr applicationData;
|
||||
ox::AnyPtr applicationData;
|
||||
|
||||
// GBA impl data /////////////////////////////////////////////////////////
|
||||
bool running = true;
|
||||
|
@ -20,12 +20,12 @@ keel::Context &keelCtx(Context &ctx) noexcept {
|
||||
return ctx.keelCtx;
|
||||
}
|
||||
|
||||
void setApplicationDataRaw(Context &ctx, WrapBase &applicationData) noexcept {
|
||||
void setApplicationDataRaw(Context &ctx, ox::AnyPtr const&applicationData) noexcept {
|
||||
ctx.applicationData = applicationData;
|
||||
}
|
||||
|
||||
WrapBase *applicationDataRaw(Context &ctx) noexcept {
|
||||
return ctx.applicationData.getWrapBase();
|
||||
ox::AnyPtr &applicationDataRaw(Context &ctx) noexcept {
|
||||
return ctx.applicationData;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ class Context {
|
||||
UpdateHandler updateHandler = [](Context&) -> int {return 0;};
|
||||
keel::Context keelCtx;
|
||||
KeyEventHandler keyEventHandler = nullptr;
|
||||
WrapPtr applicationData;
|
||||
ox::AnyPtr applicationData;
|
||||
|
||||
// GLFW impl data ////////////////////////////////////////////////////////
|
||||
int uninterruptedRefreshes = 3;
|
||||
|
Loading…
Reference in New Issue
Block a user