C++ Boost

EventVisitor Concept

This concept defines the interface for single-event visitors. The visitor has a single apply member function (operator()). This function is invoked within the graph algorithm at the event-point specified by the event_filter typedef within the EventVisitor. The following is the list of event tags that can be invoked in BGL algorithms. Each tag corresponds to a member function of the visitor for an algorithm. For example, the BFSVisitor of breadth_first_search() has a cycle_edge() member function. The corresponding tag is on_cycle_edge. The first argument is the event visitor's operator() must be either an edge or vertex descriptor depending on the event tag.
namespace boost {
  struct on_initialize_vertex { };
  struct on_start_vertex { };
  struct on_discover_vertex { };
  struct on_examine_edge { };
  struct on_tree_edge { };
  struct on_cycle_edge { };
  struct on_finish_vertex { };
  struct on_forward_or_cross_edge { };
  struct on_back_edge { };
  struct on_edge_relaxed { };
  struct on_edge_not_relaxed { };
  struct on_edge_minimized { };
  struct on_edge_not_minimized { };
} // namespace boost

Creating an EventVisitorList

Often times one would like to use multiple single-event vistors in a call to a particular algorithm. In the following example we will show how to combine event visitors into a list using std::pair and the algorithm's visitor adaptor class.

Suppose we would like to print out the parenthesis structure of the discover/finish times of vertices in a depth-first search. We can use the BGL algorithm depth_first_search() and two event visitors to accomplish this. The complete source code for the following example is in examples/dfs_parenthesis.cpp. First we define the two event visitors. We use on_discover_vertex and on_finish_vertex as the event points, selected from the list of event points specified in DFSVisitor.

struct open_paren : public base_visitor<open_paren> {
  typedef on_discover_vertex event_filter;
  template <class Vertex, class Graph>
  void operator()(Vertex v, Graph& G) {
    std::cout << "(" << v;
  }
};
struct close_paren : public base_visitor<close_paren> {
  typedef on_finish_vertex event_filter;
  template <class Vertex, class Graph>
  void operator()(Vertex v, Graph& G) {
    std::cout << v << ")";
  }
};
Next we create two event visitor objects and make an EventVisitorList out of them using a std::pair which created by std::make_pair.
std::make_pair(open_paren(), close_paren())
Next we want to pass this list into depth_first_search(), but depth_first_search() is expecting a DFSVisitor, not a EventVisitorList. We therefore use the dfs_visitor adaptor which turns an EventVisitor list into a DFSVisitor. Like all of the visitor adaptors, dfs_visitor has a creation function called make_dfs_visitor().
make_dfs_visitor(std::make_pair(open_paren(), close_paren()))
Now we can pass the resulting visitor object into depth_first_search() as follows.
  // graph object G is created ...

  std::vector<default_color_type> color(num_vertices(G));

  depth_first_search(G, make_dfs_visitor(std::make_pair(open_paren(), close_paren())),
		     color.begin());

Refinement of

none

Notation

G A type that is a model of Graph.
g An object of type G.
V A type that is a model of EventVisitor.
vis An object of type V.
x An object of type boost::graph_traits<G>::vertex_descriptor or boost::graph_traits<G>::edge_descriptor.

Associated Types

Event Filter V::event_filter A tag struct to specify on which event the visitor should be invoked.

Valid Expressions

NameExpressionReturn TypeDescription
Apply Visitor vis(x, g) void Invokes the visitor operation on object x, which is either a vertex or edge descriptor of the graph.

Models

See Also

Visitor concepts

Copyright © 2000 Jeremy Siek, Univ.of Notre Dame (jsiek@lsc.nd.edu)
Lie-Quan Lee, Univ.of Notre Dame (llee1@lsc.nd.edu)
Andrew Lumsdaine, Univ.of Notre Dame (lums@lsc.nd.edu)