Merge r6160:6168 from jcorgan/fg into trunk.
[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  *
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  *
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 vector of vectors of disjointly connected blocks, topologically
125   // sorted.
126   std::vector<gr_basic_block_vector_t> partition();
127
128 protected:
129   gr_basic_block_vector_t d_blocks;
130   gr_edge_vector_t d_edges;
131
132   gr_flowgraph();
133   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs); 
134   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
135   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
136   bool has_block_p(gr_basic_block_sptr block);
137   gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
138
139 private:
140
141   void check_valid_port(gr_io_signature_sptr sig, int port);
142   void check_dst_not_used(const gr_endpoint &dst);
143   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
144   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
145   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
146
147   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
148   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
149   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
150   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
151   gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
152   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
153   bool source_p(gr_basic_block_sptr block);
154   void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
155 };
156
157 // Convenience functions
158 inline
159 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
160                       gr_basic_block_sptr dst_block, int dst_port)
161 {
162   connect(gr_endpoint(src_block, src_port),
163           gr_endpoint(dst_block, dst_port));
164 }
165
166 inline
167 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
168                          gr_basic_block_sptr dst_block, int dst_port)
169 {
170   disconnect(gr_endpoint(src_block, src_port),
171              gr_endpoint(dst_block, dst_port));
172 }
173
174 inline std::ostream&
175 operator <<(std::ostream &os, const gr_endpoint endp)
176 {
177   os << endp.block() << ":" << endp.port();
178   return os;
179 }
180
181 inline std::ostream&
182 operator <<(std::ostream &os, const gr_edge edge)
183 {
184   os << edge.src() << "->" << edge.dst();
185   return os;
186 }
187
188 #endif /* INCLUDED_GR_FLOWGRAPH_H */