All right so in the previous lesson we implemented our other bizarre constructs for modeling the state
of the system and we did it using default virtual implementations and whatnot.
So now I'm going to show you something that's both simpler and more efficient.
At the same time and that is the idea of hand making a state machine using a slightly different approach.
So let me get my headers in here and let's have some sort of mean implementation now what I'm going
to begin with is a scenario I'm going to model a phone call and a phone can be in several different
states and I'm going to model those states not as entire types but just as members.
So having the name classical state so the phone can be off the hook it can be connecting it can be connected
to somebody.
You can be on hold.
Not a very nice state to be and you can put the phone back on the hook when you're done talking.
OK.
So now what I'm going to do is define a stream output the operator for actually printing these members.
So here it is just getting everyone to print nicely.
And then when I'm going to do is I'm going to specify the triggers.
Now the triggers are essentially you can think of them as events which cause you to transition from
one state to another.
So once again this is going to be in a class called trigger.
And you might die a little saw a trigger might be called dialed hung up call connected.
You can be placed on hold
taken off hold.
You can leave a message.
And finally you can stop using the phone when you're done using it.
You can just you know transition to the on hook kind of thing.
So once again when I'm going to do is I'm going to paste in a definition of the stream map with operator.
Unfortunately C++ does not give us this functionality by default so I hear this and that what we're
going to do is we're going to actually model the state machine in our main function.
So how do we model the state machine while the state machine is really a set of rules.
It's a set of definitions for which state can transition you from a current state to some other state.
So it's essentially a map.
So it's a map where you are starting off in a particular state and what you can do is you can transition
to a number of states based on a number of triggers.
So here we have a vector of pairs of trigger and state like so.
And that's our rules essentially.
So for example you can transition from the on hold state to the phone thrown against the wall state
and that will be on hold here.
And then one of the elements of the vector would be a trigger off placed on hold.
And another state called phone destroyed for example.
So that is how you can and because you can transition from one state to a number of other possible states
we have a vector here instead of just a single transition.
So out of any given state you can have as many transitions as you want.
So I give you a concrete example.
Let's actually define those rules so for example if I'm off the hook if state is off hook then what
I can do is I can do the following.
I can dial a call for example.
So in this case the trigger would be called dialed and the resulting state would be connecting.
Alternatively what I can do is I can stop using the phone.
So the trigger would be stop using the phone and the state would be on hook.
So in here I've defined a part of the state machine so I've defined one of the rules let me define a
few more rules.
So in addition to the rules that I've just defined here I've also defined the current state variables
so this tells us what the starting state is and it's going to be updating with the current state.
So we're starting in the off hook.
Let's suppose we picked up the phone and that's when the game begins and then we have the exit state
the exit state is when you should stop executing the entire state machine altogether.
And that is the on hook state.
So now that we have all of these rules what we can do is we can define a loop.
So I would just say while true here what we're going to do is we're going to first of all printout the
state we are currently in.
So I'm going to say the phone is currently and then our current state.
And that's why we have those wonderful overloads of the well not all the loads we have these teams up
with operator for the members.
And then of course I'm going to actually select a trigger for me to simulate.
Now the way I'm going to do this is I will specify select trigger label here I actually move this.
No I don't suppose the line is going to like that but I'll move it back here.
So we are asking the user to select a trigger
and we can now find the triggers for the current state.
So let me make a counter with zero hand for item in rules for the current state.
So I find all the rules and each of those rules is a pair effectively so I'm going to print out the
index which is a plus plus so I will have the numbered with a dot here and I will print item first which
tells me the trigger that I'm going to be using to actually go somewhere.
So this is a list of triggers.
Item The first is that trigger an item that second is the state that you can transition to some bizarre
coloring here from sea line.
But yes it is an endless loop at the moment so I guess it's correct to sort of complain at me like this.
So what we're going to do is we're going to read that uses input for simulating the kind of transition
that we want.
So it inputs and then see an input and then if input is less than zero or input plus one is greater
than the number of rules available here rules at current state that should be rules not results rules
at current state current with the current state DOT size.
Now all we can do is we can complain we can say that this is an incorrect option.
Please try again and we can jump back to select trigger label so we can use the evil go to here that
would go OK so we change the current state assuming that the choice was correct.
We change the current state to wherever the transition takes us.
Rules have current state at inputs dot.
Second sorry that should be like this.
So we change the state to whatever the trigger took us to.
And then if the current state is equal to the exit state then we break the loop.
That's the only condition when we break the loop and this is where Sea-Lion stops complaining at us
for having an infinite loop and then we can say that we are done using the phone.
We aren't done using that we go.
So this is our scenario our state machine indeed so we can now run it and see what exactly we get here.
All right so let's have some fun with it.
So this is actually an interactive demo.
What I'm going to do is I'm going to dial a call which is zero.
And the phone is currently connecting so I can simulate the call as being connected.
And now the phone is currently connected so I can leave a message I can hang up or I can be placed on
hold so if I'm placed on hold for example the phone is currently on hold I can be taken off hold for
example and at some point I can leave a message for example and then I can put the phone on the hook
to stop the whole thing and that's where we exit the loop and we are essentially done.
So this is how you can very quickly hand roll your own state machine by simply defining an set of states
as an interim set of names basically defining Similarly a set of triggers and then just defining a map
which kind of relates the rules for going from one state to another state using a particular trigger
and then you orchestrate that state machine and you get your results.