GNU Radio Manual and C++ API Reference  3.9.8.0
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)
144 GR_RUNTIME_API flowgraph_sptr make_flowgraph();
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  */
234  basic_block_vector_t calc_used_blocks();
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  */
243  basic_block_vector_t topological_sort(basic_block_vector_t& blocks);
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:
257 
258  flowgraph();
259  std::vector<int> calc_used_ports(basic_block_sptr block, bool check_inputs);
260  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block, int port);
261  edge_vector_t calc_upstream_edges(basic_block_sptr block);
262  bool has_block_p(basic_block_sptr block);
263  edge calc_upstream_edge(basic_block_sptr block, int port);
264 
265 private:
266  void check_valid_port(gr::io_signature::sptr sig, int port);
267  void check_valid_port(const msg_endpoint& e);
268  void check_dst_not_used(const endpoint& dst);
269  void check_type_match(const endpoint& src, const endpoint& dst);
270  edge_vector_t calc_connections(basic_block_sptr block,
271  bool check_inputs); // false=use outputs
272  void check_contiguity(basic_block_sptr block,
273  const std::vector<int>& used_ports,
274  bool check_inputs);
275 
276  basic_block_vector_t calc_downstream_blocks(basic_block_sptr block);
277  basic_block_vector_t calc_reachable_blocks(basic_block_sptr block,
278  basic_block_vector_t& blocks);
279  void reachable_dfs_visit(basic_block_sptr block, basic_block_vector_t& blocks);
280  basic_block_vector_t calc_adjacent_blocks(basic_block_sptr block,
281  basic_block_vector_t& blocks);
282  basic_block_vector_t sort_sources_first(basic_block_vector_t& blocks);
283  bool source_p(basic_block_sptr block);
284  void topological_dfs_visit(basic_block_sptr block, basic_block_vector_t& output);
285 };
286 
287 // Convenience functions
288 inline void flowgraph::connect(basic_block_sptr src_block,
289  int src_port,
290  basic_block_sptr dst_block,
291  int dst_port)
292 {
293  connect(endpoint(src_block, src_port), endpoint(dst_block, dst_port));
294 }
295 
296 inline void flowgraph::disconnect(basic_block_sptr src_block,
297  int src_port,
298  basic_block_sptr dst_block,
299  int dst_port)
300 {
301  disconnect(endpoint(src_block, src_port), endpoint(dst_block, dst_port));
302 }
303 
304 inline std::ostream& operator<<(std::ostream& os, const endpoint endp)
305 {
306  os << endp.identifier();
307  return os;
308 }
309 
310 inline std::ostream& operator<<(std::ostream& os, const edge edge)
311 {
312  os << edge.identifier();
313  return os;
314 }
315 
316 inline std::ostream& operator<<(std::ostream& os, const msg_endpoint endp)
317 {
318  os << endp.identifier();
319  return os;
320 }
321 
322 inline std::ostream& operator<<(std::ostream& os, const msg_edge edge)
323 {
324  os << edge.identifier();
325  return os;
326 }
327 
328 std::string dot_graph_fg(flowgraph_sptr fg);
329 
330 } /* namespace gr */
331 
332 #endif /* INCLUDED_GR_RUNTIME_FLOWGRAPH_H */
const msg_edge_vector_t & msg_edges() const
Get vector of message edges.
Definition: flowgraph.h:224
int port() const
Definition: flowgraph.h:39
endpoint()
Definition: flowgraph.h:32
bool operator==(const endpoint &other) const
Definition: flowgraph.h:48
std::shared_ptr< io_signature > sptr
Definition: io_signature.h:34
std::string dot_graph_fg(flowgraph_sptr fg)
std::vector< msg_edge >::iterator msg_edge_viter_t
Definition: flowgraph.h:140
const edge_vector_t & edges() const
Get vector of edges.
Definition: flowgraph.h:219
basic_block_sptr block() const
Definition: flowgraph.h:68
std::string identifier() const
Definition: flowgraph.h:128
~msg_edge()
Definition: flowgraph.h:124
msg_edge_vector_t d_msg_edges
Definition: flowgraph.h:254
msg_endpoint()
Definition: flowgraph.h:61
const endpoint & src() const
Definition: flowgraph.h:99
const endpoint & dst() const
Definition: flowgraph.h:100
basic_block_sptr block() const
Definition: flowgraph.h:38
Class representing a directed, acyclic graph of basic blocks.
Definition: flowgraph.h:150
std::string identifier() const
Definition: flowgraph.h:101
PMT_API const std::string symbol_to_string(const pmt_t &sym)
std::vector< endpoint > endpoint_vector_t
Definition: flowgraph.h:86
void connect(const endpoint &src, const endpoint &dst)
Connect two endpoints.
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
edge()
Definition: flowgraph.h:95
basic_block_vector_t d_blocks
Definition: flowgraph.h:252
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29
PMT_API bool equal(const pmt_t &x, const pmt_t &y)
pmt::pmt_t port() const
Definition: flowgraph.h:69
std::string identifier() const
Definition: flowgraph.h:72
edge_vector_t d_edges
Definition: flowgraph.h:253
gr::logger_ptr d_debug_logger
Definition: flowgraph.h:256
bool is_hier() const
Definition: flowgraph.h:70
#define PMT_NIL
Definition: pmt.h:122
std::vector< endpoint >::iterator endpoint_viter_t
Definition: flowgraph.h:87
Class representing a specific input or output graph endpoint.
Definition: flowgraph.h:25
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:33
std::vector< basic_block_sptr > basic_block_vector_t
Definition: basic_block.h:419
log4cpp::Category * logger_ptr
GR_LOG macrosThese macros wrap the standard LOG4CPP_LEVEL macros. The availablie macros are: LOG_DEBU...
Definition: logger.h:60
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:424
std::vector< edge >::iterator edge_viter_t
Definition: flowgraph.h:113
gr::logger_ptr d_logger
Definition: flowgraph.h:255
msg_endpoint(basic_block_sptr block, pmt::pmt_t port, bool is_hier=false)
Definition: flowgraph.h:62
const msg_endpoint & dst() const
Definition: flowgraph.h:127
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:53
void set_hier(bool h)
Definition: flowgraph.h:71
std::vector< msg_edge > msg_edge_vector_t
Definition: flowgraph.h:139
msg_edge()
Definition: flowgraph.h:122
GR_RUNTIME_API flowgraph_sptr make_flowgraph()
bool operator==(const msg_endpoint &other) const
Definition: flowgraph.h:80
The abstract base class for all &#39;terminal&#39; processing blocks.A signal processing flow is constructed ...
Definition: gnuradio-runtime/include/gnuradio/block.h:59
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
Definition: pmt.h:39
std::string identifier() const
Definition: flowgraph.h:40
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:84
const msg_endpoint & src() const
Definition: flowgraph.h:126