Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_block_detail.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 2, 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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_block_detail.h>
28 #include <gr_buffer.h>
29
30 static long s_ncurrently_allocated = 0;
31
32 long
33 gr_block_detail_ncurrently_allocated ()
34 {
35   return s_ncurrently_allocated;
36 }
37
38 gr_block_detail::gr_block_detail (unsigned int ninputs, unsigned int noutputs)
39   : d_ninputs (ninputs), d_noutputs (noutputs),
40     d_input (ninputs), d_output (noutputs),
41     d_done (false)
42 {
43   s_ncurrently_allocated++;
44 }
45
46 gr_block_detail::~gr_block_detail ()
47 {
48   // should take care of itself
49   s_ncurrently_allocated--;
50 }
51
52 void
53 gr_block_detail::set_input (unsigned int which, gr_buffer_reader_sptr reader)
54 {
55   if (which >= d_ninputs)
56     throw std::invalid_argument ("gr_block_detail::set_input");
57
58   d_input[which] = reader;
59 }
60
61 void
62 gr_block_detail::set_output (unsigned int which, gr_buffer_sptr buffer)
63 {
64   if (which >= d_noutputs)
65     throw std::invalid_argument ("gr_block_detail::set_output");
66
67   d_output[which] = buffer;
68 }
69
70 gr_block_detail_sptr
71 gr_make_block_detail (unsigned int ninputs, unsigned int noutputs)
72 {
73   return gr_block_detail_sptr (new gr_block_detail (ninputs, noutputs));
74 }
75
76 void
77 gr_block_detail::set_done (bool done)
78 {
79   d_done = done;
80   for (unsigned int i = 0; i < d_noutputs; i++)
81     d_output[i]->set_done (done);
82
83   for (unsigned int i = 0; i < d_ninputs; i++)
84     d_input[i]->set_done (done);
85 }
86
87 void 
88 gr_block_detail::consume (int which_input, int how_many_items)
89 {
90   if (how_many_items > 0)
91     input (which_input)->update_read_pointer (how_many_items);
92 }
93
94 void
95 gr_block_detail::consume_each (int how_many_items)
96 {
97   if (how_many_items > 0)
98     for (int i = 0; i < ninputs (); i++)
99       d_input[i]->update_read_pointer (how_many_items);
100 }
101
102 void
103 gr_block_detail::produce_each (int how_many_items)
104 {
105   if (how_many_items > 0)
106     for (int i = 0; i < noutputs (); i++)
107       d_output[i]->update_write_pointer (how_many_items);
108 }