Jump to content
  • Advertisement
Sign in to follow this  
ph33r

Generic Singleton

This topic is 5039 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

Hey, I understand singleton's but I've been trying do write a base singleton class so that any class that needs to be a singleton I can just derive from. But I can't figure it out without abusing macro's. Is it even possible without macro magic :P

Share this post


Link to post
Share on other sites
Advertisement
Use templates! :)


/* Copyright (C) Scott Bilas, 2000.
* All rights reserved worldwide.
*
* This software is provided "as is" without express or implied
* warranties. You may freely copy and compile this source into
* applications you distribute provided that the copyright text
* below is included in the resulting source code, for example:
* "Portions Copyright (C) Scott Bilas, 2000"
*/



template <typename T> class Singleton
{
protected:
static T* ms_Singleton;

public:
Singleton( void )
{
assert( !ms_Singleton );
int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
ms_Singleton = (T*)((int)this + offset);
}
~Singleton( void )
{ assert( ms_Singleton ); ms_Singleton = 0; }
static T& getSingleton( void )
{ assert( ms_Singleton ); return ( *ms_Singleton ); }
static T* getSingletonPtr( void )
{ return ( ms_Singleton ); }
};


Share this post


Link to post
Share on other sites
Sure thing :) I'll just show a basic shell of a derived class.

Header:

/* LOGFILE.H */
class Logfile : public Singleton< Logfile >
{
public:
Logfile( ) { } // initialize here in your constructor.
~Logfile( ) { } // destructor.

/* PUT THE CLASS FUNCTIONS HERE */
void YourFunction() { } // Fake function.

/* Singleton accessors */
static Logfile& getSingleton( void );
static Logfile& getSingletonPtr( void );

private:
/* Deny the copy constructor and assignment. */
Logfile( const Logfile &logfile ) { }
Logfile& operator=( const Logfile &logger )
{
return *this;
}




Source:

/* LOGFILE.CPP */
template<> Logfile* Singleton< Logfile >::ms_Singleton = 0;

/* Methods needed for singleton. */
Logfile& Logfile::getSingleton( void )
{
return Singleton< Logfile >::getSingleton();
}

Logfile* Logfile::getSingletonPtr( void )
{
return Singleton< Logfile >::getSingletonPtr();
}




To use this object:

new Logfile(); // Create the object for use.

Logfile::getSingleton().YourFunction(); // Function calls.

delete Logfile::GetPtr(); // Delete the object at shutdown.




I hope that clears things up for you.

Share this post


Link to post
Share on other sites
i like to define my singletons to make it easier to call them:

#define SceneManager CSceneMngr::GetSingletonPtr()

class CSceneMngr : public Singleton <CSceneMngr>
{
...
};

Now I can just do

SceneManager->SomeFunc();

I got that and the singleton class I'm using from Game Programming Gems 1

Share this post


Link to post
Share on other sites
Quote:
Original post by Gaiiden
i like to define my singletons to make it easier to call them:

#define SceneManager CSceneMngr::GetSingletonPtr()

class CSceneMngr : public Singleton <CSceneMngr>
{
...
};

Now I can just do

SceneManager->SomeFunc();

I got that and the singleton class I'm using from Game Programming Gems 1
does intellisense still work on that???

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!