GNU Radio 3.6.0 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 struct GR_CORE_API gr_msg_connection{
00050     gr_basic_block_sptr pro; //provider
00051     gr_basic_block_sptr sub; //subscriber
00052     std::string name;
00053 };
00054 
00055 inline bool gr_endpoint::operator==(const gr_endpoint &other) const
00056 {
00057   return (d_basic_block == other.d_basic_block &&
00058           d_port == other.d_port);
00059 }
00060 
00061 // Hold vectors of gr_endpoint objects
00062 typedef std::vector<gr_endpoint> gr_endpoint_vector_t;
00063 typedef std::vector<gr_endpoint>::iterator gr_endpoint_viter_t;
00064 
00065 /*!
00066  *\brief Class representing a connection between to graph endpoints
00067  *
00068  */
00069 class GR_CORE_API gr_edge
00070 {
00071 public:
00072   gr_edge() : d_src(), d_dst() { };
00073   gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
00074   ~gr_edge();
00075 
00076   const gr_endpoint &src() const { return d_src; }
00077   const gr_endpoint &dst() const { return d_dst; }
00078 
00079 private:
00080   gr_endpoint d_src;
00081   gr_endpoint d_dst;
00082 };
00083 
00084 // Hold vectors of gr_edge objects
00085 typedef std::vector<gr_edge> gr_edge_vector_t;
00086 typedef std::vector<gr_edge>::iterator gr_edge_viter_t;
00087 
00088 
00089 // Create a shared pointer to a heap allocated flowgraph
00090 // (types defined in gr_runtime_types.h)
00091 GR_CORE_API gr_flowgraph_sptr gr_make_flowgraph();
00092 
00093 /*!
00094  * \brief Class representing a directed, acyclic graph of basic blocks
00095  * \ingroup internal
00096  */
00097 class GR_CORE_API gr_flowgraph
00098 {
00099 public:
00100   friend GR_CORE_API gr_flowgraph_sptr gr_make_flowgraph();
00101 
00102   // Destruct an arbitrary flowgraph
00103   ~gr_flowgraph();
00104 
00105   // Connect two endpoints
00106   void connect(const gr_endpoint &src, const gr_endpoint &dst);
00107 
00108   // Disconnect two endpoints
00109   void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
00110 
00111   // Connect an output port to an input port (convenience)
00112   void connect(gr_basic_block_sptr src_block, int src_port,
00113                gr_basic_block_sptr dst_block, int dst_port);
00114 
00115   // Disconnect an input port from an output port (convenience)
00116   void disconnect(gr_basic_block_sptr src_block, int src_port,
00117                   gr_basic_block_sptr dst_block, int dst_port);
00118 
00119   // Validate connectivity, raise exception if invalid
00120   void validate();
00121 
00122   // Clear existing flowgraph
00123   void clear();
00124 
00125   // Return vector of edges
00126   const gr_edge_vector_t &edges() const { return d_edges; }
00127 
00128   // Return vector of connected blocks
00129   gr_basic_block_vector_t calc_used_blocks();
00130 
00131   // Return toplogically sorted vector of blocks.  All the sources come first.
00132   gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
00133 
00134   // Return vector of vectors of disjointly connected blocks, topologically
00135   // sorted.
00136   std::vector<gr_basic_block_vector_t> partition();
00137 
00138 protected:
00139   gr_basic_block_vector_t d_blocks;
00140   gr_basic_block_vector_t d_blocks_from_msg_connect;
00141   gr_edge_vector_t d_edges;
00142 
00143   gr_flowgraph();
00144   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs);
00145   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
00146   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
00147   bool has_block_p(gr_basic_block_sptr block);
00148   gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
00149 
00150 private:
00151 
00152   void check_valid_port(gr_io_signature_sptr sig, int port);
00153   void check_dst_not_used(const gr_endpoint &dst);
00154   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
00155   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
00156   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
00157 
00158   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
00159   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00160   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00161   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00162   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
00163   bool source_p(gr_basic_block_sptr block);
00164   void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
00165 };
00166 
00167 // Convenience functions
00168 inline
00169 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
00170                       gr_basic_block_sptr dst_block, int dst_port)
00171 {
00172   connect(gr_endpoint(src_block, src_port),
00173           gr_endpoint(dst_block, dst_port));
00174 }
00175 
00176 inline
00177 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
00178                          gr_basic_block_sptr dst_block, int dst_port)
00179 {
00180   disconnect(gr_endpoint(src_block, src_port),
00181              gr_endpoint(dst_block, dst_port));
00182 }
00183 
00184 inline std::ostream&
00185 operator <<(std::ostream &os, const gr_endpoint endp)
00186 {
00187   os << endp.block() << ":" << endp.port();
00188   return os;
00189 }
00190 
00191 inline std::ostream&
00192 operator <<(std::ostream &os, const gr_edge edge)
00193 {
00194   os << edge.src() << "->" << edge.dst();
00195   return os;
00196 }
00197 
00198 #endif /* INCLUDED_GR_FLOWGRAPH_H */