From ba7531ac46ac9cc6a1200bd5c41eb93825b80929 Mon Sep 17 00:00:00 2001 From: Daniel Krebs Date: Tue, 30 Jan 2018 19:13:10 +0100 Subject: [PATCH] lib/graph: allow stringifying of vertex and edge derived types This yields nices debug messages and a much nice dump(). --- fpga/include/villas/directed_graph.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/fpga/include/villas/directed_graph.hpp b/fpga/include/villas/directed_graph.hpp index 192673f76..8d964dae1 100644 --- a/fpga/include/villas/directed_graph.hpp +++ b/fpga/include/villas/directed_graph.hpp @@ -28,6 +28,10 @@ class Vertex { public: using Identifier = std::size_t; + friend std::ostream& + operator<< (std::ostream& stream, const Vertex& vertex) + { return stream << vertex.id; } + bool operator==(const Vertex& other) { return this->id == other.id; } @@ -46,6 +50,10 @@ class Edge { public: using Identifier = std::size_t; + friend std::ostream& + operator<< (std::ostream& stream, const Edge& edge) + { return stream << edge.id; } + bool operator==(const Edge& other) { return this->id == other.id; } @@ -100,7 +108,7 @@ public: { vertex->id = lastVertexId++; - logger->debug("New vertex: {}", vertex->id); + logger->debug("New vertex: {}", *vertex); vertices[vertex->id] = vertex; return vertex->id; @@ -117,7 +125,7 @@ public: edge->from = fromVertexId; edge->to = toVertexId; - logger->debug("New edge {}: {} -> {}", edge->id, edge->from, edge->to); + logger->debug("New edge {}: {} -> {}", *edge, edge->from, edge->to); // this is a directed graph, so only push edge to starting vertex getVertex(edge->from)->edges.push_back(edge->id); @@ -240,12 +248,12 @@ public: ssEdges << getEdge(edge)->to << " "; } - logger->info(" {} connected to: {}", vertexId, ssEdges.str()); + logger->info(" {} connected to: {}", *vertex, ssEdges.str()); } logger->info("Edges:"); for(auto& [edgeId, edge] : edges) { - logger->info(" {}: {} -> {}", edgeId, edge->from, edge->to); + logger->info(" {}: {} -> {}", *edge, edge->from, edge->to); } }