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