GNU Radio 3.6.5 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 class GR_CORE_API gr_msg_endpoint
00056 {
00057 private:
00058   gr_basic_block_sptr d_basic_block;
00059   pmt::pmt_t d_port;
00060   bool d_is_hier;
00061 public:
00062   gr_msg_endpoint() : d_basic_block(), d_port(pmt::PMT_NIL) { }
00063   gr_msg_endpoint(gr_basic_block_sptr block, pmt::pmt_t port, bool is_hier=false){ d_basic_block = block; d_port = port; d_is_hier = is_hier;}
00064   gr_basic_block_sptr block() const { return d_basic_block; }
00065   pmt::pmt_t port() const { return d_port; }
00066   bool is_hier() const { return d_is_hier; }
00067   void set_hier(bool h) { d_is_hier = h; }
00068 
00069   bool operator==(const gr_msg_endpoint &other) const;
00070 
00071 };
00072 
00073 inline bool gr_msg_endpoint::operator==(const gr_msg_endpoint &other) const
00074 {
00075   return (d_basic_block == other.d_basic_block &&
00076           pmt::pmt_equal(d_port, other.d_port));
00077 }
00078 
00079 
00080 // Hold vectors of gr_endpoint objects
00081 typedef std::vector<gr_endpoint> gr_endpoint_vector_t;
00082 typedef std::vector<gr_endpoint>::iterator gr_endpoint_viter_t;
00083 
00084 /*!
00085  *\brief Class representing a connection between to graph endpoints
00086  *
00087  */
00088 class GR_CORE_API gr_edge
00089 {
00090 public:
00091   gr_edge() : d_src(), d_dst() { };
00092   gr_edge(const gr_endpoint &src, const gr_endpoint &dst) : d_src(src), d_dst(dst) { }
00093   ~gr_edge();
00094 
00095   const gr_endpoint &src() const { return d_src; }
00096   const gr_endpoint &dst() const { return d_dst; }
00097 
00098 private:
00099   gr_endpoint d_src;
00100   gr_endpoint d_dst;
00101 };
00102 
00103 
00104 // Hold vectors of gr_edge objects
00105 typedef std::vector<gr_edge> gr_edge_vector_t;
00106 typedef std::vector<gr_edge>::iterator gr_edge_viter_t;
00107 
00108 
00109 /*!
00110  *\brief Class representing a msg connection between to graph msg endpoints
00111  *
00112  */
00113 class GR_CORE_API gr_msg_edge
00114 {
00115 public:
00116   gr_msg_edge() : d_src(), d_dst() { };
00117   gr_msg_edge(const gr_msg_endpoint &src, const gr_msg_endpoint &dst) : d_src(src), d_dst(dst) { }
00118   ~gr_msg_edge() {}
00119 
00120   const gr_msg_endpoint &src() const { return d_src; }
00121   const gr_msg_endpoint &dst() const { return d_dst; }
00122 
00123 private:
00124   gr_msg_endpoint d_src;
00125   gr_msg_endpoint d_dst;
00126 };
00127 
00128 // Hold vectors of gr_edge objects
00129 typedef std::vector<gr_msg_edge> gr_msg_edge_vector_t;
00130 typedef std::vector<gr_msg_edge>::iterator gr_msg_edge_viter_t;
00131 
00132 // Create a shared pointer to a heap allocated flowgraph
00133 // (types defined in gr_runtime_types.h)
00134 GR_CORE_API gr_flowgraph_sptr gr_make_flowgraph();
00135 
00136 /*!
00137  * \brief Class representing a directed, acyclic graph of basic blocks
00138  * \ingroup internal
00139  */
00140 class GR_CORE_API gr_flowgraph
00141 {
00142 public:
00143   friend GR_CORE_API gr_flowgraph_sptr gr_make_flowgraph();
00144 
00145   // Destruct an arbitrary flowgraph
00146   ~gr_flowgraph();
00147 
00148   // Connect two endpoints
00149   void connect(const gr_endpoint &src, const gr_endpoint &dst);
00150 
00151   // Disconnect two endpoints
00152   void disconnect(const gr_endpoint &src, const gr_endpoint &dst);
00153 
00154   // Connect an output port to an input port (convenience)
00155   void connect(gr_basic_block_sptr src_block, int src_port,
00156                gr_basic_block_sptr dst_block, int dst_port);
00157 
00158   // Disconnect an input port from an output port (convenience)
00159   void disconnect(gr_basic_block_sptr src_block, int src_port,
00160                   gr_basic_block_sptr dst_block, int dst_port);
00161 
00162   // Connect two msg endpoints
00163   void connect(const gr_msg_endpoint &src, const gr_msg_endpoint &dst);
00164   
00165   // Disconnect two msg endpoints
00166   void disconnect(const gr_msg_endpoint &src, const gr_msg_endpoint &dst);
00167 
00168   // Validate connectivity, raise exception if invalid
00169   void validate();
00170 
00171   // Clear existing flowgraph
00172   void clear();
00173 
00174   // Return vector of edges
00175   const gr_edge_vector_t &edges() const { return d_edges; }
00176   
00177   // Return vector of msg edges
00178   const gr_msg_edge_vector_t &msg_edges() const { return d_msg_edges; }
00179 
00180   // Return vector of connected blocks
00181   gr_basic_block_vector_t calc_used_blocks();
00182 
00183   // Return toplogically sorted vector of blocks.  All the sources come first.
00184   gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks);
00185 
00186   // Return vector of vectors of disjointly connected blocks, topologically
00187   // sorted.
00188   std::vector<gr_basic_block_vector_t> partition();
00189 
00190 protected:
00191   gr_basic_block_vector_t d_blocks;
00192   gr_edge_vector_t d_edges;
00193   gr_msg_edge_vector_t d_msg_edges;
00194 
00195   gr_flowgraph();
00196   std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs);
00197   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port);
00198   gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block);
00199   bool has_block_p(gr_basic_block_sptr block);
00200   gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port);
00201 
00202 private:
00203 
00204   void check_valid_port(gr_io_signature_sptr sig, int port);
00205   void check_valid_port(const gr_msg_endpoint &e);
00206   void check_dst_not_used(const gr_endpoint &dst);
00207   void check_type_match(const gr_endpoint &src, const gr_endpoint &dst);
00208   gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs
00209   void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs);
00210 
00211   gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block);
00212   gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00213   void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00214   gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks);
00215   gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks);
00216   bool source_p(gr_basic_block_sptr block);
00217   void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output);
00218 };
00219 
00220 // Convenience functions
00221 inline
00222 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port,
00223                       gr_basic_block_sptr dst_block, int dst_port)
00224 {
00225   connect(gr_endpoint(src_block, src_port),
00226           gr_endpoint(dst_block, dst_port));
00227 }
00228 
00229 inline
00230 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port,
00231                          gr_basic_block_sptr dst_block, int dst_port)
00232 {
00233   disconnect(gr_endpoint(src_block, src_port),
00234              gr_endpoint(dst_block, dst_port));
00235 }
00236 
00237 inline std::ostream&
00238 operator <<(std::ostream &os, const gr_endpoint endp)
00239 {
00240   os << endp.block() << ":" << endp.port();
00241   return os;
00242 }
00243 
00244 inline std::ostream&
00245 operator <<(std::ostream &os, const gr_edge edge)
00246 {
00247   os << edge.src() << "->" << edge.dst();
00248   return os;
00249 }
00250 
00251 #endif /* INCLUDED_GR_FLOWGRAPH_H */