GNU Radio v3.6.2-209-ga47c5485 C++ API
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_core_api.h>
00027 #include <gr_basic_block.h>
00028 #include <iostream>
00029 
00030 /*!
00031  * \brief Class representing a specific input or output graph endpoint
00032  * \ingroup internal
00033  */
00034 class GR_CORE_API 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 
00059 /*!
00060  *\brief Class representing a connection between to graph endpoints
00061  *
00062  */
00063 class GR_CORE_API 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_CORE_API gr_flowgraph_sptr gr_make_flowgraph();
00086 
00087 /*!
00088  * \brief Class representing a directed, acyclic graph of basic blocks
00089  * \ingroup internal
00090  */
00091 class GR_CORE_API gr_flowgraph
00092 {
00093 public:
00094   friend GR_CORE_API 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   void add_msg_block(gr_basic_block_sptr blk);
00114 
00115   // Validate connectivity, raise exception if invalid
00116   void validate();
00117 
00118   // Clear existing flowgraph
00119   void clear();
00120 
00121   // Return vector of edges
00122   const gr_edge_vector_t &edges() const { return d_edges; }
00123 
00124   // Return vector of connected blocks
00125   gr_basic_block_vector_t calc_used_blocks();
00126 
00127   // Return toplogically sorted vector of blocks.  All the sources come first.
00128   gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
00129 
00130   // Return vector of vectors of disjointly connected blocks, topologically
00131   // sorted.
00132   std::vector<gr_basic_block_vector_t> partition();
00133   gr_basic_block_vector_t d_msgblocks;
00134 
00135 protected:
00136   gr_basic_block_vector_t d_blocks;
00137   gr_edge_vector_t d_edges;
00138 
00139   gr_flowgraph();
00140   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs);
00141   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
00142   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
00143   bool has_block_p(gr_basic_block_sptr block);
00144   gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
00145 
00146 private:
00147 
00148   void check_valid_port(gr_io_signature_sptr sig, int port);
00149   void check_dst_not_used(const gr_endpoint &dst);
00150   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
00151   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
00152   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
00153 
00154   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
00155   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00156   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00157   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00158   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
00159   bool source_p(gr_basic_block_sptr block);
00160   void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
00161 };
00162 
00163 // Convenience functions
00164 inline
00165 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
00166                       gr_basic_block_sptr dst_block, int dst_port)
00167 {
00168   connect(gr_endpoint(src_block, src_port),
00169           gr_endpoint(dst_block, dst_port));
00170 }
00171 
00172 inline
00173 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
00174                          gr_basic_block_sptr dst_block, int dst_port)
00175 {
00176   disconnect(gr_endpoint(src_block, src_port),
00177              gr_endpoint(dst_block, dst_port));
00178 }
00179 
00180 inline std::ostream&
00181 operator <<(std::ostream &os, const gr_endpoint endp)
00182 {
00183   os << endp.block() << ":" << endp.port();
00184   return os;
00185 }
00186 
00187 inline std::ostream&
00188 operator <<(std::ostream &os, const gr_edge edge)
00189 {
00190   os << edge.src() << "->" << edge.dst();
00191   return os;
00192 }
00193 
00194 #endif /* INCLUDED_GR_FLOWGRAPH_H */