All right so the scenario that we're going to consider for the singleton is precisely the scenario that
we've discussed when we were talking about the motivation for using the singleton.
So what I'm going to do is I'm going to read some data into a database.
Now I have a text file here called capitals Darty XTi you can see that it has the name of the city followed
by the population of that city.
And we're going to read this into a database and obviously for us it only makes sense to have one instance
of such a database because well essentially you don't want to replicate the information again and again.
So we're going to make a singleton database effectively So here we go.
I'm going to make a singleton database like so and here what we're going to do is we're going to have
a constructor which is private.
So here is the constructor.
It's in the private section and then the public stuff will follow that.
So in the singleton database what we really want to do is we want to get the file so have an input file
stream and then read the data into some sort of map or something.
So once again I'm going to keep that data private as well so here I'll have a map from string to end
and I'm going to call it capitals.
So that the population of the different capitals Obviously I need another include in here for a map.
We have plenty of influence here already.
But we're going to have one more.
So here's the idea I'm going to output just some diagnostic information that we're initializing the
database like so.
And then what I'm going to do is get the input file stream from capitals to XTi and I'm going to read
the data from it.
So let's have a temporary string.
Well actually that's have two temporary strings so String s and s to.
And then I'll be using getline to actually read the individual line.
So while get Line Input fall stream as some going to read the line into ass and then I'll read the second
line so I'll say get a line.
I have asked two so I have both the capital as well as the what I need from the from the population.
And I'm going to actually be converting it into a number.
So for this I'm going to be using Boost So let's have a boost lexical cast in here.
Very convenient kind of library.
So once we get this and you can see the slight delay here it's you to see line indexing the whole thing
and basically as soon as you add the boost header.
Oh off it goes in all sorts of different directions trying to parse or do whatever.
So using namespace boost as well.
OK so what I'm going to do here is once I've got all the lines I'm going to get the population so into
Pol Pot equals lexical cast two ends of as two.
And then I have both of these so I can put them into the map so I can say capitals at s he calls the
population.
So that's it.
That's how you would read the database.
Now of course we made the constructor private so that nobody could be able to just instantiate the database.
Now the question is how exactly are we going to expose it and what are the requirements for making a
safe Singleton So just because you've hidden the constructor doesn't mean that people cannot create
other instances of the singleton database because if they get one instance they can use let's say they
can use the copy constructor or copy assignment so for both of these operations at least what you want
to do is you want to delete them.
Now in the modern plus Bos variety we can use the delete key words so here I can make a copy constructor
so I'll make a singleton database which takes another Singleton database Consta reference and I will
delete this.
And in addition what I'm going to do also is I'm going to delete the copy assignment operator.
So you have operator equals which also takes a console reference and you can delete that as well.
So this ensures that nobody can actually replicate this database in any way shape or form thereby wasting
valuable resources.
Now the question is Well how are people going to actually get the singleton.
So for this we're going to have a static function called get and is going to return a reference to the
singleton database Singleton database.
So there is a function call get.
Now what we do here is we make a static instance of the database so static Singleton database DB and
then we return DVH that's pretty much all that you need to do in order to provide a singleton.
So somebody has to call gouts and they get the appropriate singleton because it's being constructed
statically.
And this is all completely safe and normal.
And then we won some sort of API for actually getting the population from a particular city.
So we'll have a function called Get population will take a string reference the name of the actual city
and here what we're going to do is we're going to return capitals at this name so we're going to return
the actual population.
So we are done with the singleton actually start using it now.
So essentially we can have a go at getting the population of a particular city.
So let's do that so we're going to have our Singleton database and to get an instant solve it you just
say get like so and I can say auto be equals.
Singleton database get.
So this should be more or less legit.
Let's see what's going on here.
Well the copy constructor is deleted.
So I notice I'm using the equals here for assignment but of course you're using other lots to do that.
Unfortunately you can only work with the singleton data directly so you're not going to be able to sort
of perform a copy anywhere.
OK so we have this go then database.
And now we can actually get the population off a particular city like Tokyo for example.
And so for a given city actually let's have a string city equals Tokyo.
And then for a given city what I'm going to do is I'm going to see out the fact that city has population
and then just use the API to gather population.
So here I'll put the city and that's put up and line at the end of it all.
OK.
So this is something that we can actually work with and let's try compiling it and seeing what we get
here.
OK so as you can see we have initialising database and we have the information that Tokyo has population
33 million that's a lot of people.
So this is how you implement the singleton.
And it might look to you as though there is no particular issue here.
There is no particular problem in terms of using the.
But as we're going to see later on it's actually rather inflexible.
We're going to improve this approach in the later lessons.