GNU Radio 3.3.0 C++ API
gr_basic_block.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006,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 
00023 #ifndef INCLUDED_GR_BASIC_BLOCK_H
00024 #define INCLUDED_GR_BASIC_BLOCK_H
00025 
00026 #include <gr_runtime_types.h>
00027 #include <gr_sptr_magic.h>
00028 #include <boost/enable_shared_from_this.hpp>
00029 #include <gr_msg_accepter.h>
00030 #include <string>
00031 
00032 /*!
00033  * \brief The abstract base class for all signal processing blocks.
00034  * \ingroup internal
00035  *
00036  * Basic blocks are the bare abstraction of an entity that has a name,
00037  * a set of inputs and outputs, and a message queue.  These are never instantiated
00038  * directly; rather, this is the abstract parent class of both gr_hier_block,
00039  * which is a recursive container, and gr_block, which implements actual
00040  * signal processing functions.
00041  */
00042 
00043 class gr_basic_block : public gr_msg_accepter, public boost::enable_shared_from_this<gr_basic_block>
00044 {
00045 protected:
00046     friend class gr_flowgraph;
00047     friend class gr_flat_flowgraph; // TODO: will be redundant
00048 
00049     enum vcolor { WHITE, GREY, BLACK };
00050 
00051     std::string          d_name;
00052     gr_io_signature_sptr d_input_signature;
00053     gr_io_signature_sptr d_output_signature;
00054     long                 d_unique_id;
00055     vcolor               d_color;
00056 
00057     //! Protected constructor prevents instantiation by non-derived classes
00058     gr_basic_block(const std::string &name,
00059                    gr_io_signature_sptr input_signature,
00060                    gr_io_signature_sptr output_signature);
00061 
00062     //! may only be called during constructor
00063     void set_input_signature(gr_io_signature_sptr iosig) {
00064         d_input_signature = iosig;
00065     }
00066     
00067     //! may only be called during constructor
00068     void set_output_signature(gr_io_signature_sptr iosig) {
00069         d_output_signature = iosig;
00070     }
00071 
00072     /*!
00073      * \brief Allow the flowgraph to set for sorting and partitioning
00074      */
00075     void set_color(vcolor color) { d_color = color; }
00076     vcolor color() const { return d_color; }
00077 
00078 public:
00079     virtual ~gr_basic_block();
00080     long unique_id() const { return d_unique_id; }
00081     std::string name() const { return d_name; }
00082     gr_io_signature_sptr input_signature() const  { return d_input_signature; }
00083     gr_io_signature_sptr output_signature() const { return d_output_signature; }
00084     gr_basic_block_sptr basic_block(); // Needed for Python type coercion
00085 
00086     /*!
00087      * \brief Confirm that ninputs and noutputs is an acceptable combination.
00088      *
00089      * \param ninputs   number of input streams connected
00090      * \param noutputs  number of output streams connected
00091      *
00092      * \returns true if this is a valid configuration for this block.
00093      *
00094      * This function is called by the runtime system whenever the
00095      * topology changes.  Most classes do not need to override this.
00096      * This check is in addition to the constraints specified by the input
00097      * and output gr_io_signatures.
00098      */
00099     virtual bool check_topology(int ninputs, int noutputs) { return true; }
00100 
00101     /*!
00102      * \brief Block message handler.
00103      * 
00104      * \param msg  Arbitrary message encapsulated as pmt::pmt_t
00105      *
00106      * This function is called by the runtime system whenever there are
00107      * messages in its queue.  Blocks should override this to receive
00108      * messages; the default behavior is to drop them on the floor.
00109      */
00110     virtual void handle_msg(pmt::pmt_t msg) { };
00111 };
00112 
00113 inline bool operator<(gr_basic_block_sptr lhs, gr_basic_block_sptr rhs)
00114 {
00115   return lhs->unique_id() < rhs->unique_id();
00116 }
00117 
00118 typedef std::vector<gr_basic_block_sptr> gr_basic_block_vector_t;
00119 typedef std::vector<gr_basic_block_sptr>::iterator gr_basic_block_viter_t;
00120 
00121 long gr_basic_block_ncurrently_allocated();
00122 
00123 inline std::ostream &operator << (std::ostream &os, gr_basic_block_sptr basic_block)
00124 {
00125     os << basic_block->name() << "(" << basic_block->unique_id() << ")";
00126     return os;
00127 }
00128 
00129 #endif /* INCLUDED_GR_BASIC_BLOCK_H */