包装器函数可以重构吗?

Can the wrapper functions be refactored

本文关键字:重构 函数 包装      更新时间:2023-10-16

>我有一个C++函数的集合,这些函数都接受来自同一组类型(下面的示例中的TypeATypeBTypeC(的类型作为模板参数。为了简化这些函数对 python 的公开,我想为每个函数定义一个函数,该函数不将类型作为模板参数,而是作为字符串参数,如下所示:

template<typename dataType>
int function(int arg)
{
...
}
int function(int arg, string type)
{
if (type == "type_A")
{
return function<TypeA>(arg);
}
else if (type == "type_B")
{
return function<TypeB>(arg);
}
else if (type == "type_C")
{
return function<TypeC>(arg);
}
else
{
std::cerr << "Invalid type!" << std::endl;
exit(1);
}
}

目前,我以这种方式包装所有函数,但这会导致大量代码重复,所以我想知道是否有更好的方法来做到这一点,也许使用预处理器指令?

减少if/else逻辑的一种方法是存储std::function对象的映射,并使用映射进行正确的调用。

int function(int arg, std::string type)
{
using FMap = std::map<std::string, std::function<int(int)>>;
static const FMap fmap{{"type_A", [](int arg) { return function<TypeA>(arg); }},
{"type_B", [](int arg) { return function<TypeB>(arg); }},
{"type_C", [](int arg) { return function<TypeC>(arg); }}};
auto iter = fmap.find(type);
if ( iter != fmap.end() )
{
return iter->second(arg);
}
std::cerr << "Invalid type!" << std::endl;
exit(1);
return 0;
}

如果愿意重命名函数模板,可以简化构造函数图的代码。

template <typename T>
int fun_2(int arg) { ... }
int function(int arg, std::string type)
{
using FMap = std::map<std::string, std::function<int(int)>>;
static const FMap fmap{{"type_A", fun_2<TypeA>},
{"type_B", fun_2<TypeB>},
{"type_C", fun_2<TypeC>}};
auto iter = fmap.find(type);
if ( iter != fmap.end() )
{
return iter->second(arg);
}
std::cerr << "Invalid type!" << std::endl;
exit(1);
return 0;
}