Now seeing how we're talking about returning HTL from C++ Wouldn't it be really cool if you could actually
write a DML right inside your C++ code.
Well unfortunately this isn't Visual Basic for you.
You can do this kind of thing but what you do is you can sort of approach it so in a way you can construct
a domain specific language which allows you to construct H.M.S. like constructs in foulard C++ using
uniform initialization syntax.
So that's exactly what we're going to take a look at in this demo.
And for this demo what I'm going to do is I'm going to define a truck took hold a tag.
So this is once again this idea of having an HMO element this time I'm going to call it tax just because
it's shorter but the idea is roughly the same so you have a tag which consists of a name and text you
also have a vector of children which can be of the tax inside this tag and in addition you can also
have a team attribute so that's the new thing.
So I'm going to add to this particular demo so I'm going to have a vector of pairs of strings which
indicate the key value Paris for the male attributes and I'm going to call them attributes like so now
what we're going to do is we're going to take a look and how to actually use this tag as a base class
in order to allow the Clines to actually write something which looks a lot like a DML.
So first of all I will add the output operator.
So the stream output operator that's going to be useful for actually princeling the whole thing.
And it is a rather involved chunk of code so I'm just going to drop it in here and we're not really
going to discuss it.
So let me just say that we define the stream output operator on the task to print all the tags including
all the children and all the content correctly.
That's what this chunk of code does.
Now the interesting part comes after.
So suppose we have the tag we're going to have a couple of constructors I'm actually going to make them
protect that.
So we're going to have just a couple of constructors.
First of all we'll have a constructor which initializes the tag using the name as well as the text.
So we're going to have this as you can see I've generated it here but in addition we're also going to
have a tag which gets initialized by its name but instead of the text is going to have a bunch of children
so I'm going to once again go and generate a constructor so this time round we we're interested in the
name and the children.
I'm going to press OK here once again.
And as you can see what's happening here is we have two arguments we have the conn string name the name
of the tag as well as a vector reference for the children.
So this is great.
Now if you think about a CML it has concrete implementations of different tasks like for example a paragraph.
So we're going to define a paragraph as an inherited class.
I'm going to have struct p p stands whole paragraph and that's going to be an HMO tag.
Now what we're going to do is we're going to make a constructor which initializes both the name as well
as the text.
Now we know what the name is the name is going to be p because that's what the tag actually is.
But when it comes to tax we'll simply proxy that over so let's see what we can do here and in terms
of generating the constructors so we're going to have a constructor which initializes both of these.
But instead of the name here what I'm going to do is I'm going to put the tag in here so I'm going to
put just a lowercase P in here.
And I'm going to get rid of the name argument because we know what the name of this particular attack
is.
OK so this is great but we can do something better because a paragraph as it happens can also have some
children of its own.
Maybe you have an image inside a paragraph that's a completely valid thing to have an image inside a
paragraph.
So that's bad.
So the way you would do this is you would have a paragraph which can be initialized not with the text
but with a bunch of children.
And what I'm going to do here is I'm going to use and initialize the list.
So I'm going to use an initialize a list of type tags so this tag the tag P is going to take in its
constructor a bunch of other tags.
So here we go again to have a bunch of children and these children they can actually be assigned to
a vector.
Surprise surprise you can take an initializer list and stick it into a vector wholesaled.
So once again we're going to call the base class which in our case is called tag and we're going to
call it with the name P as before and the second argument is going to be the children.
So essentially what's happening here is when you provide the children as an initializer list it actually
goes in here as a vector.
So fairly obvious stuff when I'm going to add anything more to the tag.
The tag is just fine.
Let's add another tag just to illustrate the concept even more.
We're going to have the image tag.
So an image tag is a tag which doesn't really contain and you think and because it doesn't contain anything
we're actually not going to give it a constructor which takes an initializer list which is going to
give it a different constructor which specifies the role of the image that you want to show.
So we'll have a constructor called AMG which is going to take a string specifying the you Arel of the
image.
And of course the you l is specified as an attribute.
No not as an inner tag or anything.
So once again we call the base class tag.
The name is item G and we have to use the constructor which takes the text but there is no text.
I'm just going to put an empty string in here.
OK.
But we do need to put something in the constructor itself.
And what we're going to put in here.
Let me just move this so that it's more clear what we're going to put in here is an instruction to add
the natural beauty so I'm going to say attributes that emplace back.
And I'm going to make a pair here where the name of the attribute is S R C that's how you make the source
of the image and of the actual source is the U R L that's being provided.
OK so let's put the semi-colon in here.
OK.
So why did we do all of this.
Well because we've just built a very unique and very unusual domain specific language if you will which
allows us to construct h t m l which looks like a l.
Let me show you so you can have a let's say you have C out and then I'll just do a couple of line breaks
so you can make a paragraph and then things to use the form initialization.
You can just open the curly brace in here and have the closing curly brace in here and inside the paragraph
you can have an image.
And once again thanks to you in the form initialization you can just specify the image you are over
here.
So it would be something like HTP Pokemon dot com Pikachu top and G something like that.
So we have a paragraph and we have the image inside it.
And I will now add maybe the headline here as a kind of finishing touch and we can actually print this
stuff out interestingly enough because all that we have written right now is valid C++ even though it
looks like almost like a DML without the without the obviously the actual tags as they look in a stamp.
But it's very similar to what we have is very similar.
And here is the output.
So as you can see we have a paragraph and we have the image inside that paragraph.
So all I'm trying to show here is that thanks to just a single mechanism of uniform initialization and
C++ what you can do is you can construct these kind of pseudo D-cells.
And furthermore it's interesting to note that you can actually constrain what elements go inside what
other elements by defining different constructor zone.
All of these elements are for example the image cannot contain anything else it cannot contain other
tags so it simply doesn't have a constructor which takes an initializer list.
So this is a small aside to the overall discussion of the builder concept but essentially it's very
similar because you're using or abusing depending on how you look at it where abusing uniform initialization
to define structures in a more natural more understandable way.