I'm trying to create a kind of Factory based on some enum values that the user can choose a compile time.
The main thing here is to create a kind of magic switch that works on different values of more than one kind of enum (kind of union of different union values, possibly with same value).
enum class A {A1, A2, A3, A4};
enum class B {B1, B2, B3, B4, B6, B7};
struct ObjectBase
{
virtual void apply(char input) = 0;
};
template<A a, class... Args>
struct Object : ObjectBase;
template<>
struct Object<A::A1, int> : ObjectBase
{
Object(int i) i_(i) { }
void apply(char input) { /*do stuff with i_*/}
int i_;
}
template<>
struct Object<A::A2, int, double> : ObjectBase
{
Object(int i, double d) i_(i), d_(d) { }
void apply(char input) { /*do stuff with i_*/}
int i_;
double d_;
}
template<class V, V value>
struct Element
{
};
template<class V, V first, V last>
struct AllElementWithin
{
};
Switcher< Element<A,A1>, Element<A,A2>,
AllElementWithin<B, B1, B3> > switcher (2, 4.0);
// This should create a switch / lookup table that
// initializes for instance Object<A::A1> with 2
// and Object<A::A2> with 2 and 4
char myInput = 'F';
ObjectBase* ob = switcher.create(A::A1);
// This should return an ObjectBase* that points to a
// Object<A1,int>
ob->apply(myInput);
Is there an already kwown implementation pattern I can exploit here? I wouldn't like to re-invent the wheel.
Best would be something that compiles with C++11
[Edit] Some more info:
The factory should allow the creation of objects of different kind (that inherit from a specific base) in an efficient way. Ideally the user class that wants to add more objects can just create his enum and some classes that define the wanted behavior, and simply use the factory with these enums together with some other enums defined by other people.
Please ask for other clarifications if it's not clear