Basic singleton object in C ++
Is this a legal way to create a basic singleton object that ensures that all of its children are singleton as well? I want to use this in conjunction with a factory pattern to ensure that all factories are single.
The key class is designed to prevent child elements from being cracked around the singleletons constructor, but is basically just a formal parameter.
Is there something wrong in particular with this approach?
class singleton
{
protected:
struct key
{
private:
friend class singleton;
key(){}
};
public:
singleton(const key&)
{}
template <class child> static child* getInstance()
{
static key instanceKey;
static child* unique = new child(instanceKey);
return unique;
}
private:
};
class test : public singleton
{
public:
test(singleton::key& key)
: singleton(key)
{}
void init()
{
//init object
}
private:
};
int main()
{
test* t = singleton::getInstance<test>();
return 0;
}
+3
source to share
2 answers
I added a class to remove all your factories at the end. :) For this mechanism, you need a virtual destructor chain. Also, I have not found any other memory leaks and the instances are unique. The "key" security mechanism seems fine too. I see no way to get a key from a static function.
#include <iostream>
#include <set>
class Singleton;
class Set_of_Singletons
{
friend class Singleton;
private:
std::set<Singleton*> instances;
Set_of_Singletons():instances(){}
public:
~Set_of_Singletons();
};
class Singleton
{
private:
static Set_of_Singletons children;
protected:
struct key
{
private:
friend class Singleton;
key(){}
};
public:
template <class Child> static Child* doNew()
{
static key instanceKey;
Child* u = new Child(instanceKey);
children.instances.insert((Singleton*)u);
return u;
}
template <class Child> static Child* getInstance()
{
static Child* unique = doNew<Child>();
return unique;
}
Singleton(const key&)
{}
virtual ~Singleton(){}
};
Set_of_Singletons::~Set_of_Singletons()
{
for (auto inst: instances)
delete inst;
instances.clear();
}
Set_of_Singletons Singleton::children;
class B: public Singleton
{
public:
B(Singleton::key& key)
: Singleton(key)
{
std::cout << ">>>> Construction of B \n";
}
virtual ~B()
{
std::cout << "<<<< Destruction of B \n";
}
};
class C final: public Singleton
{
public:
C(Singleton::key& key)
: Singleton(key)
{
std::cout << ">>>> Construction of C \n";
}
virtual ~C()
{
std::cout << "<<<< Destruction of C \n";
}
};
int main()
{
// Object creation seems all ok
B* x = Singleton::getInstance<B>();
std::cout << "x: " << x << "\n";
B* y = Singleton::getInstance<B>();
std::cout << "y: " << y << "\n";
C* v = Singleton::getInstance<C>();
std::cout << "v: " << v << "\n";
C* w = Singleton::getInstance<C>();
std::cout << "w: " << w << "\n";
return 0;
}
// ~Have fun.~
0
source to share
You can use CRTP something like:
template <typename T>
class Singleton
{
public:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
static T& getInstance() {
static_assert(std::is_base_of<Singleton, T>::value, "T should inherit of Singleton");
static T instance;
return instance;
}
protected:
Singleton() = default;
~Singleton() = default;
};
And then
class MyFactory : Singleton<MyFactory>
{
private:
friend class Singleton<MyFactory>;
MyFactory() = default;
public:
//...
};
0
source to share