GNU Radio Manual and C++ API Reference  3.7.14.0
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
flowgraph.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007,2013 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_RUNTIME_FLOWGRAPH_H
24 #define INCLUDED_GR_RUNTIME_FLOWGRAPH_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/basic_block.h>
28 #include <gnuradio/io_signature.h>
29 #include <iostream>
30 
31 namespace gr {
32 
33 /*!
34  * \brief Class representing a specific input or output graph endpoint
35  * \ingroup internal
36  */
38 {
39 private:
40  basic_block_sptr d_basic_block;
41  int d_port;
42 
43 public:
44  endpoint() : d_basic_block(), d_port(0) {}
45  endpoint(basic_block_sptr block, int port)
46  {
47  d_basic_block = block;
48  d_port = port;
49  }
50  basic_block_sptr block() const { return d_basic_block; }
51  int port() const { return d_port; }
52 
53  bool operator==(const endpoint& other) const;
54 };
55 
56 inline bool endpoint::operator==(const endpoint& other) const
57 {
58  return (d_basic_block == other.d_basic_block && d_port == other.d_port);
59 }
60 
62 {
63 private:
64  basic_block_sptr d_basic_block;
65  pmt::pmt_t d_port;
66  bool d_is_hier;
67 
68 public:
69  msg_endpoint() : d_basic_block(), d_port(pmt::PMT_NIL) {}
70  msg_endpoint(basic_block_sptr block, pmt::pmt_t port, bool is_hier = false)
71  {
72  d_basic_block = block;
73  d_port = port;
74  d_is_hier = is_hier;
75  }
76  basic_block_sptr block() const { return d_basic_block; }
77  pmt::pmt_t port() const { return d_port; }
78  bool is_hier() const { return d_is_hier; }
79  void set_hier(bool h) { d_is_hier = h; }
80 
81  bool operator==(const msg_endpoint& other) const;
82 };
83 
84 inline bool msg_endpoint::operator==(const msg_endpoint& other) const
85 {
86  return (d_basic_block == other.d_basic_block && pmt::equal(d_port, other.d_port));
87 }
88 
89 // Hold vectors of gr::endpoint objects
90 typedef std::vector<endpoint> endpoint_vector_t;
91 typedef std::vector<endpoint>::iterator endpoint_viter_t;
92 
93 /*!
94  *\brief Class representing a connection between to graph endpoints
95  */
97 {
98 public:
99  edge() : d_src(), d_dst(){};
100  edge(const endpoint& src, const endpoint& dst) : d_src(src), d_dst(dst) {}
101  ~edge();
102 
103  const endpoint& src() const { return d_src; }
104  const endpoint& dst() const { return d_dst; }
105 
106 private:
107  endpoint d_src;
108  endpoint d_dst;
109 };
110 
111 // Hold vectors of gr::edge objects
112 typedef std::vector<edge> edge_vector_t;
113 typedef std::vector<edge>::iterator edge_viter_t;
114 
115 
116 /*!
117  *\brief Class representing a msg connection between to graph msg endpoints
118  */
120 {
121 public:
122  msg_edge() : d_src(), d_dst(){};
123  msg_edge(const msg_endpoint& src, const msg_endpoint& dst) : d_src(src), d_dst(dst) {}
125 
126  const msg_endpoint& src() const { return d_src; }
127  const msg_endpoint& dst() const { return d_dst; }
128 
129 private:
130  msg_endpoint d_src;
131  msg_endpoint d_dst;
132 };
133 
134 // Hold vectors of gr::msg_edge objects
135 typedef std::vector<msg_edge> msg_edge_vector_t;
136 typedef std::vector<msg_edge>::iterator msg_edge_viter_t;
137 
138 // Create a shared pointer to a heap allocated flowgraph
139 // (types defined in runtime_types.h)
140 GR_RUNTIME_API flowgraph_sptr make_flowgraph();
141 
142 /*!
143  * \brief Class representing a directed, acyclic graph of basic blocks
144  * \ingroup internal
145  */
147 {
148 public:
149  friend GR_RUNTIME_API flowgraph_sptr make_flowgraph();
150 
151  /*!
152  * \brief Destruct an arbitrary flowgraph
153  */
154  virtual ~flowgraph();
155 
156  /*!
157  * \brief Connect two endpoints
158  * \details
159  * Checks the validity of both endpoints, and whether the
160  * destination is unused so far, then adds the edge to the internal list of
161  * edges.
162  */
163  void connect(const endpoint& src, const endpoint& dst);
164 
165  /*!
166  * \brief Disconnect two endpoints
167  */
168  void disconnect(const endpoint& src, const endpoint& dst);
169 
170  /*!
171  * \brief convenience wrapper; used to connect two endpoints
172  */
173  void connect(basic_block_sptr src_block,
174  int src_port,
175  basic_block_sptr dst_block,
176  int dst_port);
177 
178  /*!
179  * \brief convenience wrapper; used to disconnect two endpoints
180  */
181  void disconnect(basic_block_sptr src_block,
182  int src_port,
183  basic_block_sptr dst_block,
184  int dst_port);
185 
186  /*!
187  * \brief Connect two message endpoints
188  * \details
189  * Checks the validity of both endpoints, then adds the edge to the
190  * internal list of edges.
191  */
192  void connect(const msg_endpoint& src, const msg_endpoint& dst);
193 
194  /*!
195  * \brief Disconnect two message endpoints
196  */
197  void disconnect(const msg_endpoint& src, const msg_endpoint& dst);
198 
199  /*!
200  * \brief Validate flow graph
201  * \details
202  * Gathers all used blocks, checks the contiguity of all connected in- and
203  * outputs, and calls the check_topology method of each block.
204  */
205  void validate();
206 
207  /*!
208  * \brief Clear existing flowgraph
209  */
210  void clear();
211 
212  /*!
213  * \brief Get vector of edges
214  */
215  const edge_vector_t& edges() const { return d_edges; }
216 
217  /*!
218  * \brief Get vector of message edges
219  */
220  const msg_edge_vector_t& msg_edges() const { return d_msg_edges; }
221 
222  /*!
223  * \brief calculates all used blocks in a flow graph
224  * \details
225  * Iterates over all message edges and stream edges, noting both endpoints in a
226  * vector.
227  *
228  * \return a unique vector of used blocks
229  */
230  basic_block_vector_t calc_used_blocks();
231 
232  /*!
233  * \brief topologically sort blocks
234  * \details
235  * Uses depth-first search to return a sorted vector of blocks
236  *
237  * \return toplogically sorted vector of blocks. All the sources come first.
238  */
239  basic_block_vector_t topological_sort(basic_block_vector_t& blocks);
240 
241  /*!
242  * \brief Calculate vector of disjoint graph partions
243  * \return vector of disjoint vectors of topologically sorted blocks
244  */
245  std::vector<basic_block_vector_t> partition();
246 
247 protected:
251 
252  flowgraph();
253  std::vector<int> calc_used_ports(basic_block_sptr block, bool check_inputs);
254  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block, int port);
255  edge_vector_t calc_upstream_edges(basic_block_sptr block);
256  bool has_block_p(basic_block_sptr block);
257  edge calc_upstream_edge(basic_block_sptr block, int port);
258 
259 private:
260  void check_valid_port(gr::io_signature::sptr sig, int port);
261  void check_valid_port(const msg_endpoint& e);
262  void check_dst_not_used(const endpoint& dst);
263  void check_type_match(const endpoint& src, const endpoint& dst);
264  edge_vector_t calc_connections(basic_block_sptr block,
265  bool check_inputs); // false=use outputs
266  void check_contiguity(basic_block_sptr block,
267  const std::vector<int>& used_ports,
268  bool check_inputs);
269 
270  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block);
271  basic_block_vector_t calc_reachable_blocks(basic_block_sptr block,
272  basic_block_vector_t& blocks);
273  void reachable_dfs_visit(basic_block_sptr block, basic_block_vector_t& blocks);
274  basic_block_vector_t calc_adjacent_blocks(basic_block_sptr block,
275  basic_block_vector_t& blocks);
276  basic_block_vector_t sort_sources_first(basic_block_vector_t& blocks);
277  bool source_p(basic_block_sptr block);
278  void topological_dfs_visit(basic_block_sptr block, basic_block_vector_t& output);
279 };
280 
281 // Convenience functions
282 inline void flowgraph::connect(basic_block_sptr src_block,
283  int src_port,
284  basic_block_sptr dst_block,
285  int dst_port)
286 {
287  connect(endpoint(src_block, src_port), endpoint(dst_block, dst_port));
288 }
289 
290 inline void flowgraph::disconnect(basic_block_sptr src_block,
291  int src_port,
292  basic_block_sptr dst_block,
293  int dst_port)
294 {
295  disconnect(endpoint(src_block, src_port), endpoint(dst_block, dst_port));
296 }
297 
298 inline std::ostream& operator<<(std::ostream& os, const endpoint endp)
299 {
300  os << endp.block()->alias() << ":" << endp.port();
301  return os;
302 }
303 
304 inline std::ostream& operator<<(std::ostream& os, const edge edge)
305 {
306  os << edge.src() << "->" << edge.dst();
307  return os;
308 }
309 
310 inline std::ostream& operator<<(std::ostream& os, const msg_endpoint endp)
311 {
312  os << endp.block()->alias() << ":" << pmt::symbol_to_string(endp.port());
313  return os;
314 }
315 
316 inline std::ostream& operator<<(std::ostream& os, const msg_edge edge)
317 {
318  os << edge.src() << "->" << edge.dst();
319  return os;
320 }
321 
322 std::string dot_graph_fg(flowgraph_sptr fg);
323 
324 } /* namespace gr */
325 
326 #endif /* INCLUDED_GR_RUNTIME_FLOWGRAPH_H */
const msg_edge_vector_t & msg_edges() const
Get vector of message edges.
Definition: flowgraph.h:220
endpoint()
Definition: flowgraph.h:44
boost::shared_ptr< io_signature > sptr
Definition: io_signature.h:46
std::string dot_graph_fg(flowgraph_sptr fg)
std::vector< msg_edge >::iterator msg_edge_viter_t
Definition: flowgraph.h:136
~msg_edge()
Definition: flowgraph.h:124
bool operator==(const msg_endpoint &other) const
Definition: flowgraph.h:84
msg_edge_vector_t d_msg_edges
Definition: flowgraph.h:250
msg_endpoint()
Definition: flowgraph.h:69
const msg_endpoint & dst() const
Definition: flowgraph.h:127
Class representing a directed, acyclic graph of basic blocks.
Definition: flowgraph.h:146
bool is_hier() const
Definition: flowgraph.h:78
const msg_endpoint & src() const
Definition: flowgraph.h:126
PMT_API const std::string symbol_to_string(const pmt_t &sym)
std::vector< endpoint > endpoint_vector_t
Definition: flowgraph.h:90
const edge_vector_t & edges() const
Get vector of edges.
Definition: flowgraph.h:215
void connect(const endpoint &src, const endpoint &dst)
Connect two endpoints.
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
edge()
Definition: flowgraph.h:99
int port() const
Definition: flowgraph.h:51
basic_block_sptr block() const
Definition: flowgraph.h:76
basic_block_vector_t d_blocks
Definition: flowgraph.h:248
const endpoint & dst() const
Definition: flowgraph.h:104
PMT_API bool equal(const pmt_t &x, const pmt_t &y)
edge_vector_t d_edges
Definition: flowgraph.h:249
#define PMT_NIL
Definition: pmt.h:103
std::vector< endpoint >::iterator endpoint_viter_t
Definition: flowgraph.h:91
Class representing a specific input or output graph endpoint.
Definition: flowgraph.h:37
edge(const endpoint &src, const endpoint &dst)
Definition: flowgraph.h:100
Class representing a connection between to graph endpoints.
Definition: flowgraph.h:96
endpoint(basic_block_sptr block, int port)
Definition: flowgraph.h:45
std::vector< basic_block_sptr > basic_block_vector_t
Definition: basic_block.h:422
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:427
std::vector< edge >::iterator edge_viter_t
Definition: flowgraph.h:113
pmt::pmt_t port() const
Definition: flowgraph.h:77
msg_endpoint(basic_block_sptr block, pmt::pmt_t port, bool is_hier=false)
Definition: flowgraph.h:70
msg_edge(const msg_endpoint &src, const msg_endpoint &dst)
Definition: flowgraph.h:123
std::vector< edge > edge_vector_t
Definition: flowgraph.h:112
Definition: flowgraph.h:61
basic_block_sptr block() const
Definition: flowgraph.h:50
void set_hier(bool h)
Definition: flowgraph.h:79
std::vector< msg_edge > msg_edge_vector_t
Definition: flowgraph.h:135
const endpoint & src() const
Definition: flowgraph.h:103
msg_edge()
Definition: flowgraph.h:122
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56
GR_RUNTIME_API flowgraph_sptr make_flowgraph()
The abstract base class for all 'terminal' processing blocks.A signal processing flow is constructed ...
Definition: block.h:65
void disconnect(const endpoint &src, const endpoint &dst)
Disconnect two endpoints.
Class representing a msg connection between to graph msg endpoints.
Definition: flowgraph.h:119
bool operator==(const endpoint &other) const
Definition: flowgraph.h:56