I have an input file that looks like:
op1000,3,free-form,data
op1002,1,other,data,1.0
I have a number of template functions:
template<int version>
static bool op1000(const char * s) { /* parse free-form data */ }
My current method is:
struct op2func
{
const char * op;
const int version;
bool (*parse)(const char *s);
};
op2func mapping[] =
{
{ "op1000",0,op1000<0> },
{ "op1000",1,op1000<1> },
{ "op1000",2,op1000<2> },
{ "op1000",3,op1000<3> },
{ "op1002",0,op1000<0> },
{ "op1002",1,op1000<1> }
};
This involves a linear search for a matching name and version -- this used to be a binary search, but adding new entries in the right place was too challenging for some developers; along with the overhead of a strcmp()
per entry.
There HAS to be a better way to handle this scenario. A C++11 solution is preferred if practical.
Ideally, I'd like something that takes a list of possible operation names and generates a compile-time switch.
question from:
https://stackoverflow.com/questions/66067759/efficient-dispatch-from-name-number-to-template-handler-functions 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…