Repository navigation
Expand file tree
/
Copy pathframe.hpp
More file actions
342 lines (275 loc) · 7.14 KB
/
Copy pathframe.hpp
File metadata and controls
342 lines (275 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @file liblava/frame/frame.hpp
* @brief Framework
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/base/device.hpp"
#include "liblava/base/instance.hpp"
#include "liblava/base/platform.hpp"
#include "liblava/core/time.hpp"
#include "liblava/frame/argh.hpp"
#include "liblava/util/log.hpp"
#include "liblava/util/telegram.hpp"
namespace lava {
/**
* @brief Framework environment
*/
struct frame_env {
/// Reference to frame environment
using ref = frame_env const&;
/**
* @brief Construct a new frame environment
*/
explicit frame_env() {
set_default();
}
/**
* @brief Construct a new frame environment
* @param app_name Name of application
* @param cmd_line Command line arguments
*/
explicit frame_env(name app_name,
argh::parser cmd_line)
: cmd_line(cmd_line) {
info.app_name = app_name;
set_default();
}
/**
* @brief Set default settings
*/
void set_default();
/// Command line arguments
argh::parser cmd_line;
/// Logging configuration
log::config log;
/// Instance information
instance_info info;
/// Instance create parameters
instance::create_param param;
/// Intance debug configuration
instance::debug_config debug;
/// Message dispatcher threads
ui32 telegraph_thread_count = 4;
};
/**
* @brief Error codes
*/
enum error {
not_ready = -1,
create_failed = -2,
init_failed = -3,
load_failed = -4,
run_aborted = -5,
still_running = -6,
program_failed = -7,
};
/**
* @brief Get the current time
* @return ms Current milliseconds
*/
ms now();
/// Run abort result
constexpr bool const run_abort = false;
/// Run continue result
constexpr bool const run_continue = true;
/**
* @brief Framework
*/
struct frame : interface, no_copy_no_move {
/// Shared pointer to framework
using s_ptr = std::shared_ptr<frame>;
/**
* @brief Construct a new framework
* @param cmd_line Command line arguments
*/
explicit frame(argh::parser cmd_line);
/**
* @brief Construct a new framework
* @param env Framework environment
*/
explicit frame(frame_env env);
/**
* @brief Destroy the framework
*/
~frame() override;
/**
* @brief Check if framework is ready
* @return Framework is ready or not
*/
bool ready() const {
return m_initialized;
}
/// Framework result
using result = i32; // error < 0
/**
* @brief Run the framework
* @return result Run result
*/
result run();
/**
* @brief Shut down the framework
* @return Shut down was successful or failed
*/
bool shut_down();
/// Run function
using run_func = std::function<bool(id::ref)>;
/// Reference to run function
using run_func_ref = run_func const&;
/**
* @brief Add run to framework
* @param func Run function
* @return id Id of function
*/
id add_run(run_func_ref func);
/// Run end function
using run_end_func = std::function<void()>;
/// Reference to run end function
using run_end_func_ref = run_end_func const&;
/**
* @brief Add run end to framework
* @param func Run end function
* @return id Id of function
*/
id add_run_end(run_end_func_ref func);
/// Run once function
using run_once_func = std::function<bool()>;
/// Reference to run once function
using run_once_func_ref = run_once_func const&;
/**
* @brief Add run once to framework
* @param func Run once function
*/
void add_run_once(run_once_func_ref func) {
m_run_once_list.push_back(func);
}
/**
* @brief Remove a function from framework
* @param func_id Id of function
* @return Remove was successful or failed
*/
bool remove(id::ref func_id);
/**
* @brief Get the running time
* @return ms Time since start of framework
*/
ms get_running_time() const {
return now() - m_start_time;
}
/**
* @brief Get the running time in seconds
* @return r64 Time since start of framework
*/
r64 get_running_time_sec() const {
return to_sec(get_running_time());
}
/**
* @brief Get the command line arguments
* @return cmd_line Command line arguments
*/
cmd_line get_cmd_line() const {
return m_env.cmd_line;
}
/**
* @brief Get the framework environment
* @return frame_env::ref Framework environment
*/
frame_env::ref get_env() const {
return m_env;
}
/**
* @brief Get the name of application
* @return name Name of application
*/
name get_name() const {
return m_env.info.app_name;
}
/**
* @brief Check if framework is waiting for events
* @return Framework waits for events or not
*/
bool waiting_for_events() const {
return m_wait_for_events;
}
/**
* @brief Set wait for events in framework
* @param value Wait for events state
*/
void set_wait_for_events(bool value = true) {
m_wait_for_events = value;
}
/// Run time
lava::run_time run_time;
/// Stage platform
lava::platform platform;
/// Message dispatcher
message_dispatcher telegraph;
private:
/**
* @brief Set up the framework
* @return Setup was successful or failed
*/
bool setup();
/**
* @brief Tear down the framework
*/
void teardown();
/**
* @brief Run a step
* @return Run was successful or failed
*/
bool run_step();
/**
* @brief Trigger run remove
*/
void trigger_run_remove();
/**
* @brief Trigger run end
*/
void trigger_run_end();
/// Initialized state
bool m_initialized = false;
/// Framework environment
frame_env m_env;
/// Running state
bool m_running = false;
/// Wait for events state
bool m_wait_for_events = false;
/// Framework start time
ms m_start_time;
/// Map of run functions
using run_func_map = std::map<id, run_func>;
/// Map of run functions
run_func_map m_run_map;
/// Map fo run end functions
using run_end_func_map = std::map<id, run_end_func>;
/// Map fo run end functions
run_end_func_map m_run_end_map;
/// Map of run once functions
using run_once_func_list = std::vector<run_once_func>;
/// Map of run once functions
run_once_func_list m_run_once_list;
/// List of run ids to remove
id::list m_run_remove_list;
};
/**
* @brief Handle events
* @param wait Wait for events
*/
void handle_events(bool wait = false);
/**
* @brief Handle events with timeout
* @param timeout Wait timeout in milliseconds
*/
void handle_events_timeout(ms timeout);
/**
* @brief Handle events with timeout
* @param timeout Wait timeout in seconds
*/
void handle_events_timeout(seconds timeout);
/**
* @brief Post an empty event
*/
void post_empty_event();
} // namespace lava