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