I created some small factory class. See the code below. The factory uses a simple map with a key and a class creator function. The challenge was that I wanted to use different signatures for the createInstance functions. With that, I could use different derived classes with different constructor signatures. So, I would be able to pass different values to the constructor of the classes.
Since I cannot store functions with different signatueres in std::function
and finally in a std::map
, I have written a short wrapper that "erases" the type parameter part. So, basically I can then store the function in a std::function
, and finally in a std::any
.
The code works fine. You can compile it and run it without problem.
There is also some test code attached.
Now my question:
I tried to come up with only one constructor in my class "Creator". But I could not achieve that. In my opinion it should be possible. I would like to ask for your support to come up with a possible solution.
Please note: I am in the phase of refactoring. So the code is not yet "perfect".
#include <iostream>
#include <map>
#include <utility>
#include <functional>
#include <any>
// Some demo classes ----------------------------------------------------------------------------------
struct Base {
Base(int d) : data(d) {};
virtual ~Base() { std::cout << "Destructor Base\n"; }
virtual void print() { std::cout << "Print Base\n"; }
int data{};
};
struct Child1 : public Base {
Child1(int d, std::string s) : Base(d) { std::cout << "Constructor Child1 " << d << " " << s << "\n"; }
virtual ~Child1() { std::cout << "Destructor Child1\n"; }
virtual void print() { std::cout << "Print Child1: " << data << "\n"; }
};
struct Child2 : public Base {
Child2(int d, char c, long l) : Base(d) { std::cout << "Constructor Child2 " << d << " " << c << " " << l << "\n"; }
virtual ~Child2() { std::cout << "Destructor Child2\n"; }
virtual void print() { std::cout << "Print Child2: " << data << "\n"; }
};
struct Child3 : public Base {
Child3(int d, long l, char c, std::string s) : Base(d) { std::cout << "Constructor Child3 " << d << " " << l << " " << c << " " << s << "\n"; }
virtual ~Child3() { std::cout << "Destructor Child3\n"; }
virtual void print() { std::cout << "Print Child3: " << data << "\n"; }
};
using Ret = std::unique_ptr<Base>;
template <typename ... Args>
using Func = std::function<Ret(Args...)>;
// ---------------------------------------------------------------------------------------------------
// Hide away the different signatures for std::function
class Creator
{
public:
Creator() {}
// I want to combine the follwong 2 constructors in one
template<typename Function>
Creator(Function&& fun) : Creator(std::function(fun)) {}
template<typename ... Args>
Creator(Func<Args...> fun) : m_any(fun) {}
template<typename ... Args>
Ret operator()(Args ... args) { return std::any_cast<Func<Args...>>(m_any)(args...); }
protected:
std::any m_any;
};
template <class Child, typename ...Args>
std::unique_ptr<Base> createClass(Args...args) { return std::make_unique<Child>(args...); }
// The Factory ----------------------------------------------------------------------------------------
template <class Key>
class Factory
{
std::map<Key, Creator> selector;
public:
Factory(std::initializer_list<std::pair<const Key, Creator>> il) : selector(il) {}
template <typename ... Args>
Ret createInstance(Key key, Args ... args) {
if (selector.find(key) != selector.end()) {
return selector[key](args ...);
}
else {
return std::make_unique<Base>(0);
}
}
};
int main()
{
Factory<int> factory {
{1, createClass<Child1, int, std::string>},
{2, createClass<Child2, int, char, long>},
{3, createClass<Child3, int, long, char, std::string>}
};
// Some test values
std::string s1(" Hello1 "); std::string s3(" Hello3 ");
int i = 1; const int ci = 1; int& ri = i; const int& cri = i; int&& rri = 1;
std::unique_ptr<Base> b1 = factory.createInstance(1, 1, s1);
std::unique_ptr<Base> b2 = factory.createInstance(2, 2, '2', 2L);
std::unique_ptr<Base> b3 = factory.createInstance(3, 3, 3L, '3', s3);
b1->print();
b2->print();
b3->print();
b1 = factory.createInstance(2, 4, '4', 4L);
b1->print();
return 0;
}
So, again, I would like to get rid of the 2 constructors
template<typename Function>
Creator(Function&& fun) : Creator(std::function(fun)) {}
template<typename ... Args>
Creator(Func<Args...> fun) : m_any(fun) {}
and have only one in the end. How?
Sorry for the lengthy code.
How about simply removing the constructor taking the std::function
?
template<typename Function>
Creator(Function&& fun) : m_any(std::function(fun)) {}
This will wrap fun
only if it's not already a std::function
. For cases if it's a std::function
then it's a noop.
Looking at your code, you only use function pointers as your factory function. You could save yourself some indirections using function pointers directly instead of std::function
, which is already a layer like std::any
.
template <typename ... Args>
using Func = std::add_pointer_t<Ret(Args...)>;
// in you class:
template<typename Function, std::enable_if_t<std::is_pointer_v<Function>, int> = 0>
Creator(Function fun) : m_any(fun) {}
If you use that, in C++20 you can remove the ugly enable if and use a concept:
template<typename T>
concept FunctionPointer = requires(T t) {
requires std::is_pointer_v<T>;
requires std::is_function_v<std::remove_pointer_t<T>>;
{ std::function(t) };
};
// then in your class:
Creator(FunctionPointer auto fun) : m_any(fun) {}