GNU Radio 3.6.4.1 C++ API
gr_flowgraph.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22 
23 #ifndef INCLUDED_GR_FLOWGRAPH_H
24 #define INCLUDED_GR_FLOWGRAPH_H
25 
26 #include <gr_core_api.h>
27 #include <gr_basic_block.h>
28 #include <iostream>
29 
30 /*!
31  * \brief Class representing a specific input or output graph endpoint
32  * \ingroup internal
33  */
35 {
36 private:
37  gr_basic_block_sptr d_basic_block;
38  int d_port;
39 
40 public:
41  gr_endpoint() : d_basic_block(), d_port(0) { }
42  gr_endpoint(gr_basic_block_sptr block, int port) { d_basic_block = block; d_port = port; }
43  gr_basic_block_sptr block() const { return d_basic_block; }
44  int port() const { return d_port; }
45 
46  bool operator==(const gr_endpoint &other) const;
47 };
48 
49 inline bool gr_endpoint::operator==(const gr_endpoint &other) const
50 {
51  return (d_basic_block == other.d_basic_block &&
52  d_port == other.d_port);
53 }
54 
56 {
57 private:
58  gr_basic_block_sptr d_basic_block;
59  pmt::pmt_t d_port;
60  bool d_is_hier;
61 public:
62  gr_msg_endpoint() : d_basic_block(), d_port(pmt::PMT_NIL) { }
63  gr_msg_endpoint(gr_basic_block_sptr block, pmt::pmt_t port, bool is_hier=false){ d_basic_block = block; d_port = port; d_is_hier = is_hier;}
64  gr_basic_block_sptr block() const { return d_basic_block; }
65  pmt::pmt_t port() const { return d_port; }
66  bool is_hier() const { return d_is_hier; }
67  void set_hier(bool h) { d_is_hier = h; }
68 
69  bool operator==(const gr_msg_endpoint &other) const;
70 
71 };
72 
73 inline bool gr_msg_endpoint::operator==(const gr_msg_endpoint &other) const
74 {
75  return (d_basic_block == other.d_basic_block &&
76  pmt::pmt_equal(d_port, other.d_port));
77 }
78 
79 
80 // Hold vectors of gr_endpoint objects
81 typedef std::vector<gr_endpoint> gr_endpoint_vector_t;
82 typedef std::vector<gr_endpoint>::iterator gr_endpoint_viter_t;
83 
84 /*!
85  *\brief Class representing a connection between to graph endpoints
86  *
87  */
89 {
90 public:
91  gr_edge() : d_src(), d_dst() { };
92  gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
93  ~gr_edge();
94 
95  const gr_endpoint &src() const { return d_src; }
96  const gr_endpoint &dst() const { return d_dst; }
97 
98 private:
99  gr_endpoint d_src;
100  gr_endpoint d_dst;
101 };
102 
103 
104 // Hold vectors of gr_edge objects
105 typedef std::vector<gr_edge> gr_edge_vector_t;
106 typedef std::vector<gr_edge>::iterator gr_edge_viter_t;
107 
108 
109 /*!
110  *\brief Class representing a msg connection between to graph msg endpoints
111  *
112  */
114 {
115 public:
116  gr_msg_edge() : d_src(), d_dst() { };
117  gr_msg_edge(const gr_msg_endpoint &src, const gr_msg_endpoint &dst) : d_src(src), d_dst(dst) { }
119 
120  const gr_msg_endpoint &src() const { return d_src; }
121  const gr_msg_endpoint &dst() const { return d_dst; }
122 
123 private:
124  gr_msg_endpoint d_src;
125  gr_msg_endpoint d_dst;
126 };
127 
128 // Hold vectors of gr_edge objects
129 typedef std::vector<gr_msg_edge> gr_msg_edge_vector_t;
130 typedef std::vector<gr_msg_edge>::iterator gr_msg_edge_viter_t;
131 
132 // Create a shared pointer to a heap allocated flowgraph
133 // (types defined in gr_runtime_types.h)
135 
136 /*!
137  * \brief Class representing a directed, acyclic graph of basic blocks
138  * \ingroup internal
139  */
141 {
142 public:
144 
145  // Destruct an arbitrary flowgraph
146  ~gr_flowgraph();
147 
148  // Connect two endpoints
149  void connect(const gr_endpoint &src, const gr_endpoint &dst);
150 
151  // Disconnect two endpoints
152  void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
153 
154  // Connect an output port to an input port (convenience)
155  void connect(gr_basic_block_sptr src_block, int src_port,
156  gr_basic_block_sptr dst_block, int dst_port);
157 
158  // Disconnect an input port from an output port (convenience)
159  void disconnect(gr_basic_block_sptr src_block, int src_port,
160  gr_basic_block_sptr dst_block, int dst_port);
161 
162  // Connect two msg endpoints
163  void connect(const gr_msg_endpoint &src, const gr_msg_endpoint &dst);
164 
165  // Disconnect two msg endpoints
166  void disconnect(const gr_msg_endpoint &src, const gr_msg_endpoint &dst);
167 
168  // Validate connectivity, raise exception if invalid
169  void validate();
170 
171  // Clear existing flowgraph
172  void clear();
173 
174  // Return vector of edges
175  const gr_edge_vector_t &edges() const { return d_edges; }
176 
177  // Return vector of msg edges
178  const gr_msg_edge_vector_t &msg_edges() const { return d_msg_edges; }
179 
180  // Return vector of connected blocks
181  gr_basic_block_vector_t calc_used_blocks();
182 
183  // Return toplogically sorted vector of blocks. All the sources come first.
184  gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
185 
186  // Return vector of vectors of disjointly connected blocks, topologically
187  // sorted.
188  std::vector<gr_basic_block_vector_t> partition();
189 
190 protected:
194 
195  gr_flowgraph();
196  std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs);
197  gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
198  gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
199  bool has_block_p(gr_basic_block_sptr block);
200  gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
201 
202 private:
203 
204  void check_valid_port(gr_io_signature_sptr sig, int port);
205  void check_valid_port(const gr_msg_endpoint &e);
206  void check_dst_not_used(const gr_endpoint &dst);
207  void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
208  gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
209  void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
210 
211  gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
212  gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
213  void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
214  gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
215  gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
216  bool source_p(gr_basic_block_sptr block);
217  void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
218 };
219 
220 // Convenience functions
221 inline
222 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
223  gr_basic_block_sptr dst_block, int dst_port)
224 {
225  connect(gr_endpoint(src_block, src_port),
226  gr_endpoint(dst_block, dst_port));
227 }
228 
229 inline
230 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
231  gr_basic_block_sptr dst_block, int dst_port)
232 {
233  disconnect(gr_endpoint(src_block, src_port),
234  gr_endpoint(dst_block, dst_port));
235 }
236 
237 inline std::ostream&
238 operator <<(std::ostream &os, const gr_endpoint endp)
239 {
240  os << endp.block() << ":" << endp.port();
241  return os;
242 }
243 
244 inline std::ostream&
245 operator <<(std::ostream &os, const gr_edge edge)
246 {
247  os << edge.src() << "->" << edge.dst();
248  return os;
249 }
250 
251 #endif /* INCLUDED_GR_FLOWGRAPH_H */