Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WebsiteWill

Can Singleton be abstracted?

This topic is 5426 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So that if I have a class that I want as Singleton, all I have to do is cMyClass:cSingleton? Not sure if this will work because cMyClass will inherit the static singleton pointer from cSingleton so that if I also do cMyOtherClass:cSingleton then they will actually have instances to the same pointer. I was aiming at an abstract derivable cSingleton class but now I think that I''ll have to settle for simply repeating the singleton specific code in all of my classes that have to be singleton. Unless there is another way. Thanks for any input, Webby

Share this post


Link to post
Share on other sites
Advertisement
I think there is a way to do that using templates; the syntax looks something like cMyClass : cSingleton. I have no idea how the cSingleton class looks, though.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Havnt check this by compilation, but id guess something like this


template<typename T>
class singleton_base
{
public:
singleton_base(){}
singleton_base& operator =( const singleton_base&){}
singleton_base(const singleton_base&){}
~singleton_base(){}

static T& instance()
{
static T theinstance;
return theinstance;
}
};

//note singleton_base isnt a singleton

class the_singleton : public singleton_base<the_singleton>
{
public:
...
private:
the_singleton (){}
the_singleton& operator =( const the_singleton &){}
the_singleton (const the_singleton &){}
~the_singleton (){}
};
//the_singleton is the singleton, so it need private constructors


you then access the instance by calling the_singleton::instance();

Share this post


Link to post
Share on other sites
Rethinking this design ATM. My goal of singletons was so that I could create a cGraphics object that would control all initialization of DirectGraphics including things like screen resolution, color depth, etc and would provide the interface for minimizing and maximizing the screen etc. I was thinking singleton would be the way to go since other objects might need to create an instance of this class since objects like cObjectManager will be using cGraphics to obtain necessary pointers to the single d3d, etc pointers in the game.

Now I am thinking that it may be cleaner and easier if I just encapsulate these objects into a main cApplication class as static members. This way, there would still be only one copy of each at a time, I think.

Would this work?


class cApplication
{
public:
bool Init();
void Run();
etc...
private:
static cGraphics myGraphics;
static cInput myInput;
static cInterface myInterface;
static cSceneManager mySceneManager;
etc...
};

I have issues understanding this. Say that mySceneManager contains the functionality to have each object in the game Render() itself. Or provides some way to batch render multiple objects at a time. This class would require pointers to the d3d device, etc so how do I cleanly give it a pointer to myGraphics so that it can access these devices?

I don''t want to store a copy of cGraphics inside of the cSceneManager because this would eat up more memory and a lot of code space. The cGraphics would have a method something like
LPD3DDEVICE GetDevicePointer() that should be called to retrieve a pointer to the device. I definitely don''t want to declare myGraphics as a gobal object. I''d like a way for mySceneManager to access this function as necessary and still have only one main copy of the cGraphics object. I thought Singleton would do this but I am reading that it could be more headache than it''s worth. Hope I''m being clear enough to understand here.

Thanks for any help,
Webby

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could probably get away with making singleton_base constructors protected too.

Share this post


Link to post
Share on other sites
Your Singleton "class" would actually be a meta-class, i.e. the class of a class. Its objects would be Singleton classes.
C++ doesn''t directly support user-written metaclasses (Python does ), its metaclasses are hard-coded into the compiler : basic type, array, class types (struct, union, class).

Thus singleton type creation cannot directly be automated (AFAIK).
You can however, use templates to create classes that will contain a single instance of another class (a SingletonHolder).


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Something told me that this part would get overly complicated.
Back to the static classes Those are much simpler and probably faster anyway.

Thanks all,
Webby

Share this post


Link to post
Share on other sites
i believe there is an article in the "articles and resources" section about making a singleton template, which sounds like what you are looking for. sorry, but i don''t have a link.

Share this post


Link to post
Share on other sites
Thanks krez. I did some searching and found this article.
http://www.gamedev.net/reference/articles/article1826.asp

It''s good for templated managers but not for templated singleton. I can see how the templated singleton would work though. I''m going the way of the static members as this IMO is cleaner and easier to comprehend when looking at it. I''ll definitely be putting Manager to use in my code but so far it seems as though I can do without an actual Singleton design just fine.

Thanks again,
Webby

Share this post


Link to post
Share on other sites
Check out Richard Fine's Enginuity series here on GameDev-- specifically part II, page 5. He shows how a templated singleton can be implented like an abstracted base class (with source).

Kudos to Richard because it's a great series. I can't wait to see more.

bpopp (bpopp.net)

[edited by - bpopp on November 13, 2003 3:46:09 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!