GNU Radio 3.5.1 C++ API
|
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 // 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 toplogically sorted vector of blocks. All the sources come first. 00126 gr_basic_block_vector_t topological_sort(gr_basic_block_vector_t &blocks); 00127 00128 // Return vector of vectors of disjointly connected blocks, topologically 00129 // sorted. 00130 std::vector<gr_basic_block_vector_t> partition(); 00131 00132 protected: 00133 gr_basic_block_vector_t d_blocks; 00134 gr_edge_vector_t d_edges; 00135 00136 gr_flowgraph(); 00137 std::vector<int> calc_used_ports(gr_basic_block_sptr block, bool check_inputs); 00138 gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block, int port); 00139 gr_edge_vector_t calc_upstream_edges(gr_basic_block_sptr block); 00140 bool has_block_p(gr_basic_block_sptr block); 00141 gr_edge calc_upstream_edge(gr_basic_block_sptr block, int port); 00142 00143 private: 00144 00145 void check_valid_port(gr_io_signature_sptr sig, int port); 00146 void check_dst_not_used(const gr_endpoint &dst); 00147 void check_type_match(const gr_endpoint &src, const gr_endpoint &dst); 00148 gr_edge_vector_t calc_connections(gr_basic_block_sptr block, bool check_inputs); // false=use outputs 00149 void check_contiguity(gr_basic_block_sptr block, const std::vector<int> &used_ports, bool check_inputs); 00150 00151 gr_basic_block_vector_t calc_downstream_blocks(gr_basic_block_sptr block); 00152 gr_basic_block_vector_t calc_reachable_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks); 00153 void reachable_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks); 00154 gr_basic_block_vector_t calc_adjacent_blocks(gr_basic_block_sptr block, gr_basic_block_vector_t &blocks); 00155 gr_basic_block_vector_t sort_sources_first(gr_basic_block_vector_t &blocks); 00156 bool source_p(gr_basic_block_sptr block); 00157 void topological_dfs_visit(gr_basic_block_sptr block, gr_basic_block_vector_t &output); 00158 }; 00159 00160 // Convenience functions 00161 inline 00162 void gr_flowgraph::connect(gr_basic_block_sptr src_block, int src_port, 00163 gr_basic_block_sptr dst_block, int dst_port) 00164 { 00165 connect(gr_endpoint(src_block, src_port), 00166 gr_endpoint(dst_block, dst_port)); 00167 } 00168 00169 inline 00170 void gr_flowgraph::disconnect(gr_basic_block_sptr src_block, int src_port, 00171 gr_basic_block_sptr dst_block, int dst_port) 00172 { 00173 disconnect(gr_endpoint(src_block, src_port), 00174 gr_endpoint(dst_block, dst_port)); 00175 } 00176 00177 inline std::ostream& 00178 operator <<(std::ostream &os, const gr_endpoint endp) 00179 { 00180 os << endp.block() << ":" << endp.port(); 00181 return os; 00182 } 00183 00184 inline std::ostream& 00185 operator <<(std::ostream &os, const gr_edge edge) 00186 { 00187 os << edge.src() << "->" << edge.dst(); 00188 return os; 00189 } 00190 00191 #endif /* INCLUDED_GR_FLOWGRAPH_H */