Singleton

Started by
5 comments, last by bjogio 18 years, 7 months ago
I have what I believe is a correctly setup signleton. Now I want to use it in multiple source files. I know how to do it in two different ways. 1. extern CLog *LOG; //This way everywhere i can use LOG 2. CLog *LevelLOG = CLog::Instance(); // Here each source file needs to have own variable name I like only using LOG but is it bad to use extern? -THACO
Advertisement
With a singleton all you should have to do is include the header file in any files that want to use it. Then get an instance in that file.

This works with a singleton that looks like this:

#ifndef _SINGLETON_H_#define _SINGLETON_H_class singleton{private:	singleton()		{};	~singleton()	{ if ( pSing ) delete pSing; };	static singleton* pSing;	static bool made;public:	static singleton& Get_Instance()	{		if (made == false)			pSing  = new singleton;		return *pSing;	}};singleton* singleton::pSing;bool			singleton::made;#endif


Simple get an instance wherever you need to.

Hope that helps bud,

ace
Well yea, the singleton doesn't seem to be the problem, I am more asking what is a better way to get the instance

1. extern CLog *LOG; //This way everywhere i can use LOG
or
2. CLog *LevelLOG = CLog::Instance(); // Here each source file needs to have own variable name

-THACO
Well i personally don't fabour the use of extern. If your files are class header files then simple have a member variable to store the instance, if they are plain C .C files then i would use the latter option.

ace
thanks, that is a good idea about the member variable for the instance

-THACO
You can do a couple things here.

1) Have your class return a smart_ptr to the singleton instance (so nobody is tempted to delete it)

2) Just use static member functions and invoke them like this

YourLoggingClassName::log("whatever");

the implementation would be something like:

// a static member functionvoid YourLoggingClassName::log(const char *string){   if(_instance) _instance->_log(string); // where "_log()" was a non-static function}


The instance ptr would (of course) need to be static, for both cases to work.

In your main program source file, you can forward declare the static instance... or in another cpp file

YourClassName *YourClassName::_instance = 0;

Make sure that in your constructor, if "_instance" isn't already defined, you assign it to "this"... and if it is already assigned, you may want to throw an exception.


Just some ideas for you.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
I use a pretty system for handling singleton, check this:
template<class T>class SubSystem{public:	static T& Instance()	{		static T Singleton;		return Singleton;	}	virtual void LoadConfiguration(const Configuration& Config) = 0;	virtual void Initialize() = 0;	virtual void Shutdown() = 0;	//details omitted};

in this way you use the functionality of C that is related to static local function variable initialization (initialized the first time the code run at that point. When I need a singleton use I write this (example, the AudioSystem is a singleton that access FileSystem that is also a singleton):
class AudioSystem :	public SubSystem<AudioSystem>,{	//SubSystem	ConfigurationSystem& ConfigurationSystem;	NotificationSystem& NotificationSystem;	FileSystem& FileSystem;public:	AudioSystem::AudioSystem() :,		ConfigurationSystem(ConfigurationSystem::Instance()),	{ }	//details omitted};

et voilà you have full access to the singleton that will be instanciated only if needed. This is (in my opinion) the fastest a most beautiful way to handle this.
[ILTUOMONDOFUTURO]

This topic is closed to new replies.

Advertisement