pixled-lib  1.0
function.h
1 #ifndef FUNCTIONNAL_API_H
2 #define FUNCTIONNAL_API_H
3 
4 #include <utility>
5 #include "color.h"
6 #include "time.h"
7 #include "mapping.h"
8 
9 
10 namespace pixled {
11  namespace base {
17 
32  template<typename R>
33  class Function : private PixledFunctionTrait {
34  public:
38  typedef R Type;
39 
54  virtual R operator()(led l, time t) const = 0;
55 
62  virtual Function<R>* copy() const = 0;
63 
64  virtual ~Function() {}
65  };
66  }
67 
76  template<typename T>
82  static constexpr bool value =
83  std::is_base_of<
85  typename std::remove_reference<T>::type
86  >::value;
87  };
88 
89 
96  template<typename T>
97  class Constant : public base::Function<T> {
98  private:
99  T _value;
100 
101  public:
107  Constant(const T& value) :
108  _value(value) {}
109 
116  T operator()(led, time) const override {return _value;};
117 
124  Constant<T>* copy() const override {
125  return new Constant<T>(_value);
126  }
127  };
128 
135  template<typename R>
136  class FctWrapper {
137  private:
138  const base::Function<R>* fct;
139 
140  public:
144  typedef R Type;
145 
159  : fct(fct.copy()) {
160  }
161 
167  FctWrapper(R value)
168  : fct(new Constant<R>(value)) {
169  }
170 
179  FctWrapper(const FctWrapper<R>& other)
180  : fct(other.fct->copy()) {
181  }
182 
192  fct = other.fct;
193  other.fct = nullptr;
194  }
195 
208  if(fct!=nullptr)
209  delete fct;
210  fct = other.fct->copy();
211  return *this;
212  }
213 
226  if(fct!=nullptr)
227  delete fct;
228  fct = other.fct;
229  other.fct = nullptr;
230  return *this;
231  }
232 
233 
241  const base::Function<R>& operator*() const {
242  return *fct;
243  }
244 
248  const base::Function<R>& get() const {
249  return *fct;
250  }
251 
259  if(fct!=nullptr)
260  delete fct;
261  }
262  };
263 
348  template<typename Implem, typename R, typename... Args>
349  class Function : public base::Function<R> {
350  protected:
358  std::tuple<const FctWrapper<Args>...> args;
359 
360  public:
364  using Type = typename base::Function<R>::Type;
365 
382  template<typename... Fcts>
383  Function(Fcts&& ... fcts) : args(std::forward<Fcts>(fcts)...) {
384  }
385 
396  template<std::size_t i>
397  const base::Function<typename std::tuple_element<i, decltype(args)>::type::Type>& arg() const {
398  return *std::get<i>(args);
399  }
400 
422  template<std::size_t i>
423  typename std::tuple_element<i, decltype(args)>::type::Type call(led l, time t) const {
424  return (*std::get<i>(args))(l, t);
425  }
426 
427  protected:
431  base::Function<R>* copy() const override {
432  return new Implem(static_cast<const Implem&>(*this));
433  }
434  };
435 
440  namespace detail {
450  template<typename To, typename From>
451  class Cast : public base::Function<To> {
452  private:
453  const FctWrapper<From> f;
454 
455  public:
460  : f(from) {}
465  : f(std::move(from)) {}
466 
467  To operator()(led l, time t) const override {
468  return (*this->f)(l, t);
469  }
470 
471  Cast<To, From>* copy() const override {
472  return new Cast(*f);
473  }
474  };
475  }
476 
498  template<typename To, typename From>
501  };
502 }
503 #endif
Definition: function.h:97
T operator()(led, time) const override
Definition: function.h:116
Constant< T > * copy() const override
Definition: function.h:124
Constant(const T &value)
Definition: function.h:107
Definition: function.h:136
const base::Function< R > & operator*() const
Definition: function.h:241
FctWrapper(R value)
Definition: function.h:167
FctWrapper(FctWrapper< R > &&other)
Definition: function.h:191
FctWrapper & operator=(FctWrapper< R > &&other)
Definition: function.h:225
const base::Function< R > & get() const
Definition: function.h:248
FctWrapper(const base::Function< R > &fct)
Definition: function.h:158
~FctWrapper()
Definition: function.h:258
FctWrapper & operator=(const FctWrapper< R > &other)
Definition: function.h:207
R Type
Definition: function.h:144
FctWrapper(const FctWrapper< R > &other)
Definition: function.h:179
Definition: function.h:349
base::Function< R > * copy() const override
Definition: function.h:431
Function(Fcts &&... fcts)
Definition: function.h:383
std::tuple< const FctWrapper< Args >... > args
Definition: function.h:358
std::tuple_element< i, decltype(args)>::type::Type call(led l, time t) const
Definition: function.h:423
const base::Function< typename std::tuple_element< i, decltype(args)>::type::Type > & arg() const
Definition: function.h:397
Definition: function.h:33
R Type
Definition: function.h:38
virtual Function< R > * copy() const =0
virtual R operator()(led l, time t) const =0
Definition: chrono.h:14
Definition: function.h:451
Cast(base::Function< From > &&from)
Definition: function.h:464
Cast(const base::Function< From > &from)
Definition: function.h:459
To operator()(led l, time t) const override
Definition: function.h:467
Cast< To, From > * copy() const override
Definition: function.h:471
Definition: animation.cpp:3
detail::Cast< To, typename std::remove_reference< From >::type::Type > Cast(From &&from)
Definition: function.h:499
unsigned long time
Definition: time.h:10
Definition: function.h:16
Definition: function.h:77
static constexpr bool value
Definition: function.h:82
Definition: mapping.h:19