FastLED 3.9.3
Loading...
Searching...
No Matches
callback.h
1#pragma once
2
3#include "namespace.h"
4
5FASTLED_NAMESPACE_BEGIN
6
12template<typename ...Args>
13class Callback {
14public:
15 Callback() = default;
16 // Member function constructor.
17 Callback(void* self, void (*callback)(void* self, Args... args)) : self(self), callback(callback) {}
18 // Free function constructor.
19 explicit Callback(void* (*callback)(Args... args)) : self(nullptr), callback((void (*)(void*, Args...))callback) {}
20 Callback(const Callback&) = default;
21 operator bool() const { return callback != nullptr; }
22 void operator()(Args... args) const { if (callback) callback(self, args...); }
23 void clear() { callback = nullptr; self = nullptr; }
24 bool operator==(const Callback& other) const { return self == other.self && callback == other.callback; }
25 bool operator!=(const Callback& other) const { return !(*this == other); }
26 Callback& operator=(const Callback& other) { self = other.self; callback = other.callback; return *this; }
27 Callback& operator=(void* (*other)(Args... args)) { self = nullptr; callback = (void (*)(void*, Args...))other; return *this; }
28 Callback& operator=(void (*other)(void* self, Args... args)) { self = nullptr; callback = other; return *this; }
29 // operator < for set/map
30 bool operator<(const Callback& other) const { return self < other.self || (self == other.self && callback < other.callback); }
31private:
32 void* self = nullptr;
33 void (*callback)(void* self, Args... args) = nullptr;
34};
35
36FASTLED_NAMESPACE_END