I'm using Boost Graph library in order to read a GraphML file. What I want to do is to use Boost capabilities of graph management to create my own dynamically allocated objects structure so that I can run my custom algorithm on it.
struct VertexProperties {
std::string vertex_name;
bool defect;
bool node_logic;
custom_node * node;
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
But the problem is that I can't seem to allocate memory for the custom_node pointer when using a Deep First Search custom visitor.
node = new custom_node(g[v].vertex_name, 0, standby, normal);
As I am getting "read-only" compilation errors.
I was wandering if there was a way to map the graph to something else where I could use dynamic allocation to re-create my graph structure ?
Trimmed main :
#include <iostream>
#include <fstream>
#include <boost/graph/graphml.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include "node.h"
using namespace boost;
struct VertexProperties {
std::string vertex_name;
bool node_logic;
custom_node * node;
};
typedef adjacency_list<vecS, vecS, directedS, VertexProperties> DirectedGraph;
typedef graph_traits<DirectedGraph>::vertex_descriptor custom_vertex;
typedef graph_traits<DirectedGraph>::edge_descriptor custom_edge;
class custom_dfs_visitor : public default_dfs_visitor {
public:
void discover_vertex(custom_vertex v, const DirectedGraph& g) const
{
// Looking for adjacent vertices
DirectedGraph::adjacency_iterator neighbourIt, neighbourEnd;
if(true == g[v].node_logic)
{
g[v].node = new custom_node(g[v].vertex_name, 0, standby, normal);
}
std::cout << g[v].vertex_name << " is connected with ";
tie(neighbourIt, neighbourEnd) = adjacent_vertices(v, g);
for (; neighbourIt != neighbourEnd; ++neighbourIt) {
std::cout << g[*neighbourIt].vertex_name << " ";
}
std::cout << std::endl;
}
void examine_edge(custom_edge e, const DirectedGraph& g) const
{
std::cout << "Examining edges : " << g[e.m_source].vertex_name << " >> "
<< g[e.m_target].vertex_name << std::endl;
}
};
int main(int argc, char* argv[])
{
int i;
bool is_config = false;
DirectedGraph g;
int verbose;
std::ifstream infile;
dynamic_properties dp(ignore_other_properties);
custom_dfs_visitor vis;
dp.property("node_name", boost::get(&VertexProperties::vertex_name, g));
dp.property("node_logic", boost::get(&VertexProperties::node_logic, g));
/* Argument check */
if (argc <= 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) {
usage();
return 0;
}
/* Parse command line options */
for (i = 1; (i + 1 < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v': /* verbose */
verbose = 10;
break;
case 'c': /* read *.ini configuration file */
// d_printf(D_INFO, "parsing '%s'... \n", argv[++i]);
infile.open(argv[++i], std::ifstream::in);
if (!infile.is_open()) {
std::cout << "Loading file '" << argv[i] << "'failed" << std::endl;
throw "Could not load file";
}
else {
boost::read_graphml(infile, g, dp);
is_config = true;
}
break;
case 's': {
is_defect = true;
std::string temp_defect(argv[++i]);
defect = temp_defect;
std::cout << defect;
break;
}
default: /* something's wrong */
usage();
break;
}
}
if (true == is_config) {
depth_first_search(g, boost::visitor(vis));
}
return 0;
}
Thanks for your answers