HARDT - The Ham Radio DSP Toolkit
hswitch.h
1 #ifndef __HSWITCH_H
2 #define __HSWITCH_H
3 
4 #include "hreader.h"
5 #include "hwriter.h"
6 
7 #include <vector>
8 
24 template <class T>
25 class HSwitch: public HReader<T>, public HWriter<T>, public HWriterConsumer<T>
26 {
27  public:
28 
30  struct ComponentPtr
31  {
34 
37 
40  {
41  Reader = reader;
42  Writer = nullptr;
43  }
44 
47  {
48  Reader = nullptr;
49  Writer = writer;
50  }
51  };
52 
53  private:
54 
55  HWriter<T>* _writer;
56  HReader<T>* _reader;
57  size_t _blocksize;
58 
59  T* _buffer;
60 
61  bool _isWriter;
62  bool _isWriterConsumer;
63  bool _isReader;
64 
65  int _position;
66 
67  std::vector<ComponentPtr> _components;
68 
69  public:
70 
72  HSwitch(HWriter<T>* writer, size_t blocksize);
73 
75  HSwitch(HWriterConsumer<T>* consumer, size_t blocksize);
76 
78  HSwitch(HReader<T>* reader, size_t blocksize);
79 
81  void SetWriter(HWriter<T>* writer);
82 
83  public:
84 
86  ~HSwitch();
87 
89  int Write(T* src, size_t blocksize);
90 
92  int Read(T* dest, size_t blocksize);
93 
95  void SetPosition(int position);
96 
98  int GetPosition();
99 
101  void Add(HReader<T>* reader);
102 
104  void Add(HWriter<T>* writer);
105 
107  bool Start();
108 
110  bool Stop();
111 
113  bool Command(HCommand* command) {
114  if( _writer != nullptr && _position == 0 )
115  {
116  if( !_writer->Command(command) )
117  {
118  return false;
119  }
120  }
121  if( _reader != nullptr && _position == 0 )
122  {
123  if( !_reader->Command(command) )
124  {
125  return false;
126  }
127  }
128  if( _position > 0 )
129  {
130  if( _components.at(_position - 1).Reader != nullptr )
131  {
132  if( !_components.at(_position - 1).Reader->Command(command) )
133  {
134  return false;
135  }
136  }
137  if( _components.at(_position - 1).Writer != nullptr )
138  {
139  if( !_components.at(_position - 1).Writer->Command(command) )
140  {
141  return false;
142  }
143  }
144  }
145  return true;
146  }
147 };
148 
149 #endif
HReader::Command
virtual bool Command(HCommand *command)=0
HSwitch::Add
void Add(HReader< T > *reader)
Definition: hswitch.cpp:124
HSwitch::ComponentPtr::Writer
HWriter< T > * Writer
Definition: hswitch.h:36
HSwitch
Definition: hswitch.h:25
HSwitch::ComponentPtr::Reader
HReader< T > * Reader
Definition: hswitch.h:33
HSwitch::GetPosition
int GetPosition()
Definition: hswitch.cpp:118
HSwitch::Start
bool Start()
Definition: hswitch.cpp:161
HSwitch::SetPosition
void SetPosition(int position)
Definition: hswitch.cpp:101
HWriter
Definition: hwriter.h:10
HSwitch::Command
bool Command(HCommand *command)
Definition: hswitch.h:113
HSwitch::Read
int Read(T *dest, size_t blocksize)
Definition: hswitch.cpp:79
HSwitch::~HSwitch
~HSwitch()
Definition: hswitch.cpp:51
HWriter::Command
virtual bool Command(HCommand *command)=0
HReader
Definition: hreader.h:24
HSwitch::HSwitch
HSwitch(HWriter< T > *writer, size_t blocksize)
Definition: hswitch.cpp:7
HSwitch::ComponentPtr::ComponentPtr
ComponentPtr(HWriter< T > *writer)
Definition: hswitch.h:46
HCommand
Definition: hcommand.h:81
HSwitch::Stop
bool Stop()
Definition: hswitch.cpp:184
HSwitch::SetWriter
void SetWriter(HWriter< T > *writer)
Definition: hswitch.cpp:148
HSwitch::ComponentPtr
Definition: hswitch.h:30
HSwitch::ComponentPtr::ComponentPtr
ComponentPtr(HReader< T > *reader)
Definition: hswitch.h:39
HWriterConsumer
Definition: hwriterconsumer.h:8
HSwitch::Write
int Write(T *src, size_t blocksize)
Definition: hswitch.cpp:57