使用 boost::fusion 进行迭代时指向类成员的指针
Pointers to class members when iterating with boost::fusion
我有一个使用捆绑属性的 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
的成员。
不管现有的答案是什么,我总是对使用宏犹豫不决。
在这种情况下,我注意到由于boost::property_map<Graph, Tag>
界面,一切都变得困难。我想,你可以改用vertex_bundle_t
。
这是一个完全不使用宏的简单演示,适用于顶点和边束。(您可以删除调试输出并重新添加漂亮的打印钩子)。
住在科里鲁
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/find.hpp>
#include <boost/phoenix/fusion/at.hpp>
#include <boost/phoenix.hpp>
#include <boost/mpl/range_c.hpp>
#include <iostream>
struct Vertex {
std::string id;
int numeric_value;
};
struct Edge {
std::string more;
int awesome_sauce;
};
BOOST_FUSION_ADAPT_STRUCT(Vertex, id, numeric_value)
BOOST_FUSION_ADAPT_STRUCT(Edge, more, awesome_sauce)
template <typename Tag, typename T_Graph>
void member_iterator(boost::dynamic_properties& dp, T_Graph& g)
{
using namespace boost;
using Bundle = typename boost::property_map<T_Graph, Tag>::type;
using T_Seq = typename boost::property_traits<Bundle>::value_type;
using Indices = mpl::range_c<unsigned, 0, fusion::result_of::size<T_Seq>::value>;
fusion::for_each(
Indices{},
[&, bundle=get(Tag{}, g)](auto i) {
auto name = fusion::extension::struct_member_name<T_Seq, i>::call();
std::cout << "Adding " << name << "n";
dp.property(
name,
make_transform_value_property_map(phoenix::at_c<i>(phoenix::arg_names::arg1), bundle)
);
}
);
}
using MyGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, Vertex, Edge>;
int main()
{
MyGraph g;
boost::dynamic_properties dp;
member_iterator<boost::vertex_bundle_t>(dp, g);
member_iterator<boost::edge_bundle_t>(dp, g);
}
指纹
Adding id
Adding numeric_value
Adding more
Adding awesome_sauce
我所知,您无法从内部存储的信息中获取指向成员BOOST_FUSION_ADAPT_STRUCT
指针(正如您所发现的,您可以获取成员类型或名称)。解决此问题的一种可能方法是创建一个宏,该宏调用BOOST_FUSION_ADAPT_STRUCT
然后存储其他信息。我选择像这样存储它:
template<>
struct pointer_to_member_N<Vertex,0>
{
static constexpr std::string Vertex::* value = &Vertex::id;
};
为了生成该结构,我使用以下宏:
#define YOUR_NS_SAVE_MEMBERPTR(STRUCT_NAME,MEMBERS)
namespace your_ns {
BOOST_PP_SEQ_FOR_EACH_I(CREATE_POINTER_TO_MEMBER_TRAIT,STRUCT_NAME,BOOST_PP_CAT(YOUR_NS_SAVE_MEMBERPTR_FILLER_0 MEMBERS,_END))
}
此宏采用结构名称和一系列对(type,name)
,并简单地打开命名空间your_ns
并调用CREATE_POINTER_TO_MEMBER_TRAIT
,每个对STRUCT_NAME
作为数据传递。
#define CREATE_POINTER_TO_MEMBER_TRAIT(R,STRUCT_NAME,INDEX,TYPE_AND_NAME)
template <> struct pointer_to_member_N<STRUCT_NAME, INDEX>{ static constexpr BOOST_PP_TUPLE_ELEM(2,0,TYPE_AND_NAME) STRUCT_NAME::* value = &STRUCT_NAME::BOOST_PP_TUPLE_ELEM(2,1,TYPE_AND_NAME); };
这是真正创造特征的那个。它需要一个参数R
(我不知道它的作用),您正在适应的结构的名称,当前成员的索引和一对(type,name)
。它使用 BOOST_PP_TUPLE_ELEM(2,N,TYPE_AND_NAME)
来获取成员的类型或名称。
#include <iostream>
#include <string>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/mpl/range_c.hpp>
#include <cstddef>
namespace your_ns
{
template <typename StructName, int N>
struct pointer_to_member_N;
}
//Heavily "inspired" from BOOST_FUSION_ADAPT_STRUCT
#define YOUR_NS_SAVE_MEMBERPTR_FILLER_0(X, Y)
((X, Y)) YOUR_NS_SAVE_MEMBERPTR_FILLER_1
#define YOUR_NS_SAVE_MEMBERPTR_FILLER_1(X, Y)
((X, Y)) YOUR_NS_SAVE_MEMBERPTR_FILLER_0
#define YOUR_NS_SAVE_MEMBERPTR_FILLER_0_END
#define YOUR_NS_SAVE_MEMBERPTR_FILLER_1_END
#define CREATE_POINTER_TO_MEMBER_TRAIT(R,STRUCT_NAME,INDEX,TYPE_AND_NAME)
template <> struct pointer_to_member_N<STRUCT_NAME, INDEX>{ static constexpr BOOST_PP_TUPLE_ELEM(2,0,TYPE_AND_NAME) STRUCT_NAME::* value = &STRUCT_NAME::BOOST_PP_TUPLE_ELEM(2,1,TYPE_AND_NAME); };
#define YOUR_NS_SAVE_MEMBERPTR(STRUCT_NAME,MEMBERS)
namespace your_ns {
BOOST_PP_SEQ_FOR_EACH_I(CREATE_POINTER_TO_MEMBER_TRAIT,STRUCT_NAME,BOOST_PP_CAT(YOUR_NS_SAVE_MEMBERPTR_FILLER_0 MEMBERS,_END))
}
#define ADAPT_STRUCT_AND_SAVE_MEMBERPTR(TYPE,MEMBERS) BOOST_FUSION_ADAPT_STRUCT(TYPE,MEMBERS) YOUR_NS_SAVE_MEMBERPTR(TYPE,MEMBERS)
struct Vertex {
std::string id;
std::size_t index;
};
ADAPT_STRUCT_AND_SAVE_MEMBERPTR(
Vertex,
(std::string, id)
(std::size_t, index)
)
int main() {
Vertex v;
v.id="A";
v.index=0;
std::cout << std::mem_fn(your_ns::pointer_to_member_N<Vertex,0>::value)(v) << std::endl;
std::cout << v.*your_ns::pointer_to_member_N<Vertex,1>::value << std::endl;
}
- 从成员指针到整个结构/类的强制转换
- OOP 中的单成员指针
- 使用结构成员指针在C++中填充结构
- 聚合初始化,将成员指针设置为同一结构成员
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 共享 C++ 的数据成员指针
- 如何声明指向成员内容的成员指针m_pmd/m_pmf并访问它们?
- 从类C++外部调用指向成员方法的成员指针
- 结构成员指针是否自动初始化为零?
- 如何删除类内类类型的类成员指针
- 是否打印指向已定义 int 的成员指针
- 指向常量的成员指针
- 通过此指针访问时的成员差异和自身的成员指针(简单的 QT 示例问题)
- 类数据成员指针的非类型模板参数包无法使用 gcc 编译
- 为什么我不能在同一行中定义两个相同类型的类的成员指针
- 将类成员指针传递给 Lambda 捕获列表 c++11
- 在单一实例类中将成员指针设置为 null 的正确方法是什么
- 无法删除在destructor中的成员指针的课程
- 删除对象而不调用成员指针的析构函数
- 当派生类的基类具有成员指针时,对其进行深层复制