【发布时间】:2016-06-23 23:11:27
【问题描述】:
我有一个 boost::graph,它使用如下捆绑属性:
struct Vertex
{
std::string id;
};
如果我想在 boost::dynamic_properties 中使用此信息(例如,以 graphml 格式打印),我可以使用类似的东西:
template<typename T>
std::string myPrettyPrinter(const T& t);
int main()
{
using namespace boost;
MyGraph g;
dynamic_properties dp;
dp.property("id",
make_transform_value_property_map(
& myPrettyPrinter<std::string>,
get(&Vertex::id, g)
)
);
}
由于捆绑的属性将来可能会发生变化,我想对dynamic_properties 的创建进行概括。因此,我使用 boost::fusion
struct Vertex
{
std::string id;
};
BOOST_FUSION_ADAPT_STRUCT(
Vertex,
id
)
template<typename T>
std::string myPrettyPrinter(const T& t);
template <typename T_Seq, typename T_Graph>
void member_iterator(boost::dynamic_properties& dp, T_Graph& g)
{
using namespace boost;
using Indices = mpl::range_c<
unsigned,
0,
fusion::result_of::size<T_Seq>::value
>;
fusion::for_each(
Indices(),
[&](auto i)
{
using I = decltype(i);
dp.property(
fusion::extension::struct_member_name<T_Seq, i>::call(),
make_transform_value_property_map(
& myPrettyPrinter<
typename fusion::result_of::value_at<T_Seq, I>::type
>,
get(
// This works but is not generic,
// since it relies on the specific
// member name "id":
& T_Seq::id,
g
)
)
);
}
);
}
int main()
{
MyGraph g;
boost::dynamic_properties dp;
member_iterator<Vertex>(dp, g);
}
我的问题是,我找不到以通用方式表达&T_Seq::id 行的方法。我一直在研究fusion::extension::struct_member_name,但没有成功。
我寻找一种通用方法来替换有问题的行,或者寻找一种完全不同的方法来遍历Vertex 的成员。
【问题讨论】:
标签: c++ boost metaprogramming boost-graph boost-fusion