switch source package format to 3.0 quilt
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_basic_block.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef INCLUDED_GR_BASIC_BLOCK_H
24 #define INCLUDED_GR_BASIC_BLOCK_H
25
26 #include <gr_runtime_types.h>
27 #include <gr_sptr_magic.h>
28 #include <boost/enable_shared_from_this.hpp>
29 #include <gr_msg_accepter.h>
30 #include <string>
31
32 /*!
33  * \brief The abstract base class for all signal processing blocks.
34  * \ingroup internal
35  *
36  * Basic blocks are the bare abstraction of an entity that has a name,
37  * a set of inputs and outputs, and a message queue.  These are never instantiated
38  * directly; rather, this is the abstract parent class of both gr_hier_block,
39  * which is a recursive container, and gr_block, which implements actual
40  * signal processing functions.
41  */
42
43 class gr_basic_block : public gr_msg_accepter, public boost::enable_shared_from_this<gr_basic_block>
44 {
45 protected:
46     friend class gr_flowgraph;
47     friend class gr_flat_flowgraph; // TODO: will be redundant
48
49     enum vcolor { WHITE, GREY, BLACK };
50
51     std::string          d_name;
52     gr_io_signature_sptr d_input_signature;
53     gr_io_signature_sptr d_output_signature;
54     long                 d_unique_id;
55     vcolor               d_color;
56
57     //! Protected constructor prevents instantiation by non-derived classes
58     gr_basic_block(const std::string &name,
59                    gr_io_signature_sptr input_signature,
60                    gr_io_signature_sptr output_signature);
61
62     //! may only be called during constructor
63     void set_input_signature(gr_io_signature_sptr iosig) {
64         d_input_signature = iosig;
65     }
66     
67     //! may only be called during constructor
68     void set_output_signature(gr_io_signature_sptr iosig) {
69         d_output_signature = iosig;
70     }
71
72     /*!
73      * \brief Allow the flowgraph to set for sorting and partitioning
74      */
75     void set_color(vcolor color) { d_color = color; }
76     vcolor color() const { return d_color; }
77
78 public:
79     virtual ~gr_basic_block();
80     long unique_id() const { return d_unique_id; }
81     std::string name() const { return d_name; }
82     gr_io_signature_sptr input_signature() const  { return d_input_signature; }
83     gr_io_signature_sptr output_signature() const { return d_output_signature; }
84     gr_basic_block_sptr basic_block(); // Needed for Python type coercion
85
86     /*!
87      * \brief Confirm that ninputs and noutputs is an acceptable combination.
88      *
89      * \param ninputs   number of input streams connected
90      * \param noutputs  number of output streams connected
91      *
92      * \returns true if this is a valid configuration for this block.
93      *
94      * This function is called by the runtime system whenever the
95      * topology changes.  Most classes do not need to override this.
96      * This check is in addition to the constraints specified by the input
97      * and output gr_io_signatures.
98      */
99     virtual bool check_topology(int ninputs, int noutputs) { return true; }
100
101     /*!
102      * \brief Block message handler.
103      * 
104      * \param msg  Arbitrary message encapsulated as pmt::pmt_t
105      *
106      * This function is called by the runtime system whenever there are
107      * messages in its queue.  Blocks should override this to receive
108      * messages; the default behavior is to drop them on the floor.
109      */
110     virtual void handle_msg(pmt::pmt_t msg) { };
111 };
112
113 inline bool operator<(gr_basic_block_sptr lhs, gr_basic_block_sptr rhs)
114 {
115   return lhs->unique_id() < rhs->unique_id();
116 }
117
118 typedef std::vector<gr_basic_block_sptr> gr_basic_block_vector_t;
119 typedef std::vector<gr_basic_block_sptr>::iterator gr_basic_block_viter_t;
120
121 long gr_basic_block_ncurrently_allocated();
122
123 inline std::ostream &operator << (std::ostream &os, gr_basic_block_sptr basic_block)
124 {
125     os << basic_block->name() << "(" << basic_block->unique_id() << ")";
126     return os;
127 }
128
129 #endif /* INCLUDED_GR_BASIC_BLOCK_H */