3 * Copyright 2006,2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
23 #ifndef INCLUDED_GR_FLOWGRAPH_H
24 #define INCLUDED_GR_FLOWGRAPH_H
26 #include <gr_basic_block.h>
30 * \brief Class representing a specific input or output graph endpoint
36 gr_basic_block_sptr d_basic_block;
40 gr_endpoint() : d_basic_block(), d_port(0) { }
41 gr_endpoint(gr_basic_block_sptr block, int port) { d_basic_block = block; d_port = port; }
42 gr_basic_block_sptr block() const { return d_basic_block; }
43 int port() const { return d_port; }
45 bool operator==(const gr_endpoint &other) const;
48 inline bool gr_endpoint::operator==(const gr_endpoint &other) const
50 return (d_basic_block == other.d_basic_block &&
51 d_port == other.d_port);
54 // Hold vectors of gr_endpoint objects
55 typedef std::vector<gr_endpoint> gr_endpoint_vector_t;
56 typedef std::vector<gr_endpoint>::iterator gr_endpoint_viter_t;
59 *\brief Class representing a connection between to graph endpoints
65 gr_edge() : d_src(), d_dst() { };
66 gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
69 const gr_endpoint &src() const { return d_src; }
70 const gr_endpoint &dst() const { return d_dst; }
77 // Hold vectors of gr_edge objects
78 typedef std::vector<gr_edge> gr_edge_vector_t;
79 typedef std::vector<gr_edge>::iterator gr_edge_viter_t;
82 // Create a shared pointer to a heap allocated flowgraph
83 // (types defined in gr_runtime_types.h)
84 gr_flowgraph_sptr gr_make_flowgraph();
87 * \brief Class representing a directed, acyclic graph of basic blocks
93 friend gr_flowgraph_sptr gr_make_flowgraph();
95 // Destruct an arbitrary flowgraph
98 // Connect two endpoints
99 void connect(const gr_endpoint &src, const gr_endpoint &dst);
101 // Disconnect two endpoints
102 void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
104 // Connect an output port to an input port (convenience)
105 void connect(gr_basic_block_sptr src_block, int src_port,
106 gr_basic_block_sptr dst_block, int dst_port);
108 // Disconnect an input port from an output port (convenience)
109 void disconnect(gr_basic_block_sptr src_block, int src_port,
110 gr_basic_block_sptr dst_block, int dst_port);
112 // Validate connectivity, raise exception if invalid
115 // Clear existing flowgraph
118 // Return vector of edges
119 const gr_edge_vector_t &edges() const { return d_edges; }
121 // Return vector of connected blocks
122 gr_basic_block_vector_t calc_used_blocks();
124 // Return toplogically sorted vector of blocks. All the sources come first.
125 gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
127 // Return vector of vectors of disjointly connected blocks, topologically
129 std::vector<gr_basic_block_vector_t> partition();
132 gr_basic_block_vector_t d_blocks;
133 gr_edge_vector_t d_edges;
136 std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs);
137 gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
138 gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
139 bool has_block_p(gr_basic_block_sptr block);
140 gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
144 void check_valid_port(gr_io_signature_sptr sig, int port);
145 void check_dst_not_used(const gr_endpoint &dst);
146 void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
147 gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
148 void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
150 gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
151 gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
152 void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
153 gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
154 gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
155 bool source_p(gr_basic_block_sptr block);
156 void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
159 // Convenience functions
161 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
162 gr_basic_block_sptr dst_block, int dst_port)
164 connect(gr_endpoint(src_block, src_port),
165 gr_endpoint(dst_block, dst_port));
169 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
170 gr_basic_block_sptr dst_block, int dst_port)
172 disconnect(gr_endpoint(src_block, src_port),
173 gr_endpoint(dst_block, dst_port));
177 operator <<(std::ostream &os, const gr_endpoint endp)
179 os << endp.block() << ":" << endp.port();
184 operator <<(std::ostream &os, const gr_edge edge)
186 os << edge.src() << "->" << edge.dst();
190 #endif /* INCLUDED_GR_FLOWGRAPH_H */