-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.cpp
More file actions
486 lines (351 loc) · 12.3 KB
/
state.cpp
File metadata and controls
486 lines (351 loc) · 12.3 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright (c) January 2026 Félix-Olivier Dumas. All rights reserved.
// Licensed under the terms described in the LICENSE file
#include <iostream>
#include <type_traits>
#include <variant>
inline constexpr char IdleStateName[] = "Idle";
inline constexpr char PausedStateName[] = "Paused";
inline constexpr char RunningStateName[] = "Running";
inline constexpr char StopEventName[] = "Stop";
inline constexpr char PauseEventName[] = "Pause";
inline constexpr char StartEventName[] = "Start";
template<const char* Name>
struct Label {
inline static constexpr const char* toString() { return Name; }
};
struct IState {};
struct IEvent {};
struct InvalidState : IState {};
struct InvalidEvent : IEvent {};
struct Idle : IState, Label<IdleStateName> {};
struct Paused : IState, Label<PausedStateName> {};
struct Running : IState, Label<RunningStateName> {};
struct Stop : IEvent, Label<StopEventName> {};
struct Pause : IEvent, Label<PauseEventName> {};
struct Start : IEvent, Label<StartEventName> {};
using State = std::variant<Idle, Running, Paused>;
/*************************************************/
template<typename... Field>
struct EntryKey {};
template<typename... Field>
struct EntryValue {};
/*************************************************/
template<typename, typename>
struct TableEntry;
template<typename... KFields, typename... VFields>
struct TableEntry<EntryKey<KFields...>, EntryValue<VFields...>> {
using key = EntryKey<KFields...>;
using value = EntryValue<VFields...>;
};
/*************************************************/
template<typename>
struct entry_key_size_impl;
template<typename... Fields>
struct entry_key_size_impl<EntryKey<Fields...>> {
inline static constexpr std::size_t value = sizeof...(Fields);
};
template<typename Key>
struct entry_key_size {
inline static constexpr std::size_t value = entry_key_size_impl<Key>::value;
};
template<typename Key>
inline static constexpr std::size_t entry_key_size_v = entry_key_size<Key>::value;
/*************************************************/
template<typename>
struct entry_value_size_impl;
template<typename... Fields>
struct entry_value_size_impl<EntryValue<Fields...>> {
inline static constexpr std::size_t value = sizeof...(Fields);
};
template<typename Value>
struct entry_value_size {
inline static constexpr std::size_t value = entry_value_size_impl<Value>::value;
};
template<typename Value>
inline static constexpr std::size_t entry_value_size_v = entry_value_size<Value>::value;
/*************************************************/
template<typename...>
struct Table {}; //ULTRA TEMPORAIRE ULTRA TEMPORAIRE ULTRA TEMPORAIRE ULTRA TEMPORAIRE
template<
template<template<typename...> class, template<typename...> class> class... Entries,
template<typename...> class Key, template<typename...> class Value
>
struct Table<Entries<Key, Value>...> {};
//ici j'ai un prob, il faut que ce soit LE NOMBRE qui soit le meme,
//faut pas que ce soit les types de Key qui soit identiques
/*************************************************/
template<typename>
struct table_count_impl;
template<typename... Entries>
struct table_count_impl<Table<Entries...>> {
inline static constexpr std::size_t value = sizeof...(Entries);
};
template<typename Entry>
struct table_count {
inline static constexpr std::size_t value = table_count_impl<Entry>::value;
};
template<typename Entry>
inline static constexpr std::size_t table_count_v = table_count<Entry>::value;
/*************************************************/
template<typename>
struct table_entry_size_impl;
template<typename Entry>
struct table_entry_size_impl {
inline static constexpr std::size_t value =
entry_key_size<typename Entry::key>::value
+ entry_value_size<typename Entry::value>::value;
};
template<typename Entry>
struct table_entry_size {
inline static constexpr std::size_t value = table_entry_size_impl<Entry>::value;
};
template<typename Entry>
inline static constexpr std::size_t table_entry_size_v =
table_entry_size_impl<Entry>::value;
/*************************************************/
template<
std::size_t N, typename Table,
typename Enable = std::enable_if_t<(N < table_count_v<Table>)> //fragile
>
struct at_impl;
template<template<typename...> class L, typename T, typename... Ts>
struct at_impl<0, L<T, Ts...>> {
using type = T;
};
template<std::size_t N, template<typename...> class L, typename T, typename... Ts>
struct at_impl<N, L<T, Ts...>> {
using type = typename at_impl<N - 1, L<Ts...>>::type;
};
//possiblement inclure ma lib et utiliser celle a disposition
//meme chose pour les méthodes utilitaires extra
/*************************************************/
template<typename... Ts>
struct min { // a mettre dans ma lib
inline static constexpr std::size_t value = //fragile et brisé je penses
((std::min({ table_entry_size<Ts>::value })), ...);
};//probablement preferer max pour verifier
/*************************************************/
template<typename... Ts>
struct max {
inline static constexpr std::size_t value =
((std::max({ table_entry_size<Ts>::value })), ...);
};
/*************************************************/
//un peu sale mais ca fonctionne ;)
template<typename Table, typename F, std::size_t... Is>
constexpr auto
for_each_in_table_impl2(std::index_sequence<Is...>, F&& func) {
(func(typename at_impl<Is, std::remove_cvref_t<Table>>::type{}), ...);
}
template<typename Table, typename F, std::size_t... Is>
constexpr auto
get_for_each_in_table_impl2(std::index_sequence<Is...>, F&& func) {
return std::tuple{
func(typename at_impl<Is, std::remove_cvref_t<Table>>::type{})...
};
}
template<typename F, typename Table>
constexpr decltype(auto)
for_each_in_table(F&& func, Table&& table) {
for_each_in_table_impl2<Table>(
std::make_index_sequence<
table_count_v<std::remove_cvref_t<decltype(table)>>
>{},
func
);
}
template<typename F, typename Table>
constexpr decltype(auto)
get_for_each_in_table(F&& func, Table&& table) {
return get_for_each_in_table_impl2<Table>(
std::make_index_sequence<
table_count_v<std::remove_cvref_t<decltype(table)>>
>{},
func
);
}
/*************************************************/
template<typename Table>
struct TableLookup {
inline static constexpr std::size_t size = table_count<Table>::value;
//template<typename... Keys>
//auto find_by_key(Keys&&...) const
// -> std::enable_if_t<
// (sizeof...(Keys) == std::)
// >
//utiliser index sequence + at
//size
//find (key -> value)
//contains (value -> exists)
//potentiellement un at (index -> value)
};
/*************************************************/
//template<typename...>
//struct Entry;
//
//template<typename Current, typename Event, typename Result>
//struct Entry<Current, Event, Result> {
// using current = Current;
// using event = Event;
// using result = Result;
//};
/*************************************************/
//template<typename>
//struct Table;
//
//template<template<typename...> class... Entries, typename... Fields>
//struct Table<Entries<Fields...>...> {};
/*************************************************/
//template<typename>
//struct entry_count_impl;
//
//template<typename... Fields>
//struct entry_count_impl<Entry<Fields...>> {
// inline static constexpr std::size_t value = sizeof...(Fields);
//};
//
//template<typename Entry>
//struct entry_count {
// inline static constexpr std::size_t value = entry_count_impl<Entry>::value;
//};
//
//template<typename Entry>
//inline static constexpr std::size_t entry_count_v = entry_count<Entry>::value;
/*************************************************/
/*************************************************/
/*************************************************/
/*************************************************/
//a la longue, faire un system de dictionnaire compile-time avec clé valeur
template<typename Current, typename Event, typename Result>
struct Transition {
using current = Current;
using event = Event;
using result = Result;
};
template<typename... Transitions>
struct TransitionTable {};
template<typename Table>
struct TransitionTableDispatcher {};
template<typename... Entries>
struct TransitionTableDispatcher<TransitionTable<Entries...>> {
constexpr void test() { //peut etre faire un truc avec lambda
((std::cout
<< typename Entries::current::toString() << " + "
<< typename Entries::event::toString() << " -> "
<< typename Entries::result::toString() << "\n"
), ...);
//faire une fonction genre check_table ou something like that.
//fait juste iterer et si A et B == true alors on return C
}
};
//faire facade au pire pour garder le format (prefere lui)
//ou bien faire un type entry spécial pour transition
using MyFsm = TransitionTable<
Transition<Idle, Start, Running>,
Transition<Running, Pause, Paused>,
Transition<Running, Stop, Idle>,
Transition<Paused, Start, Running>,
Transition<Paused, Stop, Idle>
>;
/*************************************************/
template<typename State, typename Event>
struct transition_impl {
using to = State; // ou invalid state et check après
};
template<>
struct transition_impl<Idle, Start> {
using to = Running;
};
template<>
struct transition_impl<Running, Pause> {
using to = Paused;
};
template<>
struct transition_impl<Running, Stop> {
using to = Idle;
};
template<>
struct transition_impl<Paused, Start> {
using to = Running;
};
template<>
struct transition_impl<Paused, Stop> {
using to = Idle;
};
template<typename E>
State transition(const State& state, E) {
return std::visit([&]<typename Cur>(Cur&&) -> State {
return typename transition_impl<
std::remove_cvref_t<Cur>,
std::remove_cvref_t<E>
>::to{};
}, state);
}
struct Engine final {
public:
explicit Engine(State init_state = Idle{})
: state_(std::move(init_state)) {}
public:
template<typename Event>
void process_event() {
state_ = transition(state_, Event{});
printState(state_);
}
void printState(const State& s) {
std::visit([](auto&& val) {
std::cout << "State: " << val.toString() << "\n";
}, s);
}
private:
State state_;
};
struct Derived {
public:
private:
};
int main() {
//using n = transition<State::Idle, Event::Start>::to;
//std::cout << typeid(n).name() << "\n";
using f = transition_impl<Idle, Start>::to;
using f2 = transition_impl<f, Stop>::to;
f result;
f2 result2;
//std::cout << result.toString() << "\n";
//std::cout << result2.toString() << "\n";
Engine e{};
e.process_event<Start>();
e.process_event<Pause>();
e.process_event<Pause>();
e.process_event<Stop>();
e.process_event<Pause>();
auto d = TransitionTableDispatcher<MyFsm>{};
d.test();
/*
Engine e;
e.process_event<Start>(); // Idle → Running
e.process_event<Pause>(); // Running → Paused
e.process_event<Start>(); // Paused → Running
e.process_event<Stop>(); // Running → Idle
*/ //Transition<Idle, Start, Running>, Transition<Running, Pause, Paused>,
using Table0 = Table<
TableEntry<EntryKey<Idle, Start>, EntryValue<Running>>,
TableEntry<EntryKey<Running, Pause>, EntryValue<Paused>>
>;
using res0 = at_impl<0, Table0>::type;
using res1 = at_impl<1, Table0>::type;
std::cout << "Key: " << typeid(typename res0::key).name() << "\n";
std::cout << "Value: " << typeid(typename res0::value).name() << "\n";
constexpr std::size_t size = min<res0, res1>::value;
std::cout << size << "\n";
auto table = Table0{};
auto t2 = get_for_each_in_table([](auto&&... entry) {
return ((table_entry_size_v<std::remove_cvref_t<decltype(entry)>>), ...);
}, table);
std::apply([](auto&&... sz) {
std::cout << "MIN SIZE: " << std::min({ sz... }) << "\n";
std::cout << "MAX SIZE: " << std::max({ sz... }) << "\n";
}, t2);
for_each_in_table([](auto&&... entry) {
((std::cout << "for_each_in_table() -> " << typeid(decltype(entry)).name() << "\n"), ...);
}, table);
}