nostalgia/src/turbine/context.hpp

103 lines
2.4 KiB
C++

/*
* Copyright 2016 - 2023 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/fs/fs.hpp>
#include <ox/model/desctypes.hpp>
#include <ox/std/buffer.hpp>
#include <ox/std/size.hpp>
#include <keel/context.hpp>
#include "event.hpp"
#include "input.hpp"
namespace turbine {
class BaseClipboardObject {
public:
virtual ~BaseClipboardObject() noexcept = default;
[[nodiscard]]
virtual ox::String typeId() const noexcept = 0;
[[nodiscard]]
constexpr auto typeMatch(auto name, auto version) const noexcept {
return typeId() == ox::buildTypeId(name, version);
}
};
template<typename T>
class ClipboardObject: public BaseClipboardObject {
[[nodiscard]]
ox::String typeId() const noexcept final {
return ox::buildTypeId(T::TypeName, T::TypeVersion);
}
};
void shutdown(Context &ctx) noexcept;
// User Input Output
class Context {
friend constexpr void setApplicationData(Context &ctx, void *applicationData) noexcept;
template<typename T>
friend constexpr T *applicationData(Context &ctx) noexcept;
friend void setUpdateHandler(Context &ctx, UpdateHandler h) noexcept;
friend constexpr void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept;
friend constexpr KeyEventHandler keyEventHandler(Context &ctx) noexcept;
public:
UpdateHandler updateHandler = [](Context&) -> int {return 0;};
ox::UPtr<BaseClipboardObject> clipboard;
keel::Context keelCtx;
protected:
KeyEventHandler m_keyEventHandler = nullptr;
void *m_applicationData = nullptr;
Context() noexcept = default;
Context(Context &other) noexcept = delete;
Context(const Context &other) noexcept = delete;
Context(const Context &&other) noexcept = delete;
public:
virtual inline ~Context() noexcept {
shutdown(*this);
}
};
constexpr auto *rom(Context &ctx) noexcept {
return ctx.keelCtx.rom.get();
}
constexpr const auto *rom(const Context &ctx) noexcept {
return ctx.keelCtx.rom.get();
}
constexpr void setApplicationData(Context &ctx, void *applicationData) noexcept {
ctx.m_applicationData = applicationData;
}
template<typename T>
[[nodiscard]]
constexpr T *applicationData(Context &ctx) noexcept {
return static_cast<T*>(ctx.m_applicationData);
}
constexpr void setKeyEventHandler(Context &ctx, KeyEventHandler h) noexcept {
ctx.m_keyEventHandler = h;
}
constexpr KeyEventHandler keyEventHandler(Context &ctx) noexcept {
return ctx.m_keyEventHandler;
}
void setConstantRefresh(Context &ctx, bool r) noexcept;
}