The Battle for Wesnoth  1.19.0-dev
back_edge_detector.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 - 2023
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <boost/graph/depth_first_search.hpp>
18 #include <type_traits>
19 
20 namespace utils
21 {
22 /**
23  * A helper for boost::depth_first_search (DFS) usage with the purpose of detecting cycles.
24  *
25  * The callback_ is called whenever a back edge is found.
26  */
27 template<typename Callback>
28 class back_edge_detector : public boost::dfs_visitor<>
29 {
30 public:
31  explicit back_edge_detector(Callback callback)
32  : callback_(std::move(callback))
33  {
34  }
35 
36  template<typename Graph>
37  void back_edge(typename Graph::edge_descriptor edge, Graph&)
38  {
39  static_assert(std::is_invocable_v<Callback, typename Graph::edge_descriptor>);
40  callback_(edge);
41  }
42 
43 private:
44  Callback callback_;
45 };
46 
47 } // namespace utils
A helper for boost::depth_first_search (DFS) usage with the purpose of detecting cycles.
back_edge_detector(Callback callback)
void back_edge(typename Graph::edge_descriptor edge, Graph &)