Easy singletons in C++ – VSSynthesiseSingleton

Easy singletons in C++ – VSSynthesiseSingleton

posted in: blog | 0

If you’re like me, you probably use a few singletons in your code. Regardless of which language I’m programming in, be it C++, C#, Objective-C or even GameMaker… singletons are a useful way to hold information and perform operations on said information. For example, on most of my games I like to have a DeviceManager singleton (interfaces with the device for multiplatform code, whilst holding frequently used information), a SaveDelegate, ControlDelegate and so on, as required.

In Objective-C, a particularly useful macro (that is apparently obsolete now) is CWLSynthesizeSingleton. With just two lines of code you can turn a class into a singleton, with the macro inserting the required lines of code during compile time.

When I develop in C++ I’ve always found I miss this little macro and the simplicity it brings. So I’ve decided to develop its C++ incarnation. It implements the singleton pattern found here, in an easy to use macro. Do note that this singleton pattern is not thread safe! 

Using this code in your project:

Simply import VSSynthesiseSingleton.h into your class header, and call VS_DECLARESINGLETON( _classname_, _accessorname_ ); where _classname_ stands for the actual name of your class, and _accessorname_ stands for the accessor name you wish to use for your singleton. Some example code:

MySingleton.h

#include "VSSynthesiseSingleton.h"

class MySingleton
{
VS_DECLARESINGLETON(MySingleton,instance);

public:
int derp;

private:
int herp;
};

You also need to include some code in your class implementation.

Call VS_SYNTHESISESINGLETON(__classname__,__accessorname__); on your implementation file. Because of the way it’s built, VSSynthesiseSingleton needs to hide your constructor and destructor from you. You therefore need to implement two functions which VSSynthesiseSingleton declares for you in case you would like to use a constructor or a destructor. The functions are void _classname_::init() and void _classname_::destroy(). These get respectively called the first time your singleton is initialised, and if it is destroyed. Example code:

MySingleton.cpp

#include "MySingleton.h"

VS_SYNTHESISESINGLETON(AnimationManager,instance);

//Your new constructor
void MySingleton::init()
{
//Code goes here.
}

//Your new destructor
void MySingleton::destroy()
{
//Code goes here.
}

Now that you know how to use VSSynthesiseSingleton, all you need to do is download it! It’s free and open source, so modify it and pass it on! <3

Get it from GitHub:

VSSynthesiseSingleton GitHub Repo

Hopefully it’ll prove useful for people. Admittedly, this might be a bit too hackish for some people, but it seems to work fine in my projects and if you’re the type of person that is always googling singleton patterns (like me), then this might just be for you. :)


Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home1/ugriffin/public_html/vetragames/blog/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273