In this lecture we're going to talk about the boost.
Iterator facade so the boosted thereto facade is quite simply a very useful base class that you can
add to an iterator very quickly and intuitively define the operations which make up that iterator.
So let's take a look at how this works.
Now for the data structure that I intend to iterate I'm going to create a simple singly linked list
where each node is going to contain a single string.
So here is a structure called node.
So all have a value that I'm going to store and I'll also have a pointer to the next element initially.
Now PR.
Now let's make a couple of constructors I'll have a constructor which initializes just the value.
I also have a constructor which initializes both the value.
But in addition to that what we're going to do is we're going to pass in the parent of the current node
and I'm going to assign the parent.
So let me get rid of this because that's not what we're doing.
And we go into say parent next is this and this way we kind of fluently build the overall node sequence.
OK so let's suppose that I now want some sort of iterator Let's suppose I have this singly linked list.
I want a list iterator which just goes through every single element.
What I can do is I can define a structure called list iterator and I can inherited from boost.
Iterator facade.
So in the interface there facade I specify the type.
So it's a CRT thing going on here so I specify a list iterator.
But in addition I also specify the kind of element I'm iterating in this case.
I mean creating a node and I can also specify what kind of traversal I'm doing.
So in this case it's going to be a forward traversal because that's the only kind of traversal we can
do for a singly linked list.
So here is my structure.
I will have a node point at current initialized with our PVR.
If you remember our previous implementations of a tree traversals that's why you have that pointer to
the current element.
I'll also make some constructors so I'll make an empty constructor as well as a constructor which actually
takes the current element to be assigned.
So both of them go me.
And the one where we assign the current element and then I'll have lots of private implementation details
so I'll have a French class boost.
Iterator or access.
And then what I'm going to do is I'm going to define certain operations which are required by the iterator.
So let's see if we can actually get them in the override here.
So as you can see the set of overrides.
Let me just scroll this back a bit the set of overrides is not particularly exciting.
You have operator store operator plus plus we're not interested in these actually and it's interesting
that Sea-Lion hasn't picked up on the other ones.
So what we're going to do is we're going to just define a couple of functions which the iterator kind
of expects us to have.
So one of those is incremental.
And that's where we move from current to current next.
Now notice this isn't a virtual override or anything like that.
So the iterative facade itself it doesn't mandate that we have these.
But if we do have these is going to use them for iteration.
So it's kind of like a kind of convention over configuration I guess we'll also have a function called
equal which is going to return a boolean So we're going to compare it with another cons list.
Peter or other and here what we're going to do is we're going to return other current equals current.
That's how we compare them just by comparing their contents and finally we'll have a dereferenced function
so we'll have a function called the reference which is going to return a node reference.
And here I'm just going to return Star Trek though ago so essentially just by implementing these functions
what I've essentially acquired is an iterator for a single linked list and I can start using it.
So let's set up a scenario so I'm going to have a couple of nodes.
Let's have a node called alpha with the string Alpha.
I'll have a node called Vader with beta and it's going to it's parents is going to be alpha and I'll
have another node called gamma and it's parent is going to be beta.
And when I say parents I mean the previous element of course since it's just a singly linked list nothing
more.
OK so having set up the sequence what I can do is I can do a for each for example and here I can.
Well in order to do a for each I guess I need an include for something and I guess I don't have the
algorithm yet so let's go into headers and just stick it in here.
Include algorithm that would go OK.
Coming back to mean CBP we can now legitimately do a for each and what I'm going to do is I'm going
to define the iterator as the list iterator so list iterators starting at the element Alpha the terminating
element for the for each is just an empty list iterator like so.
And then for each of the elements I'm going to take it.
Konst no reference.
And I'm going to basically just print it out.
So we're going to see out and value and line again.
That's all that we are going to do.
So we can now try running in this and seeing what we get here.
And as you can see the iterator is working just fine.
So essentially the iterator facade is just a helpful construct for helping you to build iterators over
your particular types.