Program Listing for File tdgraph.hpp

Return to documentation for file (cosm/ta/ds/tdgraph.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <boost/graph/adjacency_list.hpp>
#include <functional>
#include <string>
#include <vector>

#include "rcppsw/rcppsw.hpp"
#include "rcppsw/er/client.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace cosm::ta { class polled_task;}

namespace cosm::ta::ds {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class tdgraph : public rer::client<tdgraph> {
 public:
  using vertex_type = std::unique_ptr<polled_task>;
  using walk_cb = std::function<void(polled_task*)>;
  using const_walk_cb = std::function<void(const polled_task*)>;
  using vertex_vector = std::vector<vertex_type>;

  static polled_task* vertex_parent(const tdgraph& graph, const polled_task* v);

  tdgraph(void);
  ~tdgraph(void) override = default;

  /* Necessary for use in std::variant */
  tdgraph(const tdgraph&) = default;
  tdgraph& operator=(const tdgraph&) = delete;

  polled_task* vertex_parent(const polled_task* v) const;

  status_t set_root(std::unique_ptr<polled_task> v);

  const polled_task* root(void) const RCPPSW_PURE;
  polled_task* root(void) RCPPSW_PURE;

  status_t set_children(const polled_task* parent, vertex_vector children);
  status_t set_children(const std::string& parent, vertex_vector children);

  std::vector<polled_task*> children(const polled_task* parent) const;

  int vertex_depth(const polled_task* v) const;

  int vertex_id(const polled_task* v) const;

  const polled_task* find_vertex(const std::string& task_name) const;
  polled_task* find_vertex(const std::string& task_name);

  size_t n_vertices(void) const { return boost::num_vertices(m_impl); }

  const polled_task* find_vertex(int id) const RCPPSW_PURE;
  polled_task* find_vertex(int id) RCPPSW_PURE;

  void walk(const walk_cb& f);
  void walk(const const_walk_cb& f) const;

 private:
  using vertex_type_impl = std::shared_ptr<polled_task>;
  using graph_impl = boost::adjacency_list<boost::vecS,
                                           boost::vecS,
                                           boost::bidirectionalS,
                                           vertex_type_impl>;
  using vertex_desc = boost::graph_traits<graph_impl>::vertex_descriptor;
  using edge_desc = boost::graph_traits<graph_impl>::edge_descriptor;

  using vertex_iterator = boost::graph_traits<graph_impl>::vertex_iterator;
  using edge_iterator = boost::graph_traits<graph_impl>::edge_iterator;
  using out_edge_iterator = boost::graph_traits<graph_impl>::out_edge_iterator;
  using in_edge_iterator = boost::graph_traits<graph_impl>::in_edge_iterator;

  vertex_iterator find_vertex_impl(const polled_task* v) const;
  vertex_iterator find_vertex_impl(const std::string& v) const;

  size_t vertex_depth_impl(const polled_task* v, int depth) const;

  /* clang-format off */
  polled_task*       m_root{nullptr};
  graph_impl         m_impl{};
  /* clang-format on */
};

} /* namespace cosm::ta::ds */