gamecreature / QtAwesome

QtAwesome - Font Awesome for Qt Applications
Other
787 stars 149 forks source link

Singleton class #28

Closed lp35 closed 1 year ago

lp35 commented 6 years ago

Hi,

In your documentation, you said that QtAwesome should be initialized only once. In this case, why not creating a singleton class with static methods?

Thank you for this wonderful piece of code.

Edit: I can make a PR if you think it's a good idea.

gamecreature commented 6 years ago

Thanks for your response.. In issue #16, I explained the reason why I didn't create a singletonclass:

I’ve been thinking about singleton when I created the library. I choose not to use it, because (in theory) you can use other fonts with this library. Also this current construct makes it possible to control exactly when the font is loaded. Also singletons are very limited, which prevents descent inheritance and specialization in subclasses. So that’s the reason I didn’t make a singleton :)

lp35 commented 6 years ago

It's maybe over engineering, what about using CRTP? https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

gamecreature commented 6 years ago

Maybe you can just use your own singleton function in your application?

QtAwesome *qtAwesome()
{
    static QtAwesome* qtAwesome = nullptr;
    if( !qtAwesome ) {
        qtAwesome  = new QtAwesome();
        qtAwesome->initFontAwesome();
    }
    return qtAwesome;
}

I would like to keep the QtAwesome library simple clean and lean. So adding templates for this reason is indeed over-engineering 😉

lp35 commented 6 years ago

Why not adding this to mainstream? :) and changing the name of the function like QtAwesome* fa to enable quick access? It will not change the behaviour of your system. Otherwise yes, this is a good workaround. Thanks!

Dariusz1989 commented 1 year ago

Don't add singleton. It may be an issue with dlls/etc... Unless u can wrap it around in macro so we can disable it. Thanks.

gamecreature commented 1 year ago

I will not add this Singleton. It's so trivial to implement and @Dariusz1989 it can indeed cause issues. I will close this feature request. Cheers!