Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_test.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 #ifndef INCLUDED_GR_TEST_H
24 #define INCLUDED_GR_TEST_H
25
26 #include <gr_block.h>
27 #include <string>
28 #include "gr_test_types.h"
29
30 class gr_test;
31 typedef boost::shared_ptr<gr_test> gr_test_sptr;
32
33 // public constructor
34 gr_test_sptr gr_make_test (const std::string &name=std::string("gr_test"),
35         int min_inputs=1, int max_inputs=1, unsigned int sizeof_input_item=1,
36         int min_outputs=1, int max_outputs=1, unsigned int sizeof_output_item=1,
37         unsigned int history=1,unsigned int output_multiple=1,double relative_rate=1.0,
38         bool fixed_rate=true,gr_consume_type_t cons_type=CONSUME_NOUTPUT_ITEMS, gr_produce_type_t prod_type=PRODUCE_NOUTPUT_ITEMS);
39
40 /*!
41  * \brief Test class for testing runtime system (setting up buffers and such.)
42  * \ingroup block
43  *
44  * This block does not do any usefull actual data processing.
45  * It just exposes setting all standard block parameters using the contructor or public methods.
46  *
47  * This block can be usefull when testing the runtime system.
48  * You can force this block to have a large history, decimation 
49  * factor and/or large output_multiple.
50  * The runtime system should detect this and create large enough buffers
51  * all through the signal chain.
52  *
53  */
54
55
56
57
58 class gr_test : public gr_block {
59
60  public:
61   
62   ~gr_test (){}
63   
64 int general_work (int noutput_items,
65                             gr_vector_int &ninput_items,
66                             gr_vector_const_void_star &input_items,
67                             gr_vector_void_star &output_items);
68   // ----------------------------------------------------------------
69   //            override these to define your behavior
70   // ----------------------------------------------------------------
71
72   /*!
73    * \brief  Estimate input requirements given output request
74    *
75    * \param noutput_items           number of output items to produce
76    * \param ninput_items_required   number of input items required on each input stream
77    *
78    * Given a request to product \p noutput_items, estimate the number of
79    * data items required on each input stream.  The estimate doesn't have
80    * to be exact, but should be close.
81    */
82    void forecast (int noutput_items,
83                          gr_vector_int &ninput_items_required)
84    {
85      unsigned ninputs = ninput_items_required.size ();
86      for (unsigned i = 0; i < ninputs; i++)
87        ninput_items_required[i] = (int)((double)noutput_items / relative_rate()) + (int)history();
88    }
89
90
91   /*!
92    * \brief Force check topology to return true or false.
93    *
94    * \param check_topology      value to return when check_topology is called (true or false)
95    * default check_topology returns true
96    *
97    */
98    void set_check_topology (bool check_topology){ d_check_topology=check_topology;}
99
100   /*!
101    * \brief Confirm that ninputs and noutputs is an acceptable combination.
102    *
103    * \param ninputs     number of input streams connected
104    * \param noutputs    number of output streams connected
105    *
106    * \returns true if this is a valid configuration for this block.
107    *
108    * This function is called by the runtime system whenever the
109    * topology changes.  Most classes do not need to override this.
110    * This check is in addition to the constraints specified by the input
111    * and output gr_io_signatures.
112    */
113   bool check_topology (int ninputs, int noutputs) { return d_check_topology;}
114
115   // ----------------------------------------------------------------
116   /*
117    * The following two methods provide special case info to the
118    * scheduler in the event that a block has a fixed input to output
119    * ratio.  gr_sync_block, gr_sync_decimator and gr_sync_interpolator
120    * override these.  If you're fixed rate, subclass one of those.
121    */
122   /*!
123    * \brief Given ninput samples, return number of output samples that will be produced.
124    * N.B. this is only defined if fixed_rate returns true.
125    * Generally speaking, you don't need to override this.
126    */
127   int fixed_rate_ninput_to_noutput(int ninput) { return (int)((double)ninput/relative_rate()); }
128
129   /*!
130    * \brief Given noutput samples, return number of input samples required to produce noutput.
131    * N.B. this is only defined if fixed_rate returns true.
132    */
133   int fixed_rate_noutput_to_ninput(int noutput)  { return (int)((double)noutput*relative_rate()); }
134
135   /*!
136    * \brief Set if fixed rate should return true.
137    * N.B. This is normally a private method but we make it available here as public.
138    */
139   void set_fixed_rate_public(bool fixed_rate){ set_fixed_rate(fixed_rate);}
140
141   /*!
142    * \brief Set the consume pattern.
143    *
144    * \param cons_type   which consume pattern to use
145    */
146   void set_consume_type (gr_consume_type_t cons_type) { d_consume_type=cons_type;}
147
148   /*!
149    * \brief Set the consume limit.
150    *
151    * \param limit       min or maximum items to consume (depending on consume_type)
152    */
153   void set_consume_limit (unsigned int limit) { d_min_consume=limit; d_max_consume=limit;}
154
155   /*!
156    * \brief Set the produce pattern.
157    *
158    * \param prod_type   which produce pattern to use
159    */
160   void set_produce_type (gr_produce_type_t prod_type) { d_produce_type=prod_type;}
161
162   /*!
163    * \brief Set the produce limit.
164    *
165    * \param limit       min or maximum items to produce (depending on produce_type)
166    */
167   void set_produce_limit (unsigned int limit) { d_min_produce=limit; d_max_produce=limit;}
168
169   // ----------------------------------------------------------------------------
170
171
172   
173  protected:
174   unsigned int d_sizeof_input_item;
175   unsigned int d_sizeof_output_item;
176   bool d_check_topology;
177   char d_temp;
178   gr_consume_type_t d_consume_type;
179   int d_min_consume;
180   int d_max_consume;
181   gr_produce_type_t d_produce_type;
182   int d_min_produce;
183   int d_max_produce;
184   gr_test (const std::string &name,int min_inputs, int max_inputs, unsigned int sizeof_input_item,
185                                    int min_outputs, int max_outputs, unsigned int sizeof_output_item,
186                                    unsigned int history,unsigned int output_multiple,double relative_rate,
187                                    bool fixed_rate,gr_consume_type_t cons_type, gr_produce_type_t prod_type);
188
189
190
191   friend gr_test_sptr gr_make_test (const std::string &name,int min_inputs, int max_inputs, unsigned int sizeof_input_item,
192                                    int min_outputs, int max_outputs, unsigned int sizeof_output_item,
193                                    unsigned int history,unsigned int output_multiple,double relative_rate,
194                                    bool fixed_rate,gr_consume_type_t cons_type, gr_produce_type_t prod_type);
195 };
196
197
198
199 #endif /* INCLUDED_GR_TEST_H */