Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_flowgraph.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007 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_FLOWGRAPH_H
24 #define INCLUDED_GR_FLOWGRAPH_H
25
26 #include <gr_basic_block.h>
27 #include <iostream>
28
29 /*!
30  * \brief Class representing a specific input or output graph endpoint
31  * \ingroup internal
32  */
33 class gr_endpoint
34 {
35 private:
36   gr_basic_block_sptr d_basic_block;
37   int d_port;
38
39 public:
40   gr_endpoint() : d_basic_block(), d_port(0) { }
41   gr_endpoint(gr_basic_block_sptr block, int port) { d_basic_block = block; d_port = port; }
42   gr_basic_block_sptr block() const { return d_basic_block; }
43   int port() const { return d_port; }
44
45   bool operator==(const gr_endpoint &other) const;
46 };    
47
48 inline bool gr_endpoint::operator==(const gr_endpoint &other) const
49 {
50   return (d_basic_block == other.d_basic_block && 
51           d_port == other.d_port);
52 }
53
54 // Hold vectors of gr_endpoint objects
55 typedef std::vector<gr_endpoint> gr_endpoint_vector_t;
56 typedef std::vector<gr_endpoint>::iterator gr_endpoint_viter_t;
57
58 /*!
59  *\brief Class representing a connection between to graph endpoints
60  *
61  */
62 class gr_edge
63 {
64 public:
65   gr_edge() : d_src(), d_dst() { };
66   gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
67   ~gr_edge();
68
69   const gr_endpoint &src() const { return d_src; }
70   const gr_endpoint &dst() const { return d_dst; }
71
72 private:
73   gr_endpoint d_src;
74   gr_endpoint d_dst;
75 };
76
77 // Hold vectors of gr_edge objects
78 typedef std::vector<gr_edge> gr_edge_vector_t;
79 typedef std::vector<gr_edge>::iterator gr_edge_viter_t;
80
81
82 // Create a shared pointer to a heap allocated flowgraph
83 // (types defined in gr_runtime_types.h)
84 gr_flowgraph_sptr gr_make_flowgraph();
85
86 /*!
87  * \brief Class representing a directed, acyclic graph of basic blocks
88  * \ingroup internal
89  */
90 class gr_flowgraph
91 {
92 public:
93   friend gr_flowgraph_sptr gr_make_flowgraph();
94
95   // Destruct an arbitrary flowgraph
96   ~gr_flowgraph();
97
98   // Connect two endpoints
99   void connect(const gr_endpoint &src, const gr_endpoint &dst);
100
101   // Disconnect two endpoints
102   void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
103
104   // Connect an output port to an input port (convenience)
105   void connect(gr_basic_block_sptr src_block, int src_port,
106                gr_basic_block_sptr dst_block, int dst_port);
107
108   // Disconnect an input port from an output port (convenience)
109   void disconnect(gr_basic_block_sptr src_block, int src_port,
110                   gr_basic_block_sptr dst_block, int dst_port);
111
112   // Validate connectivity, raise exception if invalid
113   void validate();
114
115   // Clear existing flowgraph
116   void clear();
117
118   // Return vector of edges
119   const gr_edge_vector_t &edges() const { return d_edges; }
120
121   // Return vector of connected blocks
122   gr_basic_block_vector_t calc_used_blocks();
123
124   // Return toplogically sorted vector of blocks.  All the sources come first.
125   gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
126
127   // Return vector of vectors of disjointly connected blocks, topologically
128   // sorted.
129   std::vector<gr_basic_block_vector_t> partition();
130
131 protected:
132   gr_basic_block_vector_t d_blocks;
133   gr_edge_vector_t d_edges;
134
135   gr_flowgraph();
136   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs); 
137   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
138   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
139   bool has_block_p(gr_basic_block_sptr block);
140   gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
141
142 private:
143
144   void check_valid_port(gr_io_signature_sptr sig, int port);
145   void check_dst_not_used(const gr_endpoint &dst);
146   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
147   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
148   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
149
150   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
151   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
152   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
153   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
154   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
155   bool source_p(gr_basic_block_sptr block);
156   void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
157 };
158
159 // Convenience functions
160 inline
161 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
162                       gr_basic_block_sptr dst_block, int dst_port)
163 {
164   connect(gr_endpoint(src_block, src_port),
165           gr_endpoint(dst_block, dst_port));
166 }
167
168 inline
169 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
170                          gr_basic_block_sptr dst_block, int dst_port)
171 {
172   disconnect(gr_endpoint(src_block, src_port),
173              gr_endpoint(dst_block, dst_port));
174 }
175
176 inline std::ostream&
177 operator <<(std::ostream &os, const gr_endpoint endp)
178 {
179   os << endp.block() << ":" << endp.port();
180   return os;
181 }
182
183 inline std::ostream&
184 operator <<(std::ostream &os, const gr_edge edge)
185 {
186   os << edge.src() << "->" << edge.dst();
187   return os;
188 }
189
190 #endif /* INCLUDED_GR_FLOWGRAPH_H */