c++ Getting vertex properties of a Boost::Graph
Given a Vertex as:
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.
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);
精彩评论