• Advertisement
Sign in to follow this  

Generic Singleton

This topic is 4867 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
Can you show me an example derived class and how to use it

I'm confused still :(

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
Quote:
Original post by silvermace
does intellisense still work on that???

Yep. I'm using VC++ Express and Intellisense does indeed pick up on it alright.

Share this post


Link to post
Share on other sites
I'm trying to use this Singleton implementation. I have a separate static library (Linux) that contains a Reactor class, which is derived from Singleton. When I try to link to the library, I get undefined reference errors for Reactor.

make
#g++ -shared -o libnet.so Port.o Reactor.o Connection.o
ar rcs libnet.a Port.o Reactor.o Connection.o
g++ -c server.cpp
g++ -o server server.o -Lnet
server.o(.text+0x80): In function `main':
: undefined reference to `Reactor::getSingletonPtr()'
server.o(.gnu.linkonce.t._ZN9SingletonI7ReactorEC2Ev+0x8): In function `Singleton<Reactor>::Singleton[not-in-charge]()':
: undefined reference to `Singleton<Reactor>::ms_Singleton'
server.o(.gnu.linkonce.t._ZN9SingletonI7ReactorEC2Ev+0x41): In function `Singleton<Reactor>::Singleton[not-in-charge]()':
: undefined reference to `Singleton<Reactor>::ms_Singleton'
server.o(.gnu.linkonce.t._ZN9SingletonI7ReactorED2Ev+0x8): In function `Singleton<Reactor>::~Singleton [not-in-charge]()':
: undefined reference to `Singleton<Reactor>::ms_Singleton'
server.o(.gnu.linkonce.t._ZN9SingletonI7ReactorED2Ev+0x35): In function `Singleton<Reactor>::~Singleton [not-in-charge]()':
: undefined reference to `Singleton<Reactor>::ms_Singleton'
collect2: ld returned 1 exit status
make: *** [server] Error 1


The Singleton class is in a header file. Any suggestions as to what I'm doing wrong?

Thanks

Share this post


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

  • Advertisement