So there isn't one more thing that I want to show you.
With regards to the memento design pattern and this thing is actually very important because as you
can see I've opened a real life project here and I'm going to show you how mementos can be used in an
automatic way so what do I mean by automatic What is automatic memento all about.
Well typically the examples that I've shown you are examples where you explicitly take the memento and
you stick it whenever you want to actually restore the system to a particular state but you can also
have a memento which causes the restoration of the state in its destructor.
Now the question is why would you want to do that in the first place.
Well sometimes you do actually want to do this and it's actually rather common case I see it all the
time so that's something that we're going to take a look at.
Now here you'll notice that I have something interesting I'm building just a type.
I'm building a C++ object out of separate strings.
Now at some point what I want to do is I want to perform indentation but I only want to perform indentation
between this opening curly brace and the corresponding curly brace here.
So how can I do this.
Well in actual fact you'll see this statement here up above.
And it's a very curious statement because the variable S which I am declaring here is not being used
anywhere.
So I'm declaring a variable that's virtually useless.
Why am I doing this.
What's going on here.
Well let's actually take a look at this function called scope.
This function is interesting because it returns something called a scope token.
Now another word for a memento is a token so you can see that there is some similarity here.
Now what's really happening here is that we're returning a particular object which has not just a constructor
but also the structure and the idea is that this scope token is using a printer.
It has a reference to the printer that we were actually using to print some code.
And in the constructor we change the indentation for example.
And in the structure we return the indentation back.
We actually manipulate indentation strings if you're interested but it doesn't really matter.
The point here is that this is a kind of a memento which does something in its constructor and then
does the reverse.
In our case it's the structure and then we construct this memento inside a scope.
So we explicitly make a scope with the curly braces as you've seen here.
So if I just get us back to where we were I'm using curly braces here so I'm constructing this object
this memento if you will which actually modifies the state of the system to increase indentation there
by getting two additional spaces on the left of the file that I'm generating.
And then when the scope is exit the variable x is subsequently destroyed the destructor fires and the
indentation is back to normal.
Now you're probably wondering.
Well hold on.
Can't you just you know do it like this.
Well unfortunately I cannot because this whole thing is going to be well it's not going to be stored
anywhere.
That's the problem.
So if I just call scope true that will construct the object and immediately destroyed so the destructor
instead of being invoked at the end of the scope the destructor will fire right about here.
So you would construct an object and then the destructor will fire here which is completely unacceptable
because we want the indentation to last for the entire scope.
So that's why unfortunately if you want to use a memento which has a destructor which does something
meaningful you have to declare some sort of variable.
And in this case it's an unused variable but it still doesn't get applied it doesn't get destroyed automatically
by the compiler.
The compiler is not that stupid although sometimes I feel it's undermining me.
And here we make this variable and we just stick with it we don't use it for anything we just have the
round so that whenever we reach the end of the scope these terracotta are all of our a memento FARs
and the indentation goes back to normal.
So this idea of having a utility function is once again just just just a helper for us to actually manufacture
a particular token.
You'll notice that inside our printer what we're doing is we're passing a token reference to ourselves
because it has to manipulate the printer itself internally you can see that it's doing that here and
here obviously.
So this is how you can construct a memento which instead of being explicit.
I mean this isn't a memento in the classical sense of where you feed the scope token to the system to
restore to some state there is actually there is no real preservation of state as such because it's
all happening on an external type called P on an external printer.
That's where you add to the indentation.
So here I simply build the indentation Char and I add that to the indent so it becomes larger and then
I reduce it by using substring here so becomes smaller.
So it's not the case that I'm using this memento to return this system to an explicit state which is
stored inside the token itself but the state is simply modified in the constructor and then modified
again in the destructor.
So this is an interesting alternative to the memento design pattern and one that's actually used in
code mean lots of different places so you make a token with a constructor and a destructor which performs
some modifications on some object.
You then maybe make utility function which actually generates this token inside the object that it's
supposed to be effecting in this case are have a scope token here I have a namespace token which is
similar but the prefix is the scope by the word namespace and that's curly braces to it and all the
rest of it.
So these kinds of things are very cool and very useful in kind of real life development.