GNU Radio 3.7.3 C++ API
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)
139 
140  /*!
141  * \brief Class representing a directed, acyclic graph of basic blocks
142  * \ingroup internal
143  */
145  {
146  public:
148 
149  // Destruct an arbitrary flowgraph
150  ~flowgraph();
151 
152  // Connect two endpoints
153  void connect(const endpoint &src, const endpoint &dst);
154 
155  // Disconnect two endpoints
156  void disconnect(const endpoint &src, const endpoint &dst);
157 
158  // Connect an output port to an input port (convenience)
159  void connect(basic_block_sptr src_block, int src_port,
160  basic_block_sptr dst_block, int dst_port);
161 
162  // Disconnect an input port from an output port (convenience)
163  void disconnect(basic_block_sptr src_block, int src_port,
164  basic_block_sptr dst_block, int dst_port);
165 
166  // Connect two msg endpoints
167  void connect(const msg_endpoint &src, const msg_endpoint &dst);
168 
169  // Disconnect two msg endpoints
170  void disconnect(const msg_endpoint &src, const msg_endpoint &dst);
171 
172  // Validate connectivity, raise exception if invalid
173  void validate();
174 
175  // Clear existing flowgraph
176  void clear();
177 
178  // Return vector of edges
179  const edge_vector_t &edges() const { return d_edges; }
180 
181  // Return vector of msg edges
182  const msg_edge_vector_t &msg_edges() const { return d_msg_edges; }
183 
184  // Return vector of connected blocks
185  basic_block_vector_t calc_used_blocks();
186 
187  // Return toplogically sorted vector of blocks. All the sources come first.
188  basic_block_vector_t topological_sort(basic_block_vector_t &blocks);
189 
190  // Return vector of vectors of disjointly connected blocks,
191  // topologically sorted.
192  std::vector<basic_block_vector_t> partition();
193 
194  protected:
198 
199  flowgraph();
200  std::vector<int> calc_used_ports(basic_block_sptr block, bool check_inputs);
201  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block, int port);
202  edge_vector_t calc_upstream_edges(basic_block_sptr block);
203  bool has_block_p(basic_block_sptr block);
204  edge calc_upstream_edge(basic_block_sptr block, int port);
205 
206  private:
207  void check_valid_port(gr::io_signature::sptr sig, int port);
208  void check_valid_port(const msg_endpoint &e);
209  void check_dst_not_used(const endpoint &dst);
210  void check_type_match(const endpoint &src, const endpoint &dst);
211  edge_vector_t calc_connections(basic_block_sptr block, bool check_inputs); // false=use outputs
212  void check_contiguity(basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
213 
214  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block);
215  basic_block_vector_t calc_reachable_blocks(basic_block_sptr block, basic_block_vector_t &blocks);
216  void reachable_dfs_visit(basic_block_sptr block, basic_block_vector_t &blocks);
217  basic_block_vector_t calc_adjacent_blocks(basic_block_sptr block, basic_block_vector_t &blocks);
218  basic_block_vector_t sort_sources_first(basic_block_vector_t &blocks);
219  bool source_p(basic_block_sptr block);
220  void topological_dfs_visit(basic_block_sptr block, basic_block_vector_t &output);
221  };
222 
223  // Convenience functions
224  inline
225  void flowgraph::connect(basic_block_sptr src_block, int src_port,
226  basic_block_sptr dst_block, int dst_port)
227  {
228  connect(endpoint(src_block, src_port),
229  endpoint(dst_block, dst_port));
230  }
231 
232  inline
233  void flowgraph::disconnect(basic_block_sptr src_block, int src_port,
234  basic_block_sptr dst_block, int dst_port)
235  {
236  disconnect(endpoint(src_block, src_port),
237  endpoint(dst_block, dst_port));
238  }
239 
240  inline std::ostream&
241  operator <<(std::ostream &os, const endpoint endp)
242  {
243  os << endp.block()->alias() << ":" << endp.port();
244  return os;
245  }
246 
247  inline std::ostream&
248  operator <<(std::ostream &os, const edge edge)
249  {
250  os << edge.src() << "->" << edge.dst();
251  return os;
252  }
253 
254  inline std::ostream&
255  operator <<(std::ostream &os, const msg_endpoint endp)
256  {
257  os << endp.block()->alias() << ":" << pmt::symbol_to_string(endp.port());
258  return os;
259  }
260 
261  inline std::ostream&
262  operator <<(std::ostream &os, const msg_edge edge)
263  {
264  os << edge.src() << "->" << edge.dst();
265  return os;
266  }
267 
268  std::string dot_graph_fg (flowgraph_sptr fg);
269 
270 } /* namespace gr */
271 
272 #endif /* INCLUDED_GR_RUNTIME_FLOWGRAPH_H */
const msg_edge_vector_t & msg_edges() const
Definition: flowgraph.h:182
endpoint()
Definition: flowgraph.h:44
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:197
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
Definition: flowgraph.h:179
void connect(const endpoint &src, const endpoint &dst)
#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:195
const endpoint & dst() const
Definition: flowgraph.h:101
PMT_API bool equal(const pmt_t &x, const pmt_t &y)
edge_vector_t d_edges
Definition: flowgraph.h:196
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:362
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:367
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)
Class representing a msg connection between to graph msg endpoints.
Definition: flowgraph.h:116
PMT_API const pmt_t PMT_NIL
bool operator==(const endpoint &other) const
Definition: flowgraph.h:52