HARDT - The Ham Radio DSP Toolkit
hconverter.h
1 #ifndef __HCONVERTER_H
2 #define __HCONVERTER_H
3 
4 #include "hprobe.h"
5 
12 template <class Tin, class Tout>
13 class HConverter : public HReader<Tout>, public HWriter<Tin>, public HWriterConsumer<Tout>
14 {
15  private:
16 
17  HReader<Tin>* _reader;
18  HWriter<Tout>* _writer;
19  size_t _blocksizeIn;
20  size_t _blocksizeOut;
21 
22  HProbe<Tout>* _probe;
23 
24  Tin *_input;
25  Tout *_output;
26 
27  public:
28 
32  bool Start()
33  {
34  return (_reader != NULL ? _reader->Start() : true) && (_writer != NULL ? _writer->Start() : true);
35  }
36 
40  bool Stop()
41  {
42  return (_reader != NULL ?_reader->Stop() : true) && (_writer != NULL ?_writer->Stop() : true);
43  }
44 
45  protected:
46 
55  HConverter(HReader<Tin>* reader, size_t blocksizeIn, size_t blocksizeOut, HProbe<Tout>* probe = nullptr):
56  _reader(reader),
57  _writer(nullptr),
58  _blocksizeIn(blocksizeIn),
59  _blocksizeOut(blocksizeOut),
60  _output(NULL),
61  _probe(probe) {
62 
63  _input = new Tin[blocksizeIn];
64  }
65 
74  HConverter(HWriter<Tout>* writer, size_t blocksizeIn, size_t blocksizeOut, HProbe<Tout>* probe = nullptr):
75  _reader(nullptr),
76  _writer(writer),
77  _blocksizeIn(blocksizeIn),
78  _blocksizeOut(blocksizeOut),
79  _input(NULL),
80  _probe(probe) {
81 
82  _output = new Tout[blocksizeOut];
83  }
84 
93  HConverter(HWriterConsumer<Tin>* consumer, size_t blocksizeIn, size_t blocksizeOut, HProbe<Tout>* probe = nullptr):
94  _reader(nullptr),
95  _writer(nullptr),
96  _blocksizeIn(blocksizeIn),
97  _blocksizeOut(blocksizeOut),
98  _input(NULL),
99  _probe(probe) {
100 
101  _output = new Tout[blocksizeOut];
102  consumer->SetWriter(this->Writer());
103  }
104 
105  public:
106 
111  {
112  if( _input != NULL )
113  {
114  delete[] _input;
115  }
116  if( _output != NULL )
117  {
118  delete[] _output;
119  }
120  }
121 
129  int Read(Tout* dest, size_t blocksize) {
130  if( blocksize != _blocksizeOut )
131  {
132  HError("Request for conversion-read with blocksize %d expected %d blocks", blocksize, _blocksizeOut);
133  throw new HConverterIOException("Invalid number of blocks");
134  }
135 
136  int read = _reader->Read(_input, _blocksizeIn);
137  if( read == 0 )
138  {
139  HLog("Zero length read. Returning eof (zero)");
140  return 0;
141  }
142  if( read != _blocksizeIn )
143  {
144  HError("Request for read with blocksize %d returned %d blocks", _blocksizeIn, read);
145  throw new HConverterIOException("Invalid number of blocks");
146  }
147 
148  int converted;
149  if( (converted = Convert(_input, dest, _blocksizeIn)) != _blocksizeOut )
150  {
151  HError("Request for conversion of blocksize %d blocks converted %d blocks", _blocksizeIn, converted);
152  throw new HConverterIOException("Converted incorrect number of blocks");
153  }
154 
155  if( _probe != nullptr ) {
156  _probe->Write(dest, converted);
157  }
158 
159  return _blocksizeOut;
160  };
161 
169  int Write(Tin* src, size_t blocksize) {
170  if( blocksize != _blocksizeIn )
171  {
172  HError("Request for conversion-write with blocksize %d expected %d blocks", blocksize, _blocksizeIn);
173  throw new HConverterIOException("Read incorrect number of blocks from input reader");
174  }
175 
176  int converted;
177  if( (converted = Convert(src, _output, _blocksizeIn)) != _blocksizeOut )
178  {
179  HError("Request for conversion of blocksize %d blocks converted %d blocks", _blocksizeIn, converted);
180  throw new HConverterIOException("Converted incorrect number of blocks");
181  }
182 
183  int written;
184  if( (written = _writer->Write(_output, _blocksizeOut)) != _blocksizeOut )
185  {
186  HError("Request for write with blocksize %d wrote %d blocks", _blocksizeOut, written);
187  throw new HConverterIOException("Wrote incorrect number of blocks to output writer");
188  }
189 
190  if( _probe != nullptr ) {
191  _probe->Write(_output, converted);
192  }
193 
194  return _blocksizeIn;
195  }
196 
200  virtual int Convert(Tin* src, Tout* dest, size_t blocksize) = 0;
201 
205  void SetWriter(HWriter<Tout>* writer)
206  {
207  HLog("setwriter %p", writer);
208  _writer = writer;
209  }
210 
214  bool Command(HCommand* command) {
215  if( _writer != nullptr ) {
216  return _writer->Command(command);
217  } else if( _reader != nullptr ){
218  return _reader->Command(command);
219  }
220  return true;
221  }
222 };
223 
224 #endif
HReader::Command
virtual bool Command(HCommand *command)=0
HConverter::Convert
virtual int Convert(Tin *src, Tout *dest, size_t blocksize)=0
HConverterIOException
Definition: hexceptions.h:137
HConverter::HConverter
HConverter(HWriter< Tout > *writer, size_t blocksizeIn, size_t blocksizeOut, HProbe< Tout > *probe=nullptr)
Definition: hconverter.h:74
HReader::Stop
virtual bool Stop()
Definition: hreader.h:41
HConverter::Command
bool Command(HCommand *command)
Definition: hconverter.h:214
HWriter
Definition: hwriter.h:10
HConverter::Stop
bool Stop()
Definition: hconverter.h:40
HWriterConsumer::SetWriter
virtual void SetWriter(HWriter< T > *writer)=0
HWriter::Write
virtual int Write(T *src, size_t blocksize)=0
HReader::Start
virtual bool Start()
Definition: hreader.h:35
HConverter
Definition: hconverter.h:13
HConverter::Read
int Read(Tout *dest, size_t blocksize)
Definition: hconverter.h:129
HWriter::Command
virtual bool Command(HCommand *command)=0
HReader
Definition: hreader.h:24
HWriter::Start
virtual bool Start()
Definition: hwriter.h:21
HConverter::~HConverter
~HConverter()
Definition: hconverter.h:110
HProbe< Tout >
HCommand
Definition: hcommand.h:81
HConverter::Start
bool Start()
Definition: hconverter.h:32
HReader::Read
virtual int Read(T *dest, size_t blocksize)=0
HWriter< Tin >::Writer
HWriter< Tin > * Writer()
Definition: hwriter.h:33
HConverter::Write
int Write(Tin *src, size_t blocksize)
Definition: hconverter.h:169
HWriter::Stop
virtual bool Stop()
Definition: hwriter.h:27
HConverter::HConverter
HConverter(HReader< Tin > *reader, size_t blocksizeIn, size_t blocksizeOut, HProbe< Tout > *probe=nullptr)
Definition: hconverter.h:55
HConverter::HConverter
HConverter(HWriterConsumer< Tin > *consumer, size_t blocksizeIn, size_t blocksizeOut, HProbe< Tout > *probe=nullptr)
Definition: hconverter.h:93
HConverter::SetWriter
void SetWriter(HWriter< Tout > *writer)
Definition: hconverter.h:205
HProbe::Write
int Write(T *src, size_t blocksize)
Definition: hprobe.h:40
HWriterConsumer
Definition: hwriterconsumer.h:8