Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_simple_flowgraph_detail.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_SIMPLE_FLOWGRAPH_DETAIL_H
24 #define INCLUDED_GR_SIMPLE_FLOWGRAPH_DETAIL_H
25
26 #include <gr_basic_block.h>
27 #include <gr_simple_flowgraph.h>
28 #include <iostream>
29
30 #define GR_FIXED_BUFFER_SIZE (32*(1L<<10))
31
32 class gr_edge;
33 typedef boost::shared_ptr<gr_edge> gr_edge_sptr;
34 gr_edge_sptr gr_make_edge(const gr_endpoint &src, const gr_endpoint &dst);
35
36 class gr_edge
37 {
38 private:
39   friend gr_edge_sptr gr_make_edge(const gr_endpoint &src, const gr_endpoint &dst);
40   gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
41
42   gr_endpoint d_src;
43   gr_endpoint d_dst;
44
45 public:
46   ~gr_edge();
47
48   const gr_endpoint &src() const { return d_src; }
49   const gr_endpoint &dst() const { return d_dst; }
50 };
51
52 typedef std::vector<gr_edge_sptr> gr_edge_vector_t;
53 typedef std::vector<gr_edge_sptr>::iterator gr_edge_viter_t;
54
55 class gr_simple_flowgraph_detail
56 {
57 private:
58   friend class gr_simple_flowgraph;
59   friend class gr_runtime_impl;
60   friend class gr_hier_block2_detail;
61   friend class topo_block_cmp;
62     
63   gr_simple_flowgraph_detail() : d_blocks(), d_edges() { }
64
65   gr_basic_block_vector_t d_blocks;
66   gr_edge_vector_t  d_edges;
67   static const unsigned int s_fixed_buffer_size = GR_FIXED_BUFFER_SIZE;
68     
69   void reset();
70   void connect(const gr_endpoint &src, const gr_endpoint &dst);
71   void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
72   void check_valid_port(gr_io_signature_sptr sig, int port);
73   void check_dst_not_used(const gr_endpoint &dst);
74   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
75   void validate();
76   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
77   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs); 
78   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
79   void setup_connections();
80   void merge_connections(gr_simple_flowgraph_sptr sfg);
81
82   void connect_block_inputs(gr_basic_block_sptr block);
83   gr_block_detail_sptr allocate_block_detail(gr_basic_block_sptr block, 
84                                              gr_block_detail_sptr old_detail=gr_block_detail_sptr());
85   gr_buffer_sptr allocate_buffer(gr_basic_block_sptr block, int port);
86   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
87   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
88   gr_edge_sptr calc_upstream_edge(gr_basic_block_sptr block, int port);
89   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
90   gr_basic_block_vector_t calc_used_blocks();
91   std::vector<gr_block_vector_t> partition();
92   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
93   gr_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
94   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
95   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
96   bool source_p(gr_basic_block_sptr block);
97   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
98   void topological_dfs_visit(gr_basic_block_sptr block, gr_block_vector_t &output);
99   bool has_block_p(gr_basic_block_sptr block);
100
101 public:
102   ~gr_simple_flowgraph_detail();
103 };
104
105 inline std::ostream&
106 operator <<(std::ostream &os, const gr_endpoint endp)
107 {
108   os << endp.block() << ":" << endp.port();
109   return os;
110 }
111
112 inline std::ostream&
113 operator <<(std::ostream &os, const gr_edge_sptr edge)
114 {
115   os << edge->src() << "->" << edge->dst();
116   return os;
117 }
118
119 inline void
120 enumerate_edges(gr_edge_vector_t &edges)
121 {
122   std::cout << "Edge list has " << edges.size() << " elements" << std::endl;
123   for(gr_edge_viter_t p = edges.begin(); p != edges.end(); p++)
124     std::cout << *p << std::endl;
125 }
126
127 #endif /* INCLUDED_GR_SIMPLE_FLOWGRAPH_H */