SoftFilters  0.1.0
Arduino framework and library of software data filters.
framework.h
Go to the documentation of this file.
1 
7 #ifndef FRAMEWORK_H
8 #define FRAMEWORK_H
9 
10 #include "LinkedList.h"
11 #include "Tree.h"
12 
20 class Filter
21 {
22  // Composite filter classes need to access protected and private members
23  // of Filter instances, and are thus marked as friends.
24  friend class FilterChain;
25  friend class FilterTree;
26 public:
68  bool push(void const * const input, void * const output)
69  {
70  if (input != NULL && update(input)) {
71  copy_to_client(output);
72  return true;
73  }
74  return false;
75  }
76 protected:
89  virtual void const * const get_output_val_ptr() = 0;
96  virtual bool update(void const * const input) = 0;
100  virtual void copy_to_client(void * const output) = 0;
101 };
102 
111 template <typename IN_T, typename OUT_T>
112 class BaseFilter : public Filter
113 {
114 public:
115 
126 //
127 // /**
128 // * Push a new data through the filter.
129 // * This function is essentially a proxy call to the Filter::push function
130 // * that does away the pointer parameter, which is supposed to be
131 // * beginner-friendly.
132 // *
133 // * @param[in] input A read-only reference to the input data.
134 // * @param[out] output The reference to the output data to be written to.
135 // *
136 // * @returns True if there is output data; false otherwise.
137 // * @note The output variable is not guaranteed to remain the same even if
138 // * the return value is false.
139 // */
140 // bool push(IN_T const &input, OUT_T &output)
141 // {
142 // return Filter::push(&input, &output);
143 // }
145 
146 protected:
147  virtual void const * const get_output_val_ptr() final { return &out_val; }
148  virtual void copy_to_client(void * const output) final
149  {
150  if (output != NULL) {
151  *(OUT_T * const) output = out_val;
152  }
153  }
157  OUT_T out_val;
158 };
159 
166 template <typename T>
167 class PassThroughFilter : public Filter
168 {
169 public:
170  PassThroughFilter() : ptr(NULL) { }
171 protected:
172  virtual void const * const get_output_val_ptr() override { return ptr; }
173  virtual bool update(void const * const input) override
174  {
175  ptr = (T const * const) input;
176  return true;
177  }
178  virtual void copy_to_client(void * const output) override
179  {
180  if (output != NULL) {
181  *(T * const) output = *ptr;
182  }
183  }
184  T const *ptr;
185 };
186 
190 class FilterChain : public LinkedList<Filter *>, public Filter
191 {
192 protected:
193  virtual void const * const get_output_val_ptr() final
194  {
195  return (*last())->get_output_val_ptr();
196  }
197  virtual bool update(void const * const input) final
198  {
199  for (it = begin(); it != last(); ++it) {
200  if (!(*it)->update(it != begin() ? (*prev)->get_output_val_ptr() : input)) {
201  return false;
202  }
203  prev = it;
204  }
205  return (*it)->update((*prev)->get_output_val_ptr());
206  }
207  virtual void copy_to_client(void * const output) final
208  {
209  if (output != NULL && !isEmpty()) {
210  (*last())->copy_to_client(output);
211  }
212  }
213 private:
216 };
217 
226 class FilterTree : public Tree<Filter *>
227 {
228 };
229 
230 #endif
virtual void const *const get_output_val_ptr()=0
Read-only access to the internal output memory.
virtual bool update(void const *const input) final
Internally update the filter output based on the given input.
Definition: framework.h:197
A tree of interconnected filters.
Definition: framework.h:226
virtual void const *const get_output_val_ptr() final
Push a new data through the filter.
Definition: framework.h:147
Definition: LinkedList.h:24
virtual void const *const get_output_val_ptr() override
Read-only access to the internal output memory.
Definition: framework.h:172
T const * ptr
Pointer to the latest data that passed through.
Definition: framework.h:184
The linked list class to support the Tree class and FilterChain class.
virtual void const *const get_output_val_ptr() final
Read-only access to the internal output memory.
Definition: framework.h:193
Definition: Tree.h:22
virtual void copy_to_client(void *const output)=0
Copy the output to client memory.
A chain of filters.
Definition: framework.h:190
A pass-through filter does nothing and is useful for derived classes to perform monitoring functional...
Definition: framework.h:167
The typed filter base class.
Definition: framework.h:112
virtual void copy_to_client(void *const output) final
Copy the output to client memory.
Definition: framework.h:207
OUT_T out_val
Internally managed storage of the output value.
Definition: framework.h:157
Definition: LinkedList.h:54
virtual void copy_to_client(void *const output) override
Copy the output to client memory.
Definition: framework.h:178
The tree class to support the filter framework.
The Filter interface without type checking at compile time.
Definition: framework.h:20
virtual bool update(void const *const input) override
Internally update the filter output based on the given input.
Definition: framework.h:173
bool push(void const *const input, void *const output)
Push a new data through the filter.
Definition: framework.h:68
virtual bool update(void const *const input)=0
Internally update the filter output based on the given input.
virtual void copy_to_client(void *const output) final
Copy the output to client memory.
Definition: framework.h:148