C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template singleton

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
choiday
Guest





PostPosted: Wed Aug 17, 2005 8:58 am    Post subject: template singleton Reply with quote



1.
I had make mistake on Singleton class by add unnecessary line.
Does it cause the significant error or it's just meanless thing?

2.
Returning a static pointer reference in instance_i() function is
acceptable code? I mean, Is it bad behavior or good idea?

template<class TYPE, class LOCK>
class Singleton
{
public:
static TYPE* instance();
protected:
TYPE instance_;
static Singleton<TYPE,LOCK>* _singleton; //<-- my mistake
static Singleton
template<class TYPE, class LOCK> Singleton<TYPE, LOCK>*&
Singleton<TYPE,LOCK>::instance_i()
{
static Singleton<TYPE,LOCK>* _singleton = 0;
return _singleton;
}
template<class TYPE, class LOCK> TYPE*
Singleton<TYPE,LOCK>::instance()
{
Singleton<TYPE,LOCK>*& singleton = Singleton<TYPE,
LOCK>::instance_i();

if (singleton == 0)
{
static LOCK lock;

GUARD_RETURN(LOCK, guard, lock, 0);
if (singleton == 0)
{
singleton = new Singleton<TYPE,LOCK>;
}
}

return &singleton->instance_;
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
mlimber
Guest





PostPosted: Wed Aug 17, 2005 5:19 pm    Post subject: Re: template singleton Reply with quote



choiday wrote:
Quote:
1.
I had make mistake on Singleton class by add unnecessary line.
Does it cause the significant error or it's just meanless thing?

2.
Returning a static pointer reference in instance_i() function is
acceptable code? I mean, Is it bad behavior or good idea?

template<class TYPE, class LOCK
class Singleton
{
public:
static TYPE* instance();
protected:
TYPE instance_;
static Singleton static Singleton
template<class TYPE, class LOCK> Singleton<TYPE, LOCK>*&
Singleton<TYPE,LOCK>::instance_i()
{
static Singleton<TYPE,LOCK>* _singleton = 0;
return _singleton;
}
template<class TYPE, class LOCK> TYPE*
Singleton<TYPE,LOCK>::instance()
{
Singleton<TYPE,LOCK>*& singleton = Singleton<TYPE,
LOCK>::instance_i();

if (singleton == 0)
{
static LOCK lock;

GUARD_RETURN(LOCK, guard, lock, 0);
if (singleton == 0)
{
singleton = new Singleton<TYPE,LOCK>;
}
}

return &singleton->instance_;
}

1. The static member variable adds a pointer to your class that could
be used in a linked list or something, though that seems to go against
the idea of having a Singleton in the first place. It's only effect is
to take up an extra bit of memory for the pointer. (Of course you'll
have to declare it outside the class, too.)

2. You have created a need for the reference to the pointer so that
your instance() function can modify the static data declared in
instance_i(). It's not wrong, but it is a rare practice and therefore a
bit confusing for those who will read your code. I would recommend
changing it for that reason.

More to the point, I think what you want to do is declare an instance
of TYPE not Singleton<TYPE,LOCK> in your instance_i() function. In
fact, from the looks of it, you could avoid extra code and confusion by
putting the static declaration of your TYPE in instance() itself and
getting rid of instance_i() altogether.

Also, within your class, you can refer to Singleton without its
typelist:

template<typename T>
struct LinkedListElem
{
T datum;
LinkedListElem* next; // No need for LinkedListElem<T>
};

You might consider the singleton implemented in _Modern C++ Design_
(available in Loki: [url]http://sourceforge.net/projects/loki-lib)[/url], which
deals with multithreaded locking and other issues.

Cheers!

M


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.