我有以下工厂函数:
std::auto_ptr<IPath> PathFactory(std::string const& branch_type, CPathModel const& path_model)
{
using namespace boost::assign;
using namespace boost::phoenix::placeholders;
typedef boost::function<IPath* (CPathModel const&)> PathFactoryFunction;
typedef boost::unordered_map<std::string, PathFactoryFunction> FactoryMap;
static FactoryMap factory_map = map_list_of<std::string, PathFactoryFunction>
("plu", &phx::new_<CPluPath>)
("time_of_day", &phx::new_<CTimeOfDayPath>)
("probability", &phx::new_<CProbabilityPath>)
;
std::auto_ptr<IPath> new_path;
FactoryMap::const_iterator it = factory_map.find(branch_type);
if (it != factory_map.end())
{
new_path.reset(it->second(path_model));
}
return new_path;
}
此代码无法编译,请注意我使用的是 C++03。我在这里要做的是创建一个字符串映射到可以分配特定类型对象的小函数对象。每个对象采用相同类型的单个构造参数 (CPathModel const&
)。
phx::new_
有几个重载,所以直接引用它可能不是最好的主意,但我希望大家能帮我找到使用 boost::的方法phoenix 清理这段代码并使映射工作优雅。
在这一点上,只定义一个带有重载的 ()
运算符的小模板类似乎更容易,该运算符接受参数并在内部执行 new T(p1)
。但这是样板文件,看起来很简单,boost 必须在某处有一个很好的解决方案......
最佳答案
Phoenix 是惰性仿函数的实用工具。
这里不需要任何东西(看不到表达式模板)。
因此,您可以制作自己的工厂方法模板:
template <typename PathType> IPath* make_path(CPathModel const& model) {
return new PathType(model);
}
并使用它:
static FactoryMap factory_map = map_list_of<std::string, PathFactoryFunction>
("plu", &make_path<CPluPath>)
("time_of_day", &make_path<CTimeOfDayPath>)
("probability", &make_path<CProbabilityPath>)
;
工作完成。
尽管在这一点上,使用 map 查找工厂的好处为零。事实上,这只是浪费。一个简单的开关[1] 将远 优越。更重要的是,因为它删除了 std::function
中的类型删除(隐式虚拟多态性)。
[1] 事实上,它需要链接 if
,或者您可以打开 Perfect Hash
关于c++ - boost phoenix 的 map new_?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22356514/