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