FastLED 3.9.15
Loading...
Searching...
No Matches
function.h
Go to the documentation of this file.
1#pragma once
2#include "fl/ptr.h"
3#include "fl/template_magic.h"
4
5#pragma GCC diagnostic push
6#pragma GCC diagnostic ignored "-Wfloat-equal"
7
8namespace fl {
9
10//----------------------------------------------------------------------------
11// More or less a drop in replacement for std::function
12// function<R(Args...)>: type‐erasing “std::function” replacement
13// Supports free functions, lambdas/functors, member functions (const &
14// non‑const)
15//----------------------------------------------------------------------------
16template <typename> class function;
17
18template <typename R, typename... Args> class function<R(Args...)> {
19 private:
20 struct CallableBase : public Referent {
21 virtual R invoke(Args... args) = 0;
22 virtual ~CallableBase() = default;
23 };
24
25 // Wraps a lambda/functor or free function pointer
26 template <typename F> struct Callable : CallableBase {
27 F f;
28 Callable(F fn) : f(fn) {}
29 R invoke(Args... args) override { return f(args...); }
30 };
31
32 // Wraps a non‑const member function
33 template <typename C> struct MemCallable : CallableBase {
34 C *obj;
35 R (C::*mfp)(Args...);
36 MemCallable(C *o, R (C::*m)(Args...)) : obj(o), mfp(m) {}
37 R invoke(Args... args) override { return (obj->*mfp)(args...); }
38 };
39
40 // Wraps a const member function
41 template <typename C> struct ConstMemCallable : CallableBase {
42 const C *obj;
43 R (C::*mfp)(Args...) const;
44 ConstMemCallable(const C *o, R (C::*m)(Args...) const)
45 : obj(o), mfp(m) {}
46 R invoke(Args... args) override { return (obj->*mfp)(args...); }
47 };
48
50
51 public:
52 function() = default;
53 ~function() = default;
54
56
57 function(function &&o) noexcept {
58 callable_.swap(o.callable_);
59 o.callable_.reset();
60 }
61
63 if (this != &o) {
64 callable_ = o.callable_;
65 }
66 return *this;
67 }
68
69 // doesn't work with our containers.
70 // function& operator=(function&& o) noexcept {
71 // if (this != &o) {
72 // callable_ = o.callable_;
73 // o.callable_.reset(nullptr);
74 // }
75 // return *this;
76 // }
77
78 // 1) generic constructor for lambdas, free functions, functors
79 template <typename F,
82
83 // 2) non‑const member function
84 template <typename C>
85 function(R (C::*mf)(Args...), C *obj)
86 : callable_(NewPtr<MemCallable<C>>(obj, mf)) {}
87
88 // 3) const member function
89 template <typename C>
90 function(R (C::*mf)(Args...) const, const C *obj)
91 : callable_(NewPtr<ConstMemCallable<C>>(obj, mf)) {}
92
93 // Invocation
94 R operator()(Args... args) const { return callable_->invoke(args...); }
95
96 explicit operator bool() const { return callable_ != nullptr; }
97
98 bool operator==(const function &o) const {
99 return callable_ == o.callable_;
100 }
101
102 bool operator!=(const function &o) const {
103 return callable_ != o.callable_;
104 }
105};
106
107} // namespace fl
108
109#pragma GCC diagnostic pop
Definition ptr.h:118
Referent()
Definition ptr.cpp:7
function(function &&o) noexcept
Definition function.h:57
function(R(C::*mf)(Args...) const, const C *obj)
Definition function.h:90
bool operator!=(const function &o) const
Definition function.h:102
bool operator==(const function &o) const
Definition function.h:98
function(const function &o)
Definition function.h:55
R operator()(Args... args) const
Definition function.h:94
Ptr< CallableBase > callable_
Definition function.h:49
function(R(C::*mf)(Args...), C *obj)
Definition function.h:85
function & operator=(const function &o)
Definition function.h:62
Ptr< T > NewPtr(Args... args)
Definition ptr.h:451
typename enable_if< Condition, T >::type enable_if_t
Definition type_traits.h:29
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
R invoke(Args... args) override
Definition function.h:29
virtual R invoke(Args... args)=0
ConstMemCallable(const C *o, R(C::*m)(Args...) const)
Definition function.h:44
MemCallable(C *o, R(C::*m)(Args...))
Definition function.h:36
R invoke(Args... args) override
Definition function.h:37