im facing a problem in which I have to generate a huge amount of code, all of it fairly similar, and I want to know if there is any way to templatized.
Lets say I have a structure of this type
template <typename ...NodeKindTs>
struct Element{
std::tuple<NodeKindTs...> nodes;
}
I have a vector of ints which related a Node with another, and a vector of an enum which says which kind of Node is each. The kind can be A, B or C.
enum class Kind {A,B,C};
std::vector<int> relationShip;
std::vector<Kind> kind;
For example, if I have
relationShip = {1,2,-1};
kind = {A,B,A}
would mean that the first node is of kind A and is related to the second node, which is of kind B. You get it.
Now, I have to create Elements and insert them into a vector depending on the NodeKind that each node and the relation ship. This Elements are templatized by up to 6 NodeKinds. For solving this, I need a huge if that check the Kind of each node and then call the Element ctor.
For the 2 NodeKinds case, this means doing something like
if (node1.type == A && node2.type == A) {
auto &simNode1 = containerOfAs(node1.id);
auto &smiNode2 = containerOfAs(node2.id);
insertElement(elementFactory(simNode1, simNode2));
}
if (node1.type == A && node2.type == C)
{
auto &simNode1 = containerOfAs(node1.id);
auto &smiNode2 = containerOfCs(node2.id);
insertElement(elementFactory(simNode1, simNode2));
}
if (node1.type == B && node2.type == C)
{
auto &simNode1 = containerOfBs(node1.id);
auto &smiNode2 = containerOfCs(node2.id);
insertElement(elementFactory(simNode1, simNode2));
}
...
inserElement is a meta-function I have create which insert the element into the container if fits in from a list of containers.
For this 2 case, this requires up to 9 ifs. For the 3 case it requires 27 ifs and for the 6 case it requires 729 ifs. I really dont want to code them.
Any idea on how I could solve this?
Thanks!
std::variant
to let it doing the combination for you... I'm afraid that it will slow down compilation though. – Scorper