-
Notifications
You must be signed in to change notification settings - Fork 0
/
participant.hpp
360 lines (281 loc) · 12.7 KB
/
participant.hpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// Created by Johannes on 28.05.2020.
//
#pragma once
#ifndef HAR_PARTICIPANT_HPP
#define HAR_PARTICIPANT_HPP
#include <har/coords.hpp>
#include <har/full_cell.hpp>
#include <har/part.hpp>
namespace har {
class inner_participant;
static constexpr struct {
constexpr participant_h operator[](const size_t n) const {
return static_cast<participant_h>(n);
}
[[nodiscard]]
constexpr participant_h no_one() const { //NOLINT
return ~participant_h();
}
} PARTICIPANT = { };
enum request_type {
PROGRAM = 0,
UI = 1
};
///Participants are used for interaction with HAR simulations.
class participant {
public:
class context;
using callback_t = std::function<void(participant::context &)>;
private:
inner_participant * _iparti; ///<Pointer to inner participant (if attached)
bool_t _destroying; ///<Whether the participant has begun detaching itself from a simulation
protected:
/// \brief Default constructor
participant();
/// \brief Requests operation on the simulation
///
/// \return A context object
context request(request_type req_type = request_type::PROGRAM);
void exec(participant::callback_t && fun);
/// \brief Starts the automaton
void start();
/// \brief Let's the automaton run for a single cycle
void step();
/// \brief Stops the automaton
void stop();
/// \brief Sets a grid cell as <tt>selected</tt> in the UI
///
/// \param [in] hnd Handle of the cell
///
/// All changes on the selected cell will be reported to the participant via callbacks
void select(const cell_h & hnd);
/// \brief Attempts to include a new part into the simulation
///
/// \param [in] pt The part to include
void include_part(const part & pt);
/// \brief Loads a model from a string
///
/// \param [in] ser A string that contains a serialized HAR model
void load_model(string_t & ser);
/// \brief Loads a model from a string
///
/// \param [in] ser A string that contains a serialized HAR model
void load_model(string_t && ser);
/// \brief Loads a model from a string
///
/// \param [in] is An input stream that supplies a serialized HAR model
void load_model(istream & is);
/// \brief Serializes the simulation's current model into a string
///
/// \param [out] ser The target for the serialized model
void store_model(string_t & ser);
/// \brief Serializes the simulation's current model into an output stream
///
/// \param [out] os The target for the serialized model
void store_model(ostream & os);
/// \brief Schedules a redraw of all cells in the current model
void redraw_all();
/// \brief Checks, if the participant is attached to a simulation
[[nodiscard]]
bool_t attached() const;
/// \brief Detaches this participant from the simulation
void detach();
/// \brief Prompts the simulation process to exit gracefully
void exit();
public:
/// \brief Called by the simulation to attach this participant to the simulation
///
/// \param [in,out] iparti Reference to the corresponding inner participant
/// \param [in] argc Number of command line arguments
/// \param [in] argv Command line arguments
/// \param [in] envp Environment variables
void attach(inner_participant & iparti, int argc, char * argv[], char * envp[]);
/// \brief Called by the simulation to detach this participant from the simulation
///
/// \param [in,out] iparti Reference to the corresponding inner participant
void detach(inner_participant & iparti);
/// \brief Identifies a participant by name
///
/// \return The name of the participant
///
/// The name returned by this function is used in messages in a simulation to identify a participant
[[nodiscard]]
virtual std::string name() const = 0;
/// \brief Retrieves a reference to the image of a participant displaying a cell
///
/// \param [in] hnd Handle of the cell
///
/// \return The participant's corresponding image
[[nodiscard]]
virtual image_t get_image_base(const cell_h & hnd) = 0;
/// \brief Processes an image painted by a parts <tt>draw</tt> delegate for display in the participant
///
/// \param [in] hnd Handle of the cell
/// \param [in] img The cell's corresponding image
///
/// \return The processed image
virtual har::image_t process_image(har::cell_h hnd, har::image_t & img) = 0;
/// \brief Retrieves the input stream of the participant
///
/// \return Input stream of the participant
virtual istream & input() = 0;
/// \brief Retrieves the output stream of the participant
///
/// \return Output stream of the participant
virtual ostream & output() = 0;
/// \brief Called once every cycle
virtual void on_cycle(participant::context & ctx) = 0;
/// \brief Called once, when the participant is added to the simulation
/// This callback is guaranteed to be called before any other callback
///
/// \param [in] argc Number of command line arguments
/// \param [in] argv Array of command line arguments
/// \param [in] envp Array of defined environment variables
virtual void on_attach(int argc, char * const argv[], char * const envp[]) = 0;
/// \brief Called every time a new part is added to the simulation
///
/// \param [in] pt Added part
/// \param [in] commit <tt>false</tt>, if more added parts will be reported subsequently, otherwise <tt>true</tt>
virtual void on_part_included(const part & pt, bool_t commit) = 0;
inline void on_part_included(const part & pt) {
on_part_included(pt, true);
}
/// \brief Called every time a part is removed from the simulation
///
/// \param [in] id ID of the removed part
virtual void on_part_removed(part_h id) = 0;
/// \brief Called every time a grid in the simulation is resized
///
/// \param [in] to ID of the grid and its new size
virtual void on_resize_grid(const gcoords_t & to) = 0;
/// \brief Called every time a model is loaded
virtual void on_model_loaded() = 0;
/// \brief Called every time the information about the model is changed
///
/// \param [in] info Updated model info
virtual void on_info_updated(const model_info & info) = 0;
/// \brief Called every time the automaton's state is set to <tt>RUN</tt>
///
/// \param [in] responsible <tt>true</tt>, if the called participant is responsible for cycling the automaton
virtual void on_run(bool_t responsible) = 0;
inline void on_run() {
on_run(false);
}
/// \brief Called every time the automaton's state is set to <tt>STEP</tt>
virtual void on_step() = 0;
/// \brief Called every time the automaton's state is set to <tt>STOP</tt>
virtual void on_stop() = 0;
/// \brief Called every time a message to the user is to be displayed
///
/// \param [in] header Header of the message
/// \param [in] content Content of the message
virtual void on_message(const string_t & header, const string_t & content) = 0;
/// \brief Called every time an exception occurs to report it to the user
///
/// \param [in] e Reported exception
virtual void on_exception(const exception::exception & e) = 0;
/// \brief Called every time a property of the selected cell is updated
///
/// \param [in] hnd Handle of the selected cell
/// \param [in] id ID of the property
/// \param [in] val New value
virtual void on_selection_update(const cell_h & hnd, entry_h id, const value & val, bool_t commit) = 0;
inline void on_selection_update(const cell_h & hnd, entry_h id, const value & val) {
on_selection_update(hnd, id, val, true);
}
/// \brief Called every time a cell was redrawn
///
/// \param [in] hnd Handle of the cell
/// \param [in] img
virtual void on_redraw(const cell_h & hnd, image_t && img, bool_t commit) = 0;
inline void on_redraw(const cell_h & hnd, image_t && img) {
on_redraw(hnd, std::forward<image_t>(img), true);
}
/// \brief Called when a connection is added to a grid cell
///
/// \param [in] from Position of the observing end of the connection
/// \param [in] to Position of the observed end of the connection
/// \param [in] use Observer's ID for the connection
virtual void on_connection_added(const gcoords_t & from, const gcoords_t & to, direction_t use) = 0;
/// \brief Called when a connection is removed from a grid cell
///
/// \param [in] from Position of the observing end of the connection
/// \param [in] use Observer's ID for the connection
virtual void on_connection_removed(const gcoords_t & from, direction_t use) = 0;
/// \brief Called every time a new cargo is spawned
///
/// \param [in] num ID of the cargo
virtual void on_cargo_spawned(cargo_h num) = 0;
/// \brief Called every time a new cargo is moved
///
/// \param [in] num ID of the cargo
/// \param [in] to New position of the cargo
virtual void on_cargo_moved(cargo_h num, ccoords_t to) = 0;
/// \brief Called every time a new cargo is destroyed
///
/// \param [in] num ID of the cargo
virtual void on_cargo_destroyed(cargo_h num) = 0;
///
virtual void on_commit() = 0;
/// \brief Called once, when the participant is detached from the simulation
/// After this, no other callbacks will be called
virtual void on_detach() = 0;
/// \brief Default destructor
virtual ~participant();
};
/// \brief Class for participants to operate on the simulation
///
/// For the lifetime of an object of this class, all other threads of the associated simulation are halted
class participant::context {
private:
std::reference_wrapper<inner_participant> _parti; ///<Reference to the creating participant's inner pendant
request_type _type;
bool_t _blocking; ///<Whether this context blocks the simulation
public:
/// \brief Constructor
///
/// \param [in,out] parti Corresponding inner participant
///
/// Waits for the automaton to yield
explicit context(inner_participant & parti, request_type type, bool_t blocking = true);
context(const context & ref) = delete;
context(context && fref) noexcept = delete;
/// \brief Retrieves a cell from the simulation's grid
///
/// \param [in] pos Position of the cell
///
/// \return The requested grid cell
full_grid_cell at(const gcoords_t & pos);
/// \brief Retrieves a cell of the simulation's cargo
///
/// \param [in] num Number of the cargo
///
/// \return The requested cargo cell
full_cargo_cell at(cargo_h num);
/// \brief Resizes one of the simulation's grids
///
/// \param [in] to Grid number and new size
void resize_grid(const gcoords_t & to);
/// \brief Cycles the simulation for one step
void cycle();
/// \brief Commits all changes captured by this context before it's destruction
///
/// This function has the same effects as the destruction of this object, without destroying it
void commit();
context & operator=(const context & ref) = delete;
context & operator=(context && fref) noexcept = delete;
/// \brief Default destructor
///
/// Unlocks the automaton of the simulation
~context();
};
}
#if defined(HAR_ENABLE_REQUEST_MACROS)
#define THISREQUEST(CTX) if (auto CTX = request(); true)
#define OBJREQUEST(CTX, OBJ) if (auto CTX = (OBJ).request(); true)
#define TYPEOBJREQUEST(CTX, OBJ, TYPE) if (auto CTX = (OBJ).request(TYPE); true)
#define GET_REQUEST_INNER(_1, _2, _3, NAME, ...) NAME
#define REQUEST(...) GET_REQUEST_INNER(__VA_ARGS__, TYPEOBJREQUEST, OBJREQUEST, THISREQUEST)(__VA_ARGS__)
#endif
#endif //HAR_PARTICIPANT_HPP