简体   繁体   English

c ++获取Boost :: Graph的顶点属性

[英]c++ Getting vertex properties of a Boost::Graph

Given a Vertex as: 鉴于Vertex为:

class VertexProps {
  public:
    int id;
    float frame;
    string name;
};

I have initialized my boost graph using bundled properties. 我已经使用捆绑属性初始化了我的提升图。 I know I can get the frame by using: 我知道我可以使用以下方式获取框架:

std::cout << "Vertex frame: " << boost::get(&VertexProps::frame, graph, vertex) << std::endl;
//Need to make this work: float frame = boost::get(&VertexProps::frame, graph, vertex);
//graph is a boost::adjacency_list and vertex is a boost::vertex_descriptor

However, I want to write a more general function or wrapper such that: 但是,我想编写一个更通用的函数或包装器,以便:

std::vector<float> frames;
std::string prop_name = "frame";
float frame = graph.get_vertex_prop(vertex, prop_name);
frames.push_back(frame);

I was hoping for something along the lines: 我希望有类似的东西:

typedef boost::variant< int, unsigned int, float, std::string > PropValType;
typedef boost::vertex_bundle_type<Graph>::type PropIdType;
typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;

PropValType get_vertex_prop(Vertex& v, PropIdType pname)
{
  boost::get(pname, graph, v);
  //If pname = frame then return value as float (or cast boost::variant to float)
  //If pname = name then return value as a string
}

I want to avoid something like: 我想避免像这样的事情:

PropValType get_vertex_prop(Vertex& v, std::string pname) {
 if (pname == "frame") {
   boost::get(&VertexProps::frame, graph, v)
   //return value as float
 }
 if (...)
}

There is no way to do that with out any macro magic at compile time. 在编译时没有任何方法可以解决任何宏魔法问题。 C++ doesn't allow string literals as non-type template parameters and has very slim reflection capabilities. C ++不允许字符串文字作为非类型模板参数,并且具有非常纤薄的反射功能。

The solution you propose (and want to avoid) requires some work at run-time and should generally be avoided. 您提出(并且希望避免)的解决方案需要在运行时进行一些工作,并且通常应该避免。

The macro solution would be along those lines: 宏观解决方案将遵循这些方针:

#define MY_GET(v, pname) boost::get(&VertexProps##pname, graph, v)
PropValType v = MY_GET(v, frame);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM