GNU Radio 3.6.5 C++ API

gr_hier_block2.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006,2007,2008,2009 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 #ifndef INCLUDED_GR_HIER_BLOCK2_H
00023 #define INCLUDED_GR_HIER_BLOCK2_H
00024 
00025 #include <gr_core_api.h>
00026 #include <gr_basic_block.h>
00027 
00028 /*!
00029  * \brief public constructor for gr_hier_block2
00030 
00031  */
00032 GR_CORE_API gr_hier_block2_sptr gr_make_hier_block2(const std::string &name,
00033                                         gr_io_signature_sptr input_signature,
00034                                         gr_io_signature_sptr output_signature);
00035 
00036 class gr_hier_block2_detail;
00037 
00038 /*!
00039  * \brief Hierarchical container class for gr_block's and gr_hier_block2's
00040  * \ingroup container_blk
00041  * \ingroup base_blk
00042  *
00043  */
00044 class GR_CORE_API gr_hier_block2 : public gr_basic_block
00045 {
00046 private:
00047   friend class gr_hier_block2_detail;
00048   friend GR_CORE_API gr_hier_block2_sptr gr_make_hier_block2(const std::string &name,
00049                                                  gr_io_signature_sptr input_signature,
00050                                                  gr_io_signature_sptr output_signature);
00051 
00052   /*!
00053    * \brief Private implementation details of gr_hier_block2
00054    */
00055   gr_hier_block2_detail *d_detail;
00056 
00057 protected:
00058   gr_hier_block2 (void){} //allows pure virtual interface sub-classes
00059   gr_hier_block2(const std::string &name,
00060                  gr_io_signature_sptr input_signature,
00061                  gr_io_signature_sptr output_signature);
00062 
00063 public:
00064   virtual ~gr_hier_block2();
00065 
00066   /*!
00067    * \brief typedef for object returned from self().
00068    *
00069    * This type is only guaranteed to be passable to connect and disconnect.
00070    * No other assumptions should be made about it.
00071    */
00072   typedef gr_basic_block_sptr   opaque_self;
00073 
00074   /*!
00075    * \brief Return an object, representing the current block, which can be passed to connect.
00076    *
00077    * The returned object may only be used as an argument to connect or disconnect.
00078    * Any other use of self() results in unspecified (erroneous) behavior.
00079    */
00080   opaque_self self();
00081 
00082   /*!
00083    * \brief Add a stand-alone (possibly hierarchical) block to internal graph
00084    *
00085    * This adds a gr-block or hierarchical block to the internal graph
00086    * without wiring it to anything else.
00087    */
00088   void connect(gr_basic_block_sptr block);
00089 
00090   /*!
00091    * \brief Add gr-blocks or hierarchical blocks to internal graph and wire together
00092    *
00093    * This adds (if not done earlier by another connect) a pair of gr-blocks or
00094    * hierarchical blocks to the internal flowgraph, and wires the specified output
00095    * port to the specified input port.
00096    */
00097   void connect(gr_basic_block_sptr src, int src_port,
00098                gr_basic_block_sptr dst, int dst_port);
00099 
00100   /*!
00101    * \brief Add gr-blocks or hierarchical blocks to internal graph and wire together
00102    *
00103    * This adds (if not done earlier by another connect) a pair of gr-blocks or
00104    * hierarchical blocks to the internal message port subscription
00105    */
00106   void msg_connect(gr_basic_block_sptr src, pmt::pmt_t srcport,
00107                gr_basic_block_sptr dst, pmt::pmt_t dstport);
00108   void msg_connect(gr_basic_block_sptr src, std::string srcport,
00109                gr_basic_block_sptr dst, std::string dstport);
00110   void msg_disconnect(gr_basic_block_sptr src, pmt::pmt_t srcport,
00111                gr_basic_block_sptr dst, pmt::pmt_t dstport);
00112   void msg_disconnect(gr_basic_block_sptr src, std::string srcport,
00113                gr_basic_block_sptr dst, std::string dstport);
00114 
00115   /*!
00116    * \brief Remove a gr-block or hierarchical block from the internal flowgraph.
00117    *
00118    * This removes a gr-block or hierarchical block from the internal flowgraph,
00119    * disconnecting it from other blocks as needed.
00120    *
00121    */
00122   void disconnect(gr_basic_block_sptr block);
00123 
00124   /*!
00125    * \brief Disconnect a pair of gr-blocks or hierarchical blocks in internal
00126    *        flowgraph.
00127    *
00128    * This disconnects the specified input port from the specified output port
00129    * of a pair of gr-blocks or hierarchical blocks.
00130    */
00131   void disconnect(gr_basic_block_sptr src, int src_port,
00132                   gr_basic_block_sptr dst, int dst_port);
00133 
00134   /*!
00135    * \brief Disconnect all connections in the internal flowgraph.
00136    *
00137    * This call removes all output port to input port connections in the internal
00138    * flowgraph.
00139    */
00140   void disconnect_all();
00141 
00142   /*!
00143    * Lock a flowgraph in preparation for reconfiguration.  When an equal
00144    * number of calls to lock() and unlock() have occurred, the flowgraph
00145    * will be reconfigured.
00146    *
00147    * N.B. lock() and unlock() may not be called from a flowgraph thread
00148    * (E.g., gr_block::work method) or deadlock will occur when
00149    * reconfiguration happens.
00150    */
00151   virtual void lock();
00152 
00153   /*!
00154    * Unlock a flowgraph in preparation for reconfiguration.  When an equal
00155    * number of calls to lock() and unlock() have occurred, the flowgraph
00156    * will be reconfigured.
00157    *
00158    * N.B. lock() and unlock() may not be called from a flowgraph thread
00159    * (E.g., gr_block::work method) or deadlock will occur when
00160    * reconfiguration happens.
00161    */
00162   virtual void unlock();
00163 
00164   // This is a public method for ease of code organization, but should be
00165   // ignored by the user.
00166   gr_flat_flowgraph_sptr flatten() const;
00167 
00168   gr_hier_block2_sptr to_hier_block2(); // Needed for Python type coercion
00169 
00170   bool has_msg_port(pmt::pmt_t which_port){
00171     return message_port_is_hier(which_port) || gr_basic_block::has_msg_port(which_port);
00172     }   
00173   
00174   bool message_port_is_hier(pmt::pmt_t port_id){
00175     return message_port_is_hier_in(port_id) || message_port_is_hier_out(port_id);
00176     }
00177   bool message_port_is_hier_in(pmt::pmt_t port_id){
00178     return pmt::pmt_list_has(hier_message_ports_in, port_id);
00179     }
00180   bool message_port_is_hier_out(pmt::pmt_t port_id){
00181     return pmt::pmt_list_has(hier_message_ports_out, port_id);
00182     }
00183 
00184   pmt::pmt_t hier_message_ports_in;
00185   pmt::pmt_t hier_message_ports_out;
00186 
00187   void message_port_register_hier_in(pmt::pmt_t port_id){
00188     if(pmt::pmt_list_has(hier_message_ports_in, port_id))
00189         throw std::invalid_argument("hier msg in port by this name already registered");
00190     if(msg_queue.find(port_id) != msg_queue.end())
00191         throw std::invalid_argument("block already has a primitive input port by this name");
00192     hier_message_ports_in = pmt::pmt_list_add(hier_message_ports_in, port_id);
00193     }
00194   void message_port_register_hier_out(pmt::pmt_t port_id){
00195     if(pmt::pmt_list_has(hier_message_ports_out, port_id))
00196         throw std::invalid_argument("hier msg out port by this name already registered");
00197     if(pmt::pmt_dict_has_key(message_subscribers, port_id))
00198         throw std::invalid_argument("block already has a primitive output port by this name");
00199     hier_message_ports_out = pmt::pmt_list_add(hier_message_ports_out, port_id);
00200     }
00201 
00202 };
00203 
00204 inline gr_hier_block2_sptr cast_to_hier_block2_sptr(gr_basic_block_sptr block) {
00205   return boost::dynamic_pointer_cast<gr_hier_block2, gr_basic_block>(block);
00206 }
00207 
00208 #endif /* INCLUDED_GR_HIER_BLOCK2_H */