gr_flowgraph.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006,2007 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_FLOWGRAPH_H
00024 #define INCLUDED_GR_FLOWGRAPH_H
00025 
00026 #include <gr_basic_block.h>
00027 #include <iostream>
00028 
00034 class gr_endpoint
00035 {
00036 private:
00037   gr_basic_block_sptr d_basic_block;
00038   int d_port;
00039 
00040 public:
00041   gr_endpoint() : d_basic_block(), d_port(0) { }
00042   gr_endpoint(gr_basic_block_sptr block, int port) { d_basic_block = block; d_port = port; }
00043   gr_basic_block_sptr block() const { return d_basic_block; }
00044   int port() const { return d_port; }
00045 
00046   bool operator==(const gr_endpoint &other) const;
00047 };    
00048 
00049 inline bool gr_endpoint::operator==(const gr_endpoint &other) const
00050 {
00051   return (d_basic_block == other.d_basic_block && 
00052           d_port == other.d_port);
00053 }
00054 
00055 // Hold vectors of gr_endpoint objects
00056 typedef std::vector<gr_endpoint> gr_endpoint_vector_t;
00057 typedef std::vector<gr_endpoint>::iterator gr_endpoint_viter_t;
00058 
00063 class gr_edge
00064 {
00065 public:
00066   gr_edge() : d_src(), d_dst() { };
00067   gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
00068   ~gr_edge();
00069 
00070   const gr_endpoint &src() const { return d_src; }
00071   const gr_endpoint &dst() const { return d_dst; }
00072 
00073 private:
00074   gr_endpoint d_src;
00075   gr_endpoint d_dst;
00076 };
00077 
00078 // Hold vectors of gr_edge objects
00079 typedef std::vector<gr_edge> gr_edge_vector_t;
00080 typedef std::vector<gr_edge>::iterator gr_edge_viter_t;
00081 
00082 
00083 // Create a shared pointer to a heap allocated flowgraph
00084 // (types defined in gr_runtime_types.h)
00085 gr_flowgraph_sptr gr_make_flowgraph();
00086 
00091 class gr_flowgraph
00092 {
00093 public:
00094   friend gr_flowgraph_sptr gr_make_flowgraph();
00095 
00096   // Destruct an arbitrary flowgraph
00097   ~gr_flowgraph();
00098 
00099   // Connect two endpoints
00100   void connect(const gr_endpoint &src, const gr_endpoint &dst);
00101 
00102   // Disconnect two endpoints
00103   void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
00104 
00105   // Connect an output port to an input port (convenience)
00106   void connect(gr_basic_block_sptr src_block, int src_port,
00107                gr_basic_block_sptr dst_block, int dst_port);
00108 
00109   // Disconnect an input port from an output port (convenience)
00110   void disconnect(gr_basic_block_sptr src_block, int src_port,
00111                   gr_basic_block_sptr dst_block, int dst_port);
00112 
00113   // Validate connectivity, raise exception if invalid
00114   void validate();
00115 
00116   // Clear existing flowgraph
00117   void clear();
00118 
00119   // Return vector of edges
00120   const gr_edge_vector_t &edges() const { return d_edges; }
00121 
00122   // Return vector of connected blocks
00123   gr_basic_block_vector_t calc_used_blocks();
00124 
00125   // Return vector of vectors of disjointly connected blocks, topologically
00126   // sorted.
00127   std::vector<gr_basic_block_vector_t> partition();
00128 
00129 protected:
00130   gr_basic_block_vector_t d_blocks;
00131   gr_edge_vector_t d_edges;
00132 
00133   gr_flowgraph();
00134   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs); 
00135   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
00136   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
00137   bool has_block_p(gr_basic_block_sptr block);
00138   gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
00139 
00140 private:
00141 
00142   void check_valid_port(gr_io_signature_sptr sig, int port);
00143   void check_dst_not_used(const gr_endpoint &dst);
00144   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
00145   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
00146   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
00147 
00148   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
00149   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00150   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00151   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00152   gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
00153   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
00154   bool source_p(gr_basic_block_sptr block);
00155   void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
00156 };
00157 
00158 // Convenience functions
00159 inline
00160 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
00161                       gr_basic_block_sptr dst_block, int dst_port)
00162 {
00163   connect(gr_endpoint(src_block, src_port),
00164           gr_endpoint(dst_block, dst_port));
00165 }
00166 
00167 inline
00168 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
00169                          gr_basic_block_sptr dst_block, int dst_port)
00170 {
00171   disconnect(gr_endpoint(src_block, src_port),
00172              gr_endpoint(dst_block, dst_port));
00173 }
00174 
00175 inline std::ostream&
00176 operator <<(std::ostream &os, const gr_endpoint endp)
00177 {
00178   os << endp.block() << ":" << endp.port();
00179   return os;
00180 }
00181 
00182 inline std::ostream&
00183 operator <<(std::ostream &os, const gr_edge edge)
00184 {
00185   os << edge.src() << "->" << edge.dst();
00186   return os;
00187 }
00188 
00189 #endif /* INCLUDED_GR_FLOWGRAPH_H */

Generated on Thu Mar 5 09:01:11 2009 for GNU Radio 3.1.3 by  doxygen 1.5.8