Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

c++ - Efficient dispatch from "name",number to template handler functions

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...