Usually singleton classes involve quite a bit of boilerplate code to retrieve and implicitly create the corresponding instance, like for example:
classMySingletonClass
{
...methodshere...publicstaticvar__instance:MySingletonClass;
publicstaticfunctionget_instance():MySingletonClass
{
if (MySingletonClass.__instance==null)
MySingletonClass.__instance=newMySingletonClass();
returnMySingletonClass.__instance;
}
}So all calls to the singleton instance would look like that:
MySingletonClass.get_instance().some_method();
Of course this could also be done using properties, but it just avoids a few characters (two parenthesis) when retrieving the instance.
Thanks to macros we can avoid those boilerplates and repetitious code. Using this library you define a singleton class like this:
classMySingletonClassimplementsSingleton
{
...methodshere...
}This turns all public fields of the class into static functions which automatically work on the right instance. So calls to the singleton instance look like that:
MySingletonClass.some_method();