 Hey everybody, this is Brian. Welcome to the 120th tutorial with cute and C++ programming. Wow 120 of these things Gonna not do a lot of typing in this tutorial because I'm very tired. I Tell you a little story children. I Work as a server administrator network administrator for a company. I'm not actually a programmer That's not true. I've been a programmer most of my life, but my bread and butter comes from babysitting a network Where I live for whatever reason there's not a lot of computer jobs and the ones that are just real low-pay and you don't want them anyways, um The we had a water pipe break in the server room and the entire building flooded So we've just been ripping out drywall and having a good old time while running a business on top of it so Wanted to do a quick tutorial, but I don't have time to do it over and over and over and over again a lot of times when I Do a video it takes me, you know, eight nine ten tries to actually get it right And I just don't have the patience for video editing software. I just don't so anyways today I Kind of wanted to point out that we have broken the 3.5 million views and 22,758 subscribers. That's a Lot more than I ever expected I started doing these videos for my daughter who completely lost interest in everything except for Facebook and all the things that teenage girls like I still program and I'm working on a few projects So if I had a dollar for every one of those views or even for one of your one of those subscribers What would I do with it? I'm glad you asked. I would put it into my website void realms.com You know complete sales pitch here you can find the complete source code for all the tutorials Including this one, which I'll be uploading here soon So you can head out there and click on tutorials and then choose the language in the search for whatever you're looking for So if you wanted like, I don't know a ES encryption. Well, I thought I had that Encrypt Yeah, there we go. How to encrypt sometimes. I'm not very good at filling in the The descriptions but you get it This tutorial would be talking about singleton patterns Which is where you create one class and you guarantee to only have one instance of that class running at any given time It's part of the plug-in bot.net project. Be sure to visit plug-in bot.net It's my newest pet project and this code is coming directly out of that project. So Let's just jump right in first off. Why do you need this? Why would you ever want one instance of your class? Give an example here. We've got a simple program and you can see here's our main function We've got in test age and consumer age and they're just very simple classes. Here's test You have a static Function that does something literally called do something and there are many reasons to use a static method But if you skipped your C++ primer Static method exists without the class meaning you don't actually need an instance of this class in order to execute this code Hmm And we also have a signal now the problem is we want to call this signal from within that static object And so we here we have it Tests do something remember this is static and we're meeting a signal What happens if we try to build this? boom Cannot call member Test signal without object. So we actually actually create an instance of this object. The problem is It's a static method so it exists without an object exists separately from the object or any instance of the object Hmm, so how do we actually call that? Well, you could try calling it as a normal function But you're gonna get pretty much same thing need an object So we're gonna get around that comment this out I Pulled the singleton class from the plug-in bot net project thought I'd share this. I actually Didn't really write this class. I think I borrowed heavily from somebody out on the net So this is your class. Hey, let me know. I forgot who you are But I think I read your tutorial somewhere and this came in very handy So I wanted to show other people how to use it. I'll give you kudos out on the YouTube page I did rewrite parts of this to suit my needs. But anyways, you're going to First include a cert H and Then we're gonna make a template now If you skipped your C++ lectures on templating you should pay attention because it's gonna be very important a Template is well a template. They kept it pretty simple meaning you can throw anything you want you can throw a string and integer Whatever, we're just saying a class and it'll be a variable called T Just we could call it fuzzy bunny slippers if we wanted to but T is the standard the class is singleton and You can see whoopsie Outside of our class. We have a template. We'll get to that in a second So in here, we have one public static function called instance where we're saying pointer to T that class If there's no instance, which is just a static, you know pointer to a variable Then create a new one. This is templating seeing that class is gonna be replaced by here So if we used a I don't know a test H class will actually be converted to test It's the same thing as saying new test All right, so then we're going to assert hence the assert.h We're gonna assert that is not null and then return the object So what we're saying is if it already exists, it's gonna skip this line And she's gonna make sure it's not null and return it if it doesn't exist It's gonna create a new instance assert it and then return it Like I said, I'm tired so I'm fighting to stay awake here all right now we've got our constructor and deconstructor and then we've got a copy constructor and a operator overload I can write a FTP server from scratch But I still try to wrap my head around operator overloading. I just for whatever reason it just It haunts my dreams all right now basically all we're saying is Make a car if we are making a copy of it, then just you know use the reference Otherwise if it's equal, then we're using the reference I'm sure I'll get a million emails saying that's not how that works. I look forward to those emails Then we're making a template and we're saying the class T. Here's our template Point or two singleton of the template and then we're returning the instance variable and it's Initialized as null That's a mouthful. So how do we make this thing work and what do we need it for? Remember our original problem is we have a static method for whatever reason you want to use a static method And we want to admit a signal from that static method In our consumer class. Let's actually go here We've got a slot which matches the signature test signal So we need to line these up here First thing we're going to do is go into test and we're going to say include singleton.h Now outside of our class here We want to create what's called a type tough in the global namespace If you fell asleep during your c++ class, don't worry. I did too Um type def is just short for type definition meaning you're creating a new definition here We're going to call it singleton And we're this is a template. So we're going to use the test class If you remember from the singleton, it's looking for a class So we're going to give it a class test And we're going to call it tester So we have a type def which is this Called tester out on the global namespace. Now if we want to simplify that we have a variable On the global namespace called tester And the variable is just a singleton of test. You've done this before with like q-list Where you've done a q-list of q strings That's really all it is No real magic in the background now because that's on the global namespace. We can do some interesting things here personally and professionally Bad things happen when you make a global variable. Let's actually call it a global variable Because you're cluttering the global area You have all this junk flying around and it's hard to track. So it's It's bad design to do that, but sometimes it's necessary and this is one of those instances So In our test class we've got that and We've still got this we're going to fix that in a second, but I just wanted you to be aware This is the problem Creating something without an object now in the consumer class We're going to actually say Let's see here Sorry, I'm kind of zoning out. I told you I'm kind of tired here We're going to connect our signals and slots Now how are we going to do that when we don't have an object? Oh contrary, we do remember We made it out in the whoopsie The global namespace, but we have to include it here No, I don't want terminal IO. That won't help us. I want test I keep having that phrase in my head water water everywhere not a drop to drink. So notice how in the Tester and we use the namespace We get instance, which is a pointer to test meaning the instance that we're guaranteed to get That's our singleton pattern right there. Now our test signal Connected to this slot test signal Should call that test slot. Let's actually do that just for for giggles I'll have to go in here and fix this real quick And of course in here and fix this real quick. So that way it reads a little better. So we're connecting the pointer to the test class in memory called tester And we're connecting it to all the consumer on the test slot So when this is called we'll debug. Yeah Nothing's going to happen if we run this because we've got this commented out. Let's give it a build now Just for reference if we try to run this Still need an object. So how do we get around this? simple tester instance And we're going to say test signal When you call a signal Cute treats it as a normal function, but it does some magic in the background and actually emits it for you Amit just tells it. Hey look for this signal So we're just going to call that There's no need to actually say Inmit I'm not even sure if that would work, but we could try it So when we call this we're going to say tester our global variable We're going to get the current instance, which if you remember from our single pattern here singleton pattern here We're just saying get the variable that's out in Any static area? Static is kind of a weird area. It puts it Kind of like out on the global would not really it's kind of kind of shifty There have been some debates some people say yes, it's a global. No, it's not but we're not going to get into that We simply want to call this test signal from our static function So when we run it Nothing happened Let me see if I did this right here It would help if we actually did something with us, right? So we need to go I told you I was very tired tester instance Do something So we're calling our static variable now. You notice how we're actually not calling static. We're calling it from the object I want to make that differentiation right here. So I don't get a million emails From the object We'll run this and we get yay. It works now As I said, we're not actually calling it from the static function. Let's fix that This is how static functions work Notice how you're calling the test class the function in it. So we're calling the do something static function We don't actually have an object and it still works Yay Just to prove that that works. Let's actually say um Consider if I could spell it's working So quick overview of how this works and why it works Okay We've got our test class with a static function Sometimes you want a static function most of the time you don't Remember a static function will live out in the global area. You don't actually need A class or I should say you don't need an instance of that class. You need the class itself, but not an instance Um that gets into the hole isn't an object or an instance conversation and then we'll start a big war on the forums But um, we want to be able to emit a signal from that, but we don't have an object to emit from So we of course use a singleton pattern using the test class And create a global variable called tester From that we can actually Make our call and you see in the do something function, which is static We're saying tester get the global instance or the global The global variable sorry and then call the function test signal now that function is actually Drum roll please a signal That's the magic of cute in the background right there That's why in every book they say always include this q object because That's what happens in the background as it does all this linkage for you All right, so Real-world practical practical applications of this. What would you possibly use this for? Well, I was working on a key logger for plug-in bot.net And it uses what's called a hook Where you hook the keyboard process and windows So every time a key is pressed windows notifies you I believe it's called an in in proc or an in-memory hook Anyways, um, it has to be a static variable while I wanted to emit a signal saying hey a key was pressed It's the chicken or the egg you can't you have to use static function to get the hook But you want to you know use you get where I'm going with that. Okay So once again a singleton pattern um is a method of creating a Single instance of that object and you want that to persist out in the global namespace Another real-world example would be a logger. That's going to write to a file or a tcp server But you don't want to create two servers listening on the same port one of them is going to die So you want to make sure that you have a single instance? Well, that's all for this tutorial. Um Thank you for watching. I hope you found this educational entertaining. Um This maybe wasn't so much about cute as it was about me just rambling because I've had a long day and more about like c++ but um Be sure to visit my website Void realms.com for the source for this tutorial and others and also check out plugin bot.net. It's in beta But um, I'm hoping it does a lot of good for the world. I want to release it free for schools non-profit organizations, etc, etc really looking for other developers to kind of jump in and Help out with writing some of the plugins. You'll get fame recognition and you know women everywhere will throw themselves at you Joking joking. Well, and if you're a woman because I there are quite a few women that watch these tutorials men will throw themselves at you Unless you don't like men then maybe cats will just follow you around because I have like four cats staring at me right now All right. Well, that's all talk to you later