GNU Radio 3.7.1 C++ API
hier_block2.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006-2009,2013 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_RUNTIME_HIER_BLOCK2_H
00024 #define INCLUDED_GR_RUNTIME_HIER_BLOCK2_H
00025 
00026 #include <gnuradio/api.h>
00027 #include <gnuradio/basic_block.h>
00028 
00029 namespace gr {
00030 
00031   /*!
00032    * \brief public constructor for hier_block2
00033    */
00034   GR_RUNTIME_API hier_block2_sptr
00035   make_hier_block2(const std::string &name,
00036                    gr::io_signature::sptr input_signature,
00037                    gr::io_signature::sptr output_signature);
00038 
00039   class hier_block2_detail;
00040 
00041   /*!
00042    * \brief Hierarchical container class for gr::block's and gr::hier_block2's
00043    * \ingroup container_blk
00044    * \ingroup base_blk
00045    */
00046   class GR_RUNTIME_API hier_block2 : public basic_block
00047   {
00048   private:
00049     friend class hier_block2_detail;
00050     friend GR_RUNTIME_API hier_block2_sptr
00051       make_hier_block2(const std::string &name,
00052                        gr::io_signature::sptr input_signature,
00053                        gr::io_signature::sptr output_signature);
00054 
00055     /*!
00056      * \brief Private implementation details of gr::hier_block2
00057      */
00058     hier_block2_detail *d_detail;
00059 
00060   protected:
00061     hier_block2(void) {} // allows pure virtual interface sub-classes
00062     hier_block2(const std::string &name,
00063                 gr::io_signature::sptr input_signature,
00064                 gr::io_signature::sptr output_signature);
00065 
00066   public:
00067     virtual ~hier_block2();
00068 
00069     /*!
00070      * \brief typedef for object returned from self().
00071      *
00072      * This type is only guaranteed to be passable to connect and
00073      * disconnect. No other assumptions should be made about it.
00074      */
00075     typedef basic_block_sptr opaque_self;
00076 
00077     /*!
00078      * \brief Return an object, representing the current block, which
00079      * can be passed to connect.
00080      *
00081      * The returned object may only be used as an argument to connect
00082      * or disconnect.  Any other use of self() results in unspecified
00083      * (erroneous) behavior.
00084      */
00085     opaque_self self();
00086 
00087     /*!
00088      * \brief Add a stand-alone (possibly hierarchical) block to
00089      * internal graph
00090      *
00091      * This adds a gr-block or hierarchical block to the internal
00092      * graph without wiring it to anything else.
00093      */
00094     void connect(basic_block_sptr block);
00095 
00096     /*!
00097      * \brief Add gr-blocks or hierarchical blocks to internal graph
00098      * and wire together
00099      *
00100      * This adds (if not done earlier by another connect) a pair of
00101      * gr-blocks or hierarchical blocks to the internal flowgraph, and
00102      * wires the specified output port to the specified input port.
00103      */
00104     void connect(basic_block_sptr src, int src_port,
00105                  basic_block_sptr dst, int dst_port);
00106 
00107     /*!
00108      * \brief Add gr-blocks or hierarchical blocks to internal graph
00109      * and wire together
00110      *
00111      * This adds (if not done earlier by another connect) a pair of
00112      * gr-blocks or hierarchical blocks to the internal message port
00113      * subscription
00114      */
00115     void msg_connect(basic_block_sptr src, pmt::pmt_t srcport,
00116                      basic_block_sptr dst, pmt::pmt_t dstport);
00117     void msg_connect(basic_block_sptr src, std::string srcport,
00118                      basic_block_sptr dst, std::string dstport);
00119     void msg_disconnect(basic_block_sptr src, pmt::pmt_t srcport,
00120                         basic_block_sptr dst, pmt::pmt_t dstport);
00121     void msg_disconnect(basic_block_sptr src, std::string srcport,
00122                         basic_block_sptr dst, std::string dstport);
00123 
00124     /*!
00125      * \brief Remove a gr-block or hierarchical block from the
00126      * internal flowgraph.
00127      *
00128      * This removes a gr-block or hierarchical block from the internal
00129      * flowgraph, disconnecting it from other blocks as needed.
00130      */
00131     void disconnect(basic_block_sptr block);
00132 
00133     /*!
00134      * \brief Disconnect a pair of gr-blocks or hierarchical blocks in
00135      *        internal flowgraph.
00136      *
00137      * This disconnects the specified input port from the specified
00138      * output port of a pair of gr-blocks or hierarchical blocks.
00139      */
00140     void disconnect(basic_block_sptr src, int src_port,
00141                     basic_block_sptr dst, int dst_port);
00142 
00143     /*!
00144      * \brief Disconnect all connections in the internal flowgraph.
00145      *
00146      * This call removes all output port to input port connections in
00147      * the internal flowgraph.
00148      */
00149     void disconnect_all();
00150 
00151     /*!
00152      * Lock a flowgraph in preparation for reconfiguration.  When an
00153      * equal number of calls to lock() and unlock() have occurred, the
00154      * flowgraph will be reconfigured.
00155      *
00156      * N.B. lock() and unlock() may not be called from a flowgraph
00157      * thread (E.g., gr::block::work method) or deadlock will occur
00158      * when reconfiguration happens.
00159      */
00160     virtual void lock();
00161 
00162     /*!
00163      * Unlock a flowgraph in preparation for reconfiguration.  When an
00164      * equal number of calls to lock() and unlock() have occurred, the
00165      * flowgraph will be reconfigured.
00166      *
00167      * N.B. lock() and unlock() may not be called from a flowgraph
00168      * thread (E.g., gr::block::work method) or deadlock will occur
00169      * when reconfiguration happens.
00170      */
00171     virtual void unlock();
00172 
00173     // This is a public method for ease of code organization, but should be
00174     // ignored by the user.
00175     flat_flowgraph_sptr flatten() const;
00176 
00177     hier_block2_sptr to_hier_block2(); // Needed for Python type coercion
00178 
00179     bool has_msg_port(pmt::pmt_t which_port) {
00180       return message_port_is_hier(which_port) || basic_block::has_msg_port(which_port);
00181     }
00182   
00183     bool message_port_is_hier(pmt::pmt_t port_id) {
00184       return message_port_is_hier_in(port_id) || message_port_is_hier_out(port_id);
00185     }
00186 
00187     bool message_port_is_hier_in(pmt::pmt_t port_id) {
00188       return pmt::list_has(hier_message_ports_in, port_id);
00189     }
00190 
00191     bool message_port_is_hier_out(pmt::pmt_t port_id) {
00192       return pmt::list_has(hier_message_ports_out, port_id);
00193     }
00194 
00195     pmt::pmt_t hier_message_ports_in;
00196     pmt::pmt_t hier_message_ports_out;
00197 
00198     void message_port_register_hier_in(pmt::pmt_t port_id) {
00199       if(pmt::list_has(hier_message_ports_in, port_id))
00200         throw std::invalid_argument("hier msg in port by this name already registered");
00201       if(msg_queue.find(port_id) != msg_queue.end())
00202         throw std::invalid_argument("block already has a primitive input port by this name");
00203       hier_message_ports_in = pmt::list_add(hier_message_ports_in, port_id);
00204     }
00205 
00206     void message_port_register_hier_out(pmt::pmt_t port_id) {
00207       if(pmt::list_has(hier_message_ports_out, port_id))
00208         throw std::invalid_argument("hier msg out port by this name already registered");
00209       if(pmt::dict_has_key(message_subscribers, port_id))
00210         throw std::invalid_argument("block already has a primitive output port by this name");
00211       hier_message_ports_out = pmt::list_add(hier_message_ports_out, port_id);
00212     }
00213 
00214     /*!
00215      * \brief Set the affinity of all blocks in hier_block2 to processor core \p n.
00216      *
00217      * \param mask a vector of ints of the core numbers available to this block.
00218      */
00219     void set_processor_affinity(const std::vector<int> &mask);
00220 
00221     /*!
00222      * \brief Remove processor affinity for all blocks in hier_block2.
00223      */
00224     void unset_processor_affinity();
00225 
00226     /*!
00227      * \brief Get the current processor affinity.
00228      *
00229      * \details This returns the processor affinity value for the first
00230      * block in the hier_block2's list of blocks with the assumption
00231      * that they have always only been set through the hier_block2's
00232      * interface. If any block has been individually set, then this
00233      * call could be misleading.
00234      */
00235     std::vector<int> processor_affinity();
00236   };
00237 
00238   inline hier_block2_sptr cast_to_hier_block2_sptr(basic_block_sptr block) {
00239     return boost::dynamic_pointer_cast<hier_block2, basic_block>(block);
00240   }
00241 
00242 } /* namespace gr */
00243 
00244 #endif /* INCLUDED_GR_RUNTIME_HIER_BLOCK2_H */