class MySingleton : public CSingleton<MySingleton>
Fairly standard to the best of my knowledge (Although if there are better ways I'd like to hear of them, I dont like the concept of a base class being templated).
The thing is, I want to be able to keep a list of pointers to all intiailised Singletons, so during shutdown it can just iterate through and call CSingleton::DestroySingleton() to avoid any accidental memory leaks (Although it would produce a warning in the logs so it can be fixed quick-smart).
Thing is, though, I'm trying to be neat about this and I'd like this list of pointers to be a member of my Singleton class rather than some global flying about or creating a manager class for this express purpose (Which seems fairly overkill for the small chance someone might forget a DestroySingleton). And, naturally, this member would need to be shared between ALL derivatives of the templated class.
So is there a way to define a static member variable that applies to all derived classes from the template? Or is my Singleton class ass backwards from sunday?
Template-static member variable?
Okay, this is a wierd question (I think):
Is there any way to make a static member variable inside a class that is static to ALL classes derived from that template? That sounds awkward, so I'll explain what I'm trying to do:
I've got a templated Singleton class, and any class that wishes to use it declares:
I'm not 100% positive but my guess is that you can't have a static var shared between different the types that use the template. What you could do though is have some class called CSingletonBase or something which CSingleton inherits. Then CSingletonBase can hold the static list of pointers and the cleanup function.
What you want to do is maintain a static list of all the Singletons that you create, then you can register a function with the atexit() function (this function is called by the C Runtime Library before the application exits) which ensures that all of the created singletons are destroyed.
Depending on your exact design something like this :
You just have to be cautious about exactly where you store you static vector of singletons, should it be a global? within some other singleton class? you also have to make sure that whenever a singleton is created or destroyed it adds or removes itself from the list. I hope that this has given you some ideas, maybe if you do a bit of googling around Singletons and the atexit function
Depending on your exact design something like this :
static std::vector<Singleton*> instances;void destroySingletons(){ for(int i=0;i<instances.size;i++) { instances->destroy; }}atexit(destroySingletons);
You just have to be cautious about exactly where you store you static vector of singletons, should it be a global? within some other singleton class? you also have to make sure that whenever a singleton is created or destroyed it adds or removes itself from the list. I hope that this has given you some ideas, maybe if you do a bit of googling around Singletons and the atexit function
You could also store a list of smart pointers (e.g. boost's shared_ptr) instead of bare pointers. Then they should be cleaned up automatically.
Hope this helps,
Pete
Hope this helps,
Pete
Quote:Original post by moagstar
What you want to do is maintain a static list of all the Singletons that you create, then you can register a function with the atexit() function (this function is called by the C Runtime Library before the application exits) which ensures that all of the created singletons are destroyed.
The problem with this is that Singleton is templated so you can't have a container holding each of the different templated types. Stro is thinking along the right sort of lines:
class SingletonBase{public: SingletonBase() { singletons.push_back(this); } virtual ~SingletonBase(); virtual void Cleanup() = 0;private: static std::vector<SingletonBase *> singletons;};
If you ever manually destroy singletons before the end of your program then SingletonBase's destructor will have to remove it's this pointer from the static singleton array.
At the end of your program you'll simply run through the singletons vector and call Cleanup on each class.
James
Hello PlayfulPuppy,
Try a static reference in the template that is set to a gobal/file scope variable.
That should do it if the compiler accepts it.
This way each verison of the template shares the gobal/file scop variable though each template static reference.
Lord Bart :)
Try a static reference in the template that is set to a gobal/file scope variable.
That should do it if the compiler accepts it.
This way each verison of the template shares the gobal/file scop variable though each template static reference.
Lord Bart :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement