Transformation.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2002-2009 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // dspsr/Kernel/Classes/dsp/Transformation.h
10 
11 #ifndef __dsp_Transformation_h
12 #define __dsp_Transformation_h
13 
14 #include "dsp/Operation.h"
15 #include "dsp/Observation.h"
16 #include "dsp/HasInput.h"
17 #include "dsp/HasOutput.h"
18 #include "dsp/BufferingPolicy.h"
19 #include "Callback.h"
20 #include "Error.h"
21 
22 #include <iostream>
23 
24 namespace dsp {
25 
27  template <class In, class Out>
29  { /* do nothing */ }
30 
32  template <class Same>
34  Reference::To<Same>& out, std::ostream* os)
35  { if (os)
36  *os << "reserve_trait resize ndat=" << in->get_ndat() << std::endl;
37  out->resize( in->get_ndat() );
38  }
39 
41  // typedef enum { inplace, outofplace, anyplace } Behaviour;
42  enum Behaviour { inplace, outofplace, anyplace };
43 
45 
48  template <class In, class Out>
49  class Transformation : public Operation,
50  public HasInput<In>,
51  public HasOutput<Out>
52  {
53 
54  public:
55 
57  Transformation (const char* _name, Behaviour _type);
58 
60  virtual ~Transformation ();
61 
63  void reserve ()
64  {
65  if (Operation::verbose) cerr << name("reserve") << std::endl;
66  reserve_trait ( this->input, this->output,
67  (Operation::verbose) ? &(this->cerr) : 0 );
68  }
69 
71  void set_input (const In* input);
72 
74  void set_output (Out* output);
75 
77  Behaviour get_type() const { return type; }
78 
80  virtual void set_buffering_policy (BufferingPolicy* policy)
81  { buffering_policy = policy; }
82 
84  bool has_buffering_policy() const
85  { return buffering_policy; }
86 
87  BufferingPolicy* get_buffering_policy () const
88  { return buffering_policy; }
89 
92 
95 
97  void reset_min_samps()
99 
101  std::string name (const std::string& function) const
102  { return "dsp::Transformation["+Operation::get_name()+"]::" + function; }
103 
105  virtual void set_cerr (std::ostream& os) const
106  {
107  Operation::set_cerr (os);
108  if (this->input)
109  this->input->set_cerr (os);
110  if (this->output)
111  this->output->set_cerr (os);
112  if (this->buffering_policy)
113  this->buffering_policy->set_cerr (os);
114  }
115 
116  protected:
117 
120 
122  virtual bool can_operate();
123 
125  virtual void operation ();
126 
128  virtual void transformation () = 0;
129 
132 
134  virtual void vchecks();
135 
136  private:
137 
139  Behaviour type;
140 
142  uint64_t rounding;
143  };
144 }
145 
147 template<class In, class Out>
149  Behaviour _type)
150  : Operation (_name)
151 {
152  if (Operation::verbose)
153  cerr << name("ctor") << std::endl;
154 
155  type = _type;
156  reset_min_samps();
157 }
158 
160 template<class In, class Out>
162 {
163  if (!this->has_input())
164  return false;
165 
166  if (minimum_samps_can_process < 0)
167  return true;
168 
169  if (int64_t(this->get_input()->get_ndat()) >= minimum_samps_can_process)
170  return true;
171 
172  if (Operation::verbose)
173  cerr << name("can_operate") <<
174  " input ndat=" << this->get_input()->get_ndat() <<
175  " min=" << minimum_samps_can_process << std::endl;
176 
177  return false;
178 }
179 
181 template <class In, class Out>
183 {
184  if (type == inplace) {
185  if (Operation::verbose)
186  cerr << name("vchecks") << " inplace checks" << std::endl;
187  // when inplace, In == Out
188  if( !this->input && this->output )
189  this->input = (In*) this->output.get();
190  if( !this->output && this->input )
191  this->output = (Out*) this->input.get();
192  }
193 
194  if (Operation::verbose)
195  cerr << name("vchecks") << " input checks" << std::endl;
196 
197  if (!this->input)
198  throw Error (InvalidState, name("vchecks"), "no input");
199 
200  if (type!=inplace && !this->output)
201  throw Error (InvalidState, name("vchecks"), "no output");
202 
203  if (Operation::verbose)
204  cerr << name("vchecks") << " done" << std::endl;
205 }
206 catch (Error& error) {
207  throw error += name("vchecks");
208 }
209 
211 template <class In, class Out>
213 {
214  if (Operation::verbose)
215  cerr << name("operation") << " call vchecks" << std::endl;
216 
217  vchecks();
218 
219  pre_transformation.send (this);
220 
221  if (buffering_policy)
222  {
223  if (Operation::verbose)
224  cerr << name("operation") <<
225  "\n calling " + buffering_policy->get_name() + "::pre_transformation"
226  " input sample=" << this->get_input()->get_input_sample() << std::endl;
227 
228  buffering_policy -> pre_transformation ();
229  }
230 
231  if (Operation::verbose)
232  cerr << name("operation") << " transformation" << std::endl;
233 
234  transformation ();
235 
236  if (buffering_policy)
237  {
238  if (Operation::verbose)
239  cerr << name("operation") << " buffering policy post_transformation" << std::endl;
240  buffering_policy -> post_transformation ();
241  }
242 
243  if (Operation::verbose)
244  cerr << name("operation") << " post_transformation callback" << std::endl;
245 
246  post_transformation.send(this);
247 
248  if (Operation::verbose)
249  cerr << name("operation") << " done" << std::endl;
250 }
251 catch (Error& error)
252 {
253  throw error += name("operation");
254 }
255 
256 
257 template <class In, class Out>
259 {
260  if (Operation::verbose)
261  cerr << "dsp::Transformation["+this->get_name()+"]::set_input ("<<_input<<")"<<std::endl;
262 
263  this->input = _input;
264 
265  if ( type == outofplace && this->input && this->output
266  && (const void*)this->input == (const void*)this->output )
267  throw Error (InvalidState, "dsp::Transformation["+this->get_name()+"]::set_input",
268  "input must != output");
269 
270  if( type==inplace )
271  this->output = (Out*)_input;
272 }
273 
274 template <class In, class Out>
276 {
277  if (Operation::verbose)
278  cerr << "dsp::Transformation["+this->get_name()+"]::set_output ("<<_output<<")"<<std::endl;
279 
280  if (type == inplace && this->input
281  && (const void*)this->input != (const void*)_output )
282  throw Error (InvalidState, "dsp::Transformation["+this->get_name()+"]::set_output",
283  "inplace transformation input must equal output");
284 
285  if ( type == outofplace && this->input && this->output
286  && (const void*)this->input.get() == (const void*)_output )
287  throw Error (InvalidState, "dsp::Transformation["+this->get_name()+"]::set_output",
288  "output must != input");
289 
290  this->output = _output;
291 
292  if( type == inplace && !this->has_input() )
293  this->input = (In*)_output;
294 
295 }
296 
297 template <class In, class Out>
299 {
300  if (Operation::verbose)
301  cerr << name("dtor") << std::endl;
302 }
303 
304 #endif
void reserve_trait(Reference::To< const In > &, Reference::To< Out > &, std::ostream *)
By default, the reserve method does nothing.
Definition: Transformation.h:33
virtual void operation()
Define the Operation pure virtual method.
Definition: Transformation.h:212
Contains all Baseband Data Reduction Library classes.
Definition: ASCIIObservation.h:17
void set_input(const In *input)
Set the container from which input data will be read.
Definition: Transformation.h:258
virtual void set_cerr(std::ostream &os) const
Set verbosity ostream.
Definition: Transformation.h:110
void reserve()
Set the size of the output to that of the input by default.
Definition: Transformation.h:68
Callback< Transformation * > pre_transformation
Functions called before the transformation takes place.
Definition: Transformation.h:96
Behaviour
All Transformations must define their behaviour.
Definition: Transformation.h:47
virtual ~Transformation()
Destructor.
Definition: Transformation.h:298
Reference::To< BufferingPolicy > buffering_policy
The buffering policy in place (if any)
Definition: Transformation.h:124
bool has_buffering_policy() const
Returns true if buffering_policy is set.
Definition: Transformation.h:89
void reset_min_samps()
Reset minimum_samps_can_process.
Definition: Transformation.h:102
virtual void set_buffering_policy(BufferingPolicy *policy)
Set the policy for buffering input and/or output data.
Definition: Transformation.h:85
Defines the interface by which Transformation data are buffered.
Definition: BufferingPolicy.h:29
std::string name
Operation name.
Definition: Operation.h:153
std::string get_name() const
Return the unique name of this operation.
Definition: Operation.h:95
Defines the interface by which operations are performed on data.
Definition: Operation.h:37
Transformation(const char *_name, Behaviour _type)
All sub-classes must specify name and capacity for inplace operation.
Definition: Transformation.h:148
virtual bool can_operate()
Return false if the input doesn't have enough data to proceed.
Definition: Transformation.h:161
virtual void transformation()=0
Declare that sub-classes must define a transformation method.
static bool verbose
Global verbosity flag.
Definition: Operation.h:48
void set_output(Out *output)
Set the container into which output data will be written.
Definition: Transformation.h:275
void send(const Type &data)
Callback< Transformation * > post_transformation
Functions called after the transformation takes place.
Definition: Transformation.h:99
virtual void vchecks()
Makes sure input & output are okay before calling transformation()
Definition: Transformation.h:182
Behaviour get_type() const
Return the Transformation type.
Definition: Transformation.h:82
virtual void set_cerr(std::ostream &) const
Reference::To< const In > input
Container from which input data will be read.
Definition: HasInput.h:49
int64_t minimum_samps_can_process
If input doesn't have this many samples, operate() returns false.
Definition: Transformation.h:136
Reference::To< Out > output
Container into which output data will be written.
Definition: HasOutput.h:49

Generated using doxygen 1.8.17