dot.hpp 1.5 KB

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