I have a graph type where each Vertex carries a std::vector<int> as a property.
struct VertexProperties {
std::vector<int> numbers;
};
using Graph = boost::adjacency_list<
boost::vecS, boost::vecS, boost::undirectedS, VertexProperties>;
I wrote an example object of my graph type to a GraphML file using boost::write_graphml. To do so, I used boost::make_transform_value_property_map to convert the std::vector<int> property to a std::string. The GraphML file has the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="key0" for="node" attr.name="numbers" attr.type="string" />
<graph id="G" edgedefault="undirected" parse.nodeids="free" parse.edgeids="canonical" parse.order="nodesfirst">
<node id="n0">
<data key="key0">1 2 3 </data>
</node>
</graph>
</graphml>
Now I would like to read the file back in to reobtain the graph (in a different program) using boost::read_graphml. To do so, it is necessary to create a boost::dynamic_properties object and add to that a property map that can understand the information found in the GraphML file and set the correct vertex property accordingly.
How can the latter property map be defined?
I solved my problem by writing a custom property map class template
TranslateStringPMapthat wraps an existing property map and takes two function objects that convert between strings and the wrapped map's value type.File
translate_string_pmap.hpp:By customizing the conversion function objects
to_stringandfrom_string,TranslateStringPMapcan be added to aboost::dynamic_propertiesobject to facilitate reading and writing of arbitrary graph property types. The following file gives a usage example.File
graph_rw.cpp: