-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstantFSM.h
1854 lines (1421 loc) · 51 KB
/
instantFSM.h
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2014 - Alexis Chol
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
Cheat Sheet :
StateMachine myMachine( parallelTag|State|OnEntry|OnExit|OnEvent|Transition )
: instantiate the FSM
myMachine.enter() : enter the root state
myMachine.pushEvent(std::string("myEvent")) : push an event
myMachine.inState(std::string("stateName")) : returns true of the named state is active
myMachine.leave() : quit all active states
State(std::string("stateName"), parallelTag|initialTag|State|OnEntry|OnExit|OnEvent|Transition) : create a state
-> OnEntry( void(void) | void(StateMachine&) ) : callback triggered when parent state is entered
-> OnExit( void(void) | void(StateMachine&) ) : callback triggered when parent state is exited
-> OnEvent( std::string("myEvent"), void(void) | void(StateMachine&) ) : callback triggered when the event "myEvent" is pushed while the parent state is active.
Transition( OnEvent|Target|Action|Condition ) : add a transition
-> OnEvent( std::string("myEvent") ) : event triggering the transition
-> Target( std::string("stateName") ) : state to activate when the transition is realised
-> Action( void(void)|void(StateMachine&) ) : callback triggered after leaving the parent state and before the target state is entered.
-> Condition( bool(void)|bool(StateMachine&) ) : callback preventing the transition from executing when it returns false
*/
#ifndef INSTANTFSM_H
#define INSTANTFSM_H
#include <unordered_map>
#include <map>
#include <vector>
#include <string>
#include <functional>
#include <utility>
#include <cstddef>
#include <algorithm>
#include <queue>
#include <list>
#include <type_traits>
#include <memory>
#include <stdexcept>
// Is noexcept supported?
#if !defined(_MSC_FULL_VER) || _MSC_VER > 1800
# define NOEXCEPT noexcept
#else
# define NOEXCEPT
#endif
namespace ifsm{
class StateMachine;
namespace priv{
class StateImpl;
}
class StateMachineException : public std::logic_error {
protected:
StateMachineException(const std::string& pWhat)
: std::logic_error(pWhat){}
};
class AlreadyHasInitial : public StateMachineException{
public:
AlreadyHasInitial(const std::string& pState)
: StateMachineException("State "+pState+" has two children is initialTag parameter set. Only one initial child is permitted.")
, mName(pState){
}
virtual ~AlreadyHasInitial() NOEXCEPT{}
public:
const std::string mName;
};
class DuplicateStateIdentifier : public StateMachineException{
public:
DuplicateStateIdentifier(const std::string& pState)
: StateMachineException("The StateMachine declares two States named "+pState+". Unique names are required.")
, mName(pState){
}
virtual ~DuplicateStateIdentifier() NOEXCEPT{}
public:
const std::string mName;
};
class NoInitialState : public StateMachineException{
public:
NoInitialState(const std::string& pState)
: StateMachineException("State "+pState+" is not parallel and doesn't have any initial child. One initial child is required for non-parallel nested States.")
, mName(pState){
}
virtual ~NoInitialState() NOEXCEPT{}
public:
const std::string mName;
};
class NoSuchState : public StateMachineException{
public:
NoSuchState(const std::string& pState)
: StateMachineException("A transition targets a State named \""+pState+"\" which doesn't exist in the StateMachine.")
, mName(pState){
}
virtual ~NoSuchState() NOEXCEPT{}
public:
const std::string mName;
};
class TargetAlreadySpecified : public StateMachineException{
public:
TargetAlreadySpecified(const std::string& pTarget)
: StateMachineException("A Transition defines two Targets. Only one Target() per Transition is allowed")
, mTarget(pTarget){
}
virtual ~TargetAlreadySpecified() NOEXCEPT{}
public:
const std::string mTarget;
};
class ActionAlreadySpecified : public StateMachineException{
public:
ActionAlreadySpecified()
: StateMachineException("A transition has two Action parameters defined. Only one is allowed.")
{}
};
class ConditionAlreadySpecified : public StateMachineException{
public:
ConditionAlreadySpecified()
: StateMachineException("A transition has two Condition parameters defined. Only one is allowed.")
{}
};
class EventAlreadySpecified : public StateMachineException{
public:
EventAlreadySpecified()
: StateMachineException("A transition has two OnEvent parameters defined. Only one is allowed.")
{}
};
}
namespace ifsm{
namespace priv{
template <class CallableType, typename... PTypes>
struct is_callable_with{
typedef char yes[1];
typedef char no[2];
template <typename CType>
static yes& test(decltype(std::declval<CType>()(std::declval<PTypes>()...))*);
template <typename>
static no& test(...);
static const bool value = sizeof(test<CallableType>(0)) == sizeof(yes);
};
template <class CallableType>
struct is_callable{
typedef char yes[1];
typedef char no[2];
template <typename CType>
static yes& test(decltype(std::declval<CType>()())*);
template <typename>
static no& test(...);
static const bool value = sizeof(test<CallableType>(0)) == sizeof(yes);
};
template <class CallableType, class Ret>
struct returns{
typedef char yes[1];
typedef char no[2];
template <typename CType, typename B = typename std::enable_if<std::is_same<decltype(std::declval<CType>()()), Ret>::value>::type>
static yes& test(int);
template <typename>
static no& test(...);
static const bool value = sizeof(test<CallableType>(0)) == sizeof(yes);
};
template <class CallableType, class Ret, typename... Params>
struct returns_with{
typedef char yes[1];
typedef char no[2];
template <typename CType, typename B = typename std::enable_if<std::is_same<decltype(std::declval<CType>()(std::declval<Params>()...)), Ret>::value>::type>
static yes& test(int);
template <typename>
static no& test(...);
static const bool value = sizeof(test<CallableType>(0)) == sizeof(yes);
};
//turn f() into f(SM)
template<class Callable, typename B = typename std::enable_if<is_callable<Callable>::value>::type>
std::function<void(StateMachine&) > fixParams(Callable && pCallable){
return std::function<void(StateMachine&) >([pCallable](StateMachine& ){
pCallable();
});
}
//turn f(SM) into f(SM) : no change
template<class Callable, typename B = typename std::enable_if<is_callable_with<Callable, StateMachine&>::value>::type, typename U = void>
std::function<void(StateMachine&) > fixParams(Callable && pCallable){
return std::function<void(StateMachine&) >(pCallable);
}
//turn bool f() into bool f(SM)
template<class Callable, typename B = typename std::enable_if<returns<Callable, bool>::value>::type, typename U = void, typename X = void>
std::function<bool(const StateMachine&)> fixConditionParams(Callable && pCallable){
return std::function<bool(const StateMachine&)>([pCallable](const StateMachine& ){
return pCallable();
});
}
//turn bool f(SM) into bool f(SM) : no change
template<class Callable, typename B = typename std::enable_if<returns_with<Callable, bool, const StateMachine&>::value>::type, typename U = void, typename X = void, typename Y = void>
std::function<bool(const StateMachine&)> fixConditionParams(Callable && pCallable){
return std::function<bool(const StateMachine&)>(pCallable);
}
}
}
namespace ifsm{
namespace priv{
class OnEntryAction;
class OnExitAction;
}
template <class FunType>
priv::OnEntryAction OnEntry(FunType && pFun);
template <class FunType>
priv::OnExitAction OnExit(FunType && pFun);
}
namespace ifsm{
namespace priv{
class OnEntryAction{
template <class FunType>
friend OnEntryAction ifsm::OnEntry(FunType && pFun);
friend class StateDef;
friend class StateImpl;
private:
OnEntryAction(std::function<void(StateMachine&) > && pFun)
: mFun(std::move(pFun))
{}
private:
void operator()(StateMachine& pRoot){
mFun(pRoot);
}
private:
std::function<void(StateMachine&) > mFun;
};
class OnExitAction{
template <class FunType>
friend OnExitAction ifsm::OnExit(FunType && pFun);
friend class StateDef;
friend class StateImpl;
OnExitAction(std::function<void(StateMachine&) > && pFun)
: mFun(std::move(pFun))
{}
private:
void operator()(StateMachine& pRoot){
mFun(pRoot);
}
private:
std::function<void(StateMachine&) > mFun;
};
}
}
namespace ifsm{
namespace priv{
class TransitionTarget;
class TransitionAction;
class TransitionCondition;
class TransitionEvent;
class TransitionImpl;
class TransitionDef;
}
/*
Define the target name of a transition
*/
inline priv::TransitionTarget Target(const std::string& pTargetName);
template <class FunType>
/*
Define the callback that will be call during execution of the transition.
pAction must be callable in the form
void(StateMachine&)
or
void(void)
it is called after leaving the source state and before entering the target state.
*/
priv::TransitionAction Action(FunType&& pAction);
template <class FunType>
/*
Define a condition for the selection of the transition as a callback callable in the form
bool(StateMachine&)
or
bool(void)
*/
priv::TransitionCondition Condition(FunType&& pCondition);
/*
Define the event that will trigger the transition
*/
inline priv::TransitionEvent OnEvent(const std::string& pEvent);
/*
Creates a new transition when called as a parameter of the State function.
*/
template <typename... Args>
priv::TransitionDef Transition(Args&&... pArgs);
/*
Shortcut for defining a targetless transition when called as a parameter of the State function.
*/
template <class FunType>
priv::TransitionDef OnEvent(const std::string& pEvent, FunType && pFun);
namespace priv{
class TransitionTarget{
friend TransitionTarget ifsm::Target(const std::string& pTargetName);
friend class ifsm::priv::TransitionDef;
inline TransitionTarget(const std::string& pTargetName);
std::string mTargetName;
};
class TransitionAction{
template <class FunType>
friend TransitionAction ifsm::Action(FunType&& pAction);
friend class ifsm::priv::TransitionDef;
inline TransitionAction(std::function<void(StateMachine&)>&& pFun);
inline void operator()(StateMachine& pRoot);
std::function<void(StateMachine&)> mFun;
};
class TransitionCondition{
template <class FunType>
friend TransitionCondition ifsm::Condition(FunType&& pCondition);
friend class ifsm::priv::TransitionDef;
inline TransitionCondition(std::function<bool(const StateMachine&)>&& pCond);
inline bool operator()(const StateMachine& pRoot);
std::function<bool(const StateMachine&)> mCond;
};
class TransitionEvent{
friend TransitionEvent ifsm::OnEvent(const std::string& pEvent);
friend class ifsm::priv::TransitionDef;
inline TransitionEvent(const std::string& pEvent);
std::string mEvent;
};
class TransitionImpl;
class TransitionDef{
template <typename... Args>
friend ifsm::priv::TransitionDef ifsm::Transition(Args&&... pArgs);
template <class FunType>
friend ifsm::priv::TransitionDef ifsm::OnEvent(const std::string& pEvent, FunType && pFun);
friend class TransitionImpl;
friend class StateImpl;
public:
inline TransitionDef(TransitionDef&& pRhs);
private:
template <typename... Params>
inline TransitionDef(Params && ... pParams);
inline void addParameter(priv::TransitionTarget && pTarget);
inline void addParameter(priv::TransitionAction && pAction);
inline void addParameter(priv::TransitionCondition && pCondition);
inline void addParameter(priv::TransitionEvent && pEvent);
template <class T>
void addParameters(T&& pParam1);
template <class T, typename... Params>
void addParameters(T&& pParam1, Params && ... pParams);
private:
std::string mTarget;
std::string mEvent;
std::function<void(StateMachine&)> mAction;
std::function<bool(const StateMachine&)> mCondition;
};
};
template <typename... Params>
priv::TransitionDef Transition(Params && ... pParams);
namespace priv{
class TransitionImpl{
friend class ifsm::StateMachine;
friend class ifsm::priv::StateImpl;
public:
inline TransitionImpl(const TransitionDef& pDef);
private:
inline void setSource(StateImpl* pSource);
inline void setTarget(StateImpl* pTarget);
inline const StateImpl* getSource() const;
inline const StateImpl* getTarget() const;
inline const std::string& getEvent() const;
inline bool test(const StateMachine& pRoot) const;
inline void doAction(StateMachine& pRoot) const;
private:
StateImpl* mSource;
StateImpl* mTarget;
std::string mEvent;
std::function<void(StateMachine&)> mAction;
std::function<bool(const StateMachine&)> mCondition;
};
}
};
namespace ifsm{
namespace priv{
struct initialTag_t{};
struct parallelTag_t{};
}
static priv::initialTag_t initialTag;
static priv::parallelTag_t parallelTag;
namespace priv{
template <class SearchType>
struct StateIterator;
}
}
namespace ifsm{
namespace priv{
class StateDef;
class StateImpl;
}
template <typename... Args>
priv::StateDef State(const std::string& pName, Args&&... pArgs);
}
namespace ifsm{
template <typename... Args>
ifsm::priv::StateDef State(const std::string& pName, Args&&... pArgs);
namespace priv{
class StateDef{
template <typename... Args>
friend StateDef ifsm::State(const std::string& pName, Args&&... pArgs);
friend class ifsm::priv::StateImpl;
friend class ifsm::StateMachine;
private:
inline StateDef(const std::string& pName);
template <typename... Params>
inline StateDef(const std::string& pName, Params && ... pParameters);
inline void addParameter(StateDef && pState);
inline void addParameter(TransitionDef && pTransition);
inline void addParameter(OnEntryAction && pAction);
inline void addParameter(OnExitAction && pAction);
inline void addParameter(initialTag_t& pTag);
inline void addParameter(parallelTag_t& pTag);
template <class FirstP>
void addParameters(FirstP && pFirst);
template <class FirstP, typename... Params>
void addParameters(FirstP && pFirst, Params && ... pParameters);
private:
std::string mName;
bool mIsInitial;
bool mIsParallel;
std::vector<StateDef> mChildren;
std::vector<TransitionDef> mTransitions;
std::vector<OnEntryAction> mOnEntryActions;
std::vector<OnExitAction> mOnExitActions;
};
class StateImpl{
//everything is private, only StateMachine is allowed to use a StateImpl
friend class ifsm::StateMachine;
template <typename>
friend struct ifsm::priv::StateIterator;
public:
typedef std::vector<StateImpl*> ChildrenMap;
typedef std::multimap<std::string, std::unique_ptr<TransitionImpl>> TransitionsMap;
public:
inline StateImpl();
inline virtual ~StateImpl();
protected:
inline void build(StateMachine* pRoot, StateImpl* pParent, StateDef&& pDef);
inline const std::string& getName() const;
inline bool isAtomic() const;
inline bool isInitial() const;
inline bool isParallel() const;
inline virtual bool isActive() const;
inline void enter();
inline void leave();
protected:
std::string mName;
StateImpl* mParent;
StateMachine* mRoot;
bool mIsInitial;
bool mIsParallel;
StateImpl* mInitial;
StateImpl* mActive;
ChildrenMap mChildren;
TransitionsMap mTransitions;
std::vector<priv::OnEntryAction> mOnEntryActions;
std::vector<priv::OnExitAction> mOnExitActions;
};
}
class StateMachine {
friend class priv::StateImpl;
public:
template <typename... Params>
StateMachine(Params && ... pParams);
public:
/*
start the current state machine by calling State::enter ()
for all initial children, depth first
*/
inline void enter();
/*
terminate the current state machine
by calling State::leave() for all active states
from leaves to root, depth first
*/
inline void leave();
/*
returns whether the state machine is active
*/
inline virtual bool isActive() const;
/*
add an event to the event queue
*/
inline void pushEvent(const std::string& pEvent);
/*
returns whether the current configuration has the given state active
*/
inline bool inState(const std::string& stateName);
private: // functioning primitives
inline void processEvents();
inline void processTransitions(const std::string& pEvent);
/*
browse through the tree of states to select transitions with a matching event
and a realized condition
*/
inline std::vector<priv::TransitionImpl*> selectTransitions(const std::string& pEvent);
/*
remove transitions having conflicting source/target state
*/
inline std::vector<priv::TransitionImpl*> removeConflicts(std::vector<priv::TransitionImpl*>& pTransitions);
/*
compute a list of states that will be exited during execution of the transtion pTransition from the current configuration
*/
inline std::list<priv::StateImpl*> listExitStates(priv::TransitionImpl* pTransition);
/*
compute a list of states that will be entered to activate pState from the current configuration
*/
inline std::list<priv::StateImpl*> listEntryStates(priv::StateImpl* pState);
/*
returns true if pCheck is a descendant of pAgainst
*/
inline bool isDescendant(priv::StateImpl* pCheck, priv::StateImpl* pAgainst);
/*
execute exit behavior for each state that must be exited while executing the
given transitions
*/
inline void exitStates(const std::vector<priv::TransitionImpl*>& pTransitions);
/*
execute enter behavior for each state that must be entered while executing the
given transitions
*/
inline void enterStates(const std::vector<priv::TransitionImpl*>& pTransitions);
/*
trigger State::leave() to each state that needs to be exited when to enter pTarget
from the first active ancestor
*/
inline void exitStates(priv::StateImpl* pTarget);
/*
trigger State::enter() to each state that needs to be entered when entering pTarget
from the first active ancestor
*/
inline void enterStates(priv::StateImpl* pTarget);
/*
get the common ancestor of the source and target states
*/
inline priv::StateImpl* getTransitionDomain(priv::TransitionImpl* pTransition);
/*
find the lowest common ancestor of the two states
*/
inline priv::StateImpl* findLeastCommonAncestor(priv::StateImpl* pLhs, priv::StateImpl* pRhs);
private:
std::unordered_map<std::string, std::unique_ptr<priv::StateImpl>> mAllStates;
std::queue<std::string> mEvents;
std::queue<priv::TransitionImpl*> mTransitions;
bool mIsActive;
bool mInToplevelProcess;
priv::StateImpl* mImpl;
};
}
namespace ifsm{
namespace priv{
/**
Iterates over children of a state
*/
template <class SearchType>
struct StateIterator{
friend class ifsm::priv::StateImpl;
friend class ifsm::StateMachine;
private: //only State is allowed to use iterators
StateIterator();
StateIterator(ifsm::priv::StateImpl* pFirst);
StateIterator operator++();
StateIterator operator++(int);
priv::StateImpl* operator*() const;
bool operator==(const StateIterator<SearchType>& pRhs) const;
bool operator!=(const StateIterator<SearchType>& pRhs) const;
void setTest(std::function < bool(::ifsm::priv::StateImpl*)>& pTest);
void setTest(std::function < bool(::ifsm::priv::StateImpl*)>&& pTest);
void lookupNextValidState();
public:
const priv::StateImpl::ChildrenMap& getStateChildren(const priv::StateImpl* pState);
private:
SearchType mSearch;
std::function < bool(::ifsm::priv::StateImpl*)> mTest;
};
struct DepthFirstSearch{
typedef DepthFirstSearch MyType;
typedef StateIterator<MyType> MyIterator;
inline DepthFirstSearch(MyIterator& pIterator, priv::StateImpl* pFirst);
inline void fetchNext();
inline priv::StateImpl* getCurrent() const;
MyIterator& mIterator;
std::list<priv::StateImpl*> mLifo;
};
struct WidthFirstSearch{
typedef WidthFirstSearch MyType;
typedef StateIterator<MyType> MyIterator;
inline WidthFirstSearch(MyIterator& pIterator, priv::StateImpl* pFirst);
inline void fetchNext();
inline priv::StateImpl* getCurrent() const;
MyIterator& mIterator;
std::list<priv::StateImpl*> mFifo;
};
}
}
template <class FunType>
ifsm::priv::OnEntryAction ifsm::OnEntry(FunType && pFun){
using ifsm::priv::is_callable;
using ifsm::priv::is_callable_with;
static_assert(is_callable<FunType>::value || is_callable_with<FunType, StateMachine&>::value, "parameter to onEntry must be callable either with no parameter or a StateMachine& parameter");
return priv::OnEntryAction(priv::fixParams(std::forward<FunType>(pFun)));
}
template <class FunType>
ifsm::priv::OnExitAction ifsm::OnExit(FunType && pFun){
using ifsm::priv::is_callable;
using ifsm::priv::is_callable_with;
static_assert(is_callable<FunType>::value || is_callable_with<FunType, StateMachine&>::value, "parameter to onExit must be callable either with no parameter or a StateMachine& parameter");
return priv::OnExitAction(priv::fixParams(std::forward<FunType>(pFun)));
}
template <class FunType>
ifsm::priv::TransitionDef ifsm::OnEvent(const std::string& pEvent, FunType && pFun){
return priv::TransitionDef(OnEvent(pEvent), Action(std::forward<FunType>(pFun)));
}
ifsm::priv::TransitionTarget ifsm::Target(const std::string& pTargetName){
return priv::TransitionTarget(pTargetName);
}
template <class FunType>
ifsm::priv::TransitionAction ifsm::Action(FunType&& pAction){
using ifsm::priv::is_callable;
using ifsm::priv::is_callable_with;
static_assert(is_callable<FunType>::value || is_callable_with<FunType, StateMachine&>::value,
"parameter to action must be callable either with no paramater or a 'StateMachine&' parameter");
return priv::TransitionAction(priv::fixParams(pAction));
}
template <class FunType>
ifsm::priv::TransitionCondition ifsm::Condition(FunType&& pCondition){
using ifsm::priv::returns;
using ifsm::priv::returns_with;
static_assert(returns<FunType, bool>::value || returns_with<FunType, bool, const StateMachine&>::value,
"parameter to action must be callable either with no paramater or a 'StateMachine&' parameter and must return 'bool'");
return priv::TransitionCondition(priv::fixConditionParams(pCondition));
}
ifsm::priv::TransitionEvent ifsm::OnEvent(const std::string& pEvent){
return priv::TransitionEvent(pEvent);
}
ifsm::priv::TransitionTarget::TransitionTarget(const std::string& pTargetName)
: mTargetName(pTargetName){}
ifsm::priv::TransitionAction::TransitionAction(std::function<void(StateMachine&)>&& pFun)
: mFun(pFun){}
void ifsm::priv::TransitionAction::operator()(StateMachine& pRoot){
mFun(pRoot);
}
ifsm::priv::TransitionCondition::TransitionCondition(std::function<bool(const StateMachine&)>&& pCond)
: mCond(pCond)
{}
bool ifsm::priv::TransitionCondition::operator()(const StateMachine& pRoot){
return mCond(pRoot);
}
ifsm::priv::TransitionEvent::TransitionEvent(const std::string& pEvent)
: mEvent(pEvent){}
ifsm::priv::TransitionDef::TransitionDef(TransitionDef&& pRhs)
: mTarget(std::move(pRhs.mTarget))
, mEvent(std::move(pRhs.mEvent))
, mAction(std::move(pRhs.mAction))
, mCondition(std::move(pRhs.mCondition)){
}
template <typename... Params>
ifsm::priv::TransitionDef::TransitionDef(Params && ... pParams){
addParameters(std::forward<Params>(pParams)...);
}
void ifsm::priv::TransitionDef::addParameter(priv::TransitionTarget && pTarget){
if (!mTarget.empty()){
throw TargetAlreadySpecified(pTarget.mTargetName);
}
mTarget = pTarget.mTargetName;
}
void ifsm::priv::TransitionDef::addParameter(priv::TransitionAction && pAction){
if (mAction){
throw ActionAlreadySpecified();
}
mAction = pAction.mFun;
}
void ifsm::priv::TransitionDef::addParameter(priv::TransitionCondition && pCondition){
if (mCondition){
throw ConditionAlreadySpecified();
}
mCondition = pCondition.mCond;
}
void ifsm::priv::TransitionDef::addParameter(priv::TransitionEvent && pEvent){
if (!mEvent.empty()){
throw EventAlreadySpecified();
}
mEvent = pEvent.mEvent;
}
template <class T>
void ifsm::priv::TransitionDef::addParameters(T&& pParam1){
addParameter(std::forward<T>(pParam1));
}
template <class T, typename... Params>
void ifsm::priv::TransitionDef::addParameters(T&& pParam1, Params && ... pParams){
addParameter(std::forward<T>(pParam1));
addParameters(std::forward<Params>(pParams)...);
}
template <typename... Params>
ifsm::priv::TransitionDef ifsm::Transition(Params && ... pParams){
return priv::TransitionDef(std::forward<Params>(pParams)...);
}
ifsm::priv::TransitionImpl::TransitionImpl(const TransitionDef& pDef)
: mSource(nullptr)
, mTarget(nullptr)
, mEvent(std::move(pDef.mEvent))
, mAction(std::move(pDef.mAction))
, mCondition(std::move(pDef.mCondition)){
}
void ifsm::priv::TransitionImpl::setSource(StateImpl* pSource){
mSource = pSource;
}
void ifsm::priv::TransitionImpl::setTarget(StateImpl* pTarget){
mTarget = pTarget;
}
const ifsm::priv::StateImpl* ifsm::priv::TransitionImpl::getSource() const{
return mSource;
}
const ifsm::priv::StateImpl* ifsm::priv::TransitionImpl::getTarget() const{
return mTarget;
}
const std::string& ifsm::priv::TransitionImpl::getEvent() const {
return mEvent;
}
bool ifsm::priv::TransitionImpl::test(const StateMachine& pRoot) const {
if (!mCondition){
return true;
}
return mCondition(pRoot);
}
void ifsm::priv::TransitionImpl::doAction(StateMachine& pRoot) const {
if (mAction) {
mAction(pRoot);
}
}
template <typename... Args>
ifsm::priv::StateDef ifsm::State(const std::string& pName, Args&&... pArgs){
return priv::StateDef(pName, std::forward<Args>(pArgs)...);
}
ifsm::priv::StateDef::StateDef(const std::string& pName)
: mName(pName)
, mIsInitial(false)
, mIsParallel(false){
}
template <typename... Params>
ifsm::priv::StateDef::StateDef(const std::string& pName, Params && ... pParameters)
: mName(pName)
, mIsInitial(false)
, mIsParallel(false){
addParameters(std::forward<Params>(pParameters)...);
}