ReXGlue SDK
Loading...
Searching...
No Matches
app.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <exception>
14#include <filesystem>
15#include <memory>
16#include <span>
17#include <string>
18#include <string_view>
19#include <utility>
20#include <vector>
21
22#include <rex/cli.h>
23#include <rex/platform.h>
25#include <rex/app_info.h>
26#include <rex/module.h>
27#include <rex/result.h>
28#include <rex/setting.h>
30#include <rex/types.h>
31
32#if REX_PLATFORM_WIN32
33#include <windows.h>
34#endif
35
36namespace rex {
37
38namespace gpu {
39class IGraphicsSystem;
40} // namespace gpu
41namespace apu {
42class IAudioSystem;
43} // namespace apu
44namespace hid {
45class IInputSystem;
46} // namespace hid
47namespace ui {
48class IUISystem;
49} // namespace ui
50
51} // namespace rex
52
53namespace rex::app {
54
68
74 public:
75 REX_SETTING(std::string, name, "", "App name, set by the constructor", kStartupOnly, kNoSave);
76 REX_SETTING(std::filesystem::path, root, "",
77 "Folder the app keeps its files in, the folder of the executable when empty",
78 kStartupOnly);
79 REX_SETTING(std::filesystem::path, log_root, "{app.root}/logs", "Log folder", kStartupOnly);
80 REX_SETTING(std::string, single_instance, "",
81 "Lock name only one running copy may hold, empty for none", kStartupOnly);
82
86 explicit App(std::string_view name);
87 ~App() override;
88
89 App(const App&) = delete;
90 App& operator=(const App&) = delete;
91
92 std::string_view GetName() const;
93
94 AppPhase GetPhase() const;
95
99 std::span<const std::string> GetArgs() const;
100
103
104 rex::gpu::IGraphicsSystem& GetGraphics();
105
106 rex::apu::IAudioSystem& GetAudio();
107
108 rex::ui::IUISystem& GetUI();
109
114
120
132 [[nodiscard]] Result<void> Init(const CLIArgList& args);
133
140 bool RunFrame();
141
143 void Run();
144
151 int Shutdown();
152
153 protected:
159
167 [[nodiscard]] virtual Result<void> OnConfigure(SettingsRegistry& settings);
168
172 virtual std::span<const CLIAction> GetActions() const;
173
181 [[nodiscard]] virtual Result<void> OnAction(const CLIActionHit& hit);
182
189 virtual void OnModules(std::vector<std::string>& modules);
190
196 virtual void OnRegisterModules(std::vector<IModule*>& modules);
197
203 [[nodiscard]] virtual Result<void> OnInit();
204
206 virtual void OnRun();
207
209 virtual void OnShutdown();
210
212 virtual void ReportError(std::string_view message);
213
216
222
228
235 [[nodiscard]] Result<void> Fail(std::string_view step, Error error);
236
238 virtual void OnStop();
239
243 virtual void OnRelease();
244
247
249 void SetPhase(AppPhase phase);
250
251 private:
252 [[nodiscard]] Result<void> ReportUnknownSettings();
253
254 struct State;
255 std::unique_ptr<State> state_;
256};
257
263REX_APP_API void RequestShutdown(i32 exit_code);
264
271REX_APP_API void RequestRestart(std::vector<std::string> args = {});
272
279
280} // namespace rex::app
281
296#define REX_CONSOLE_APP(AppClass) \
297 static int ReXConsoleEntry(const ::rex::CLIArgList& args) { \
298 AppClass rex_app; \
299 if (rex_app.Init(args)) { \
300 rex_app.Run(); \
301 } \
302 return rex_app.Shutdown(); \
303 } \
304 REX_DETAIL_ENTRY_POINTS(ReXConsoleEntry)
305
322#define REX_CONSOLE_APP_MAIN(AppClass, Function) \
323 static int ReXConsoleEntry(const ::rex::CLIArgList& args) { \
324 AppClass rex_app; \
325 return Function(rex_app, args); \
326 } \
327 REX_DETAIL_ENTRY_POINTS(ReXConsoleEntry)
328
329namespace rex::app {
330
331namespace detail {
332
338REX_APP_API int ReportEntryFailure(const char* what) noexcept;
339
346template <typename Fn>
347int RunGuarded(Fn&& fn) noexcept {
348 try {
349 return std::forward<Fn>(fn)();
350 } catch (const std::exception& error) {
351 return ReportEntryFailure(error.what());
352 } catch (...) {
353 return ReportEntryFailure("unknown exception");
354 }
355}
356
357#if REX_PLATFORM_WIN32
362REX_APP_API CLIArgList GetWindowsCommandLine();
363#endif
364
365} // namespace detail
366
367} // namespace rex::app
368
369#if REX_PLATFORM_WIN32
379#define REX_DETAIL_ENTRY_POINTS(fn) \
380 int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) { \
381 return ::rex::app::detail::RunGuarded( \
382 [] { return fn(::rex::app::detail::GetWindowsCommandLine()); }); \
383 } \
384 int main(int argc, char** argv) { \
385 return ::rex::app::detail::RunGuarded( \
386 [&] { return fn(::rex::CLIArgList::Parse(argc, argv)); }); \
387 }
388#else
389#define REX_DETAIL_ENTRY_POINTS(fn) \
390 int main(int argc, char** argv) { \
391 return ::rex::app::detail::RunGuarded( \
392 [&] { return fn(::rex::CLIArgList::Parse(argc, argv)); }); \
393 }
394#endif
REX_APP_API void RequestRestart(std::vector< std::string > args={})
REX_APP_API bool IsShutdownRequested()
AppPhase
Definition app.h:59
@ kStart
Definition app.h:60
@ kRunning
Definition app.h:62
@ kTitleStop
Definition app.h:65
@ kTitleLaunch
Definition app.h:63
@ kShutdown
Definition app.h:66
@ kProviders
Definition app.h:61
REX_APP_API void RequestShutdown(i32 exit_code)
Definition cli.h:36
Definition module.h:131
SettingOwner(std::string_view scope)
void RequestRestart()
App(std::string_view name)
bool IsHeadlessRequested() const
virtual Result< void > OnConfigure(SettingsRegistry &settings)
virtual Result< void > OnAction(const CLIActionHit &hit)
std::span< const std::string > GetArgs() const
virtual void OnRegisterModules(std::vector< IModule * > &modules)
virtual void ReportError(std::string_view message)
virtual void OnModules(std::vector< std::string > &modules)
rex::ui::IUISystem * FindUI()
void RequestShutdown()
virtual void OnShutdown()
virtual void OnRelease()
virtual std::span< const CLIAction > GetActions() const
IInterfaceFactory & GetControlFactory()
void SetPhase(AppPhase phase)
Result< void > Init(const CLIArgList &args)
Result< void > Fail(std::string_view step, Error error)
virtual Result< void > OnInit()
virtual void OnRun()
virtual platform::CrashReporterOptions GetCrashOptions()
rex::hid::IInputSystem & GetInput()
virtual void OnStop()
virtual AppSysRequirements GetRequirements() const
Definition audio_system.h:64
Definition graphics_system.h:54
Definition input_system.h:54
Definition ui_system.h:154
Interfaces, factory and module declaration.
Definition app.h:41
Definition app.h:38
Definition app.h:36
std::expected< T, Error > Result
Definition result.h:98
Platform flags, packing macros and export markers.
#define REX_APP_API
Definition platform.h:279
Process control, console, environment and crash reports.
Typed settings and the owners that declare them.
The registry of every registered setting.
Definition app_info.h:20
Definition cli.h:93
Definition result.h:43
Definition process.h:200
Integer types, byte order wrappers and size literals.