dot.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef ENTT_GRAPH_DOT_HPP
  2. #define ENTT_GRAPH_DOT_HPP
  3. #include <ostream>
  4. #include <type_traits>
  5. #include "fwd.hpp"
  6. namespace entt {
  7. /**
  8. * @brief Outputs a graph in dot format.
  9. * @tparam Graph Graph type, valid as long as it exposes edges and vertices.
  10. * @tparam Writer Vertex decorator type.
  11. * @param out A standard output stream.
  12. * @param graph The graph to output.
  13. * @param writer Vertex decorator object.
  14. */
  15. template<typename Graph, typename Writer>
  16. void dot(std::ostream &out, const Graph &graph, Writer writer) {
  17. static_assert(std::is_base_of_v<directed_tag, typename Graph::graph_category>, "Invalid graph category");
  18. if constexpr(std::is_same_v<typename Graph::graph_category, undirected_tag>) {
  19. out << "graph{";
  20. } else {
  21. out << "digraph{";
  22. }
  23. for(auto &&vertex: graph.vertices()) {
  24. out << vertex << "[";
  25. writer(out, vertex);
  26. out << "];";
  27. }
  28. for(auto [lhs, rhs]: graph.edges()) {
  29. if constexpr(std::is_same_v<typename Graph::graph_category, undirected_tag>) {
  30. out << lhs << "--" << rhs << ";";
  31. } else {
  32. out << lhs << "->" << rhs << ";";
  33. }
  34. }
  35. out << "}";
  36. }
  37. /**
  38. * @brief Outputs a graph in dot format.
  39. * @tparam Graph Graph type, valid as long as it exposes edges and vertices.
  40. * @param out A standard output stream.
  41. * @param graph The graph to output.
  42. */
  43. template<typename Graph>
  44. void dot(std::ostream &out, const Graph &graph) {
  45. return dot(out, graph, [](auto &&...) {});
  46. }
  47. } // namespace entt
  48. #endif