Imported Upstream version 3.2.2
[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 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_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 misc
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 class gr_test : public gr_block {
54
55  public:
56   
57   ~gr_test (){}
58   
59 int general_work (int noutput_items,
60                             gr_vector_int &ninput_items,
61                             gr_vector_const_void_star &input_items,
62                             gr_vector_void_star &output_items);
63   // ----------------------------------------------------------------
64   //            override these to define your behavior
65   // ----------------------------------------------------------------
66
67   /*!
68    * \brief  Estimate input requirements given output request
69    *
70    * \param noutput_items           number of output items to produce
71    * \param ninput_items_required   number of input items required on each input stream
72    *
73    * Given a request to product \p noutput_items, estimate the number of
74    * data items required on each input stream.  The estimate doesn't have
75    * to be exact, but should be close.
76    */
77    void forecast (int noutput_items,
78                          gr_vector_int &ninput_items_required)
79    {
80      unsigned ninputs = ninput_items_required.size ();
81      for (unsigned i = 0; i < ninputs; i++)
82        ninput_items_required[i] = (int)((double)noutput_items / relative_rate()) + (int)history();
83    }
84
85
86   /*!
87    * \brief Force check topology to return true or false.
88    *
89    * \param check_topology      value to return when check_topology is called (true or false)
90    * default check_topology returns true
91    *
92    */
93    void set_check_topology (bool check_topology){ d_check_topology=check_topology;}
94
95   /*!
96    * \brief Confirm that ninputs and noutputs is an acceptable combination.
97    *
98    * \param ninputs     number of input streams connected
99    * \param noutputs    number of output streams connected
100    *
101    * \returns true if this is a valid configuration for this block.
102    *
103    * This function is called by the runtime system whenever the
104    * topology changes.  Most classes do not need to override this.
105    * This check is in addition to the constraints specified by the input
106    * and output gr_io_signatures.
107    */
108   bool check_topology (int ninputs, int noutputs) { return d_check_topology;}
109
110   // ----------------------------------------------------------------
111   /*
112    * The following two methods provide special case info to the
113    * scheduler in the event that a block has a fixed input to output
114    * ratio.  gr_sync_block, gr_sync_decimator and gr_sync_interpolator
115    * override these.  If you're fixed rate, subclass one of those.
116    */
117   /*!
118    * \brief Given ninput samples, return number of output samples that will be produced.
119    * N.B. this is only defined if fixed_rate returns true.
120    * Generally speaking, you don't need to override this.
121    */
122   int fixed_rate_ninput_to_noutput(int ninput) { return (int)((double)ninput/relative_rate()); }
123
124   /*!
125    * \brief Given noutput samples, return number of input samples required to produce noutput.
126    * N.B. this is only defined if fixed_rate returns true.
127    */
128   int fixed_rate_noutput_to_ninput(int noutput)  { return (int)((double)noutput*relative_rate()); }
129
130   /*!
131    * \brief Set if fixed rate should return true.
132    * N.B. This is normally a private method but we make it available here as public.
133    */
134   void set_fixed_rate_public(bool fixed_rate){ set_fixed_rate(fixed_rate);}
135
136   /*!
137    * \brief Set the consume pattern.
138    *
139    * \param cons_type   which consume pattern to use
140    */
141   void set_consume_type (gr_consume_type_t cons_type) { d_consume_type=cons_type;}
142
143   /*!
144    * \brief Set the consume limit.
145    *
146    * \param limit       min or maximum items to consume (depending on consume_type)
147    */
148   void set_consume_limit (unsigned int limit) { d_min_consume=limit; d_max_consume=limit;}
149
150   /*!
151    * \brief Set the produce pattern.
152    *
153    * \param prod_type   which produce pattern to use
154    */
155   void set_produce_type (gr_produce_type_t prod_type) { d_produce_type=prod_type;}
156
157   /*!
158    * \brief Set the produce limit.
159    *
160    * \param limit       min or maximum items to produce (depending on produce_type)
161    */
162   void set_produce_limit (unsigned int limit) { d_min_produce=limit; d_max_produce=limit;}
163
164   // ----------------------------------------------------------------------------
165
166
167   
168  protected:
169   unsigned int d_sizeof_input_item;
170   unsigned int d_sizeof_output_item;
171   bool d_check_topology;
172   char d_temp;
173   gr_consume_type_t d_consume_type;
174   int d_min_consume;
175   int d_max_consume;
176   gr_produce_type_t d_produce_type;
177   int d_min_produce;
178   int d_max_produce;
179   gr_test (const std::string &name,int min_inputs, int max_inputs, unsigned int sizeof_input_item,
180                                    int min_outputs, int max_outputs, unsigned int sizeof_output_item,
181                                    unsigned int history,unsigned int output_multiple,double relative_rate,
182                                    bool fixed_rate,gr_consume_type_t cons_type, gr_produce_type_t prod_type);
183
184
185
186   friend gr_test_sptr gr_make_test (const std::string &name,int min_inputs, int max_inputs, unsigned int sizeof_input_item,
187                                    int min_outputs, int max_outputs, unsigned int sizeof_output_item,
188                                    unsigned int history,unsigned int output_multiple,double relative_rate,
189                                    bool fixed_rate,gr_consume_type_t cons_type, gr_produce_type_t prod_type);
190 };
191
192
193
194 #endif /* INCLUDED_GR_TEST_H */