FastLED 3.9.15
Loading...
Searching...
No Matches
function_list.h
Go to the documentation of this file.
1#pragma once
2#include "fl/function.h"
3#include "fl/pair.h"
4#include "fl/vector.h"
5#include "fl/type_traits.h"
6
7namespace fl {
8
9template <typename FunctionType> class FunctionListBase {
10 protected:
12 int mCounter = 0;
13
14 public:
15 // Iterator types
18
19 FunctionListBase() = default;
20 ~FunctionListBase() = default;
21
22 int add(FunctionType function) {
23 int id = mCounter++;
25 mFunctions.push_back(entry);
26 return id;
27 }
28
29 void remove(int id) {
30 for (int i = mFunctions.size() - 1; i >= 0; --i) {
31 if (mFunctions[i].first == id) {
32 mFunctions.erase(mFunctions.begin() + i);
33 }
34 }
35 }
36
37
38
39 void clear() { mFunctions.clear(); }
40
41 // Iterator methods
42 iterator begin() { return mFunctions.begin(); }
43 iterator end() { return mFunctions.end(); }
44 const_iterator begin() const { return mFunctions.begin(); }
45 const_iterator end() const { return mFunctions.end(); }
46 const_iterator cbegin() const { return mFunctions.cbegin(); }
47 const_iterator cend() const { return mFunctions.cend(); }
48
49 // Size information
50 size_t size() const { return mFunctions.size(); }
51 bool empty() const { return mFunctions.empty(); }
52};
53
54template <typename... Args>
55class FunctionList : public FunctionListBase<function<void(Args...)>> {
56 public:
57 void invoke(Args... args) {
58 for (const auto &pair : this->mFunctions) {
59 auto &function = pair.second;
60 function(args...);
61 }
62 }
63};
64
65template <>
66class FunctionList<void> : public FunctionListBase<function<void()>> {
67 public:
68 void invoke() {
69 for (const auto &pair : this->mFunctions) {
70 auto &function = pair.second;
71 function();
72 }
73 }
74};
75
76template <>
77class FunctionList<void()> : public FunctionListBase<function<void()>> {
78 public:
79 void invoke() {
80 for (const auto &pair : this->mFunctions) {
81 auto &function = pair.second;
82 function();
83 }
84 }
85};
86
87} // namespace fl
void invoke(Args... args)
typename fl::vector< pair< int, FunctionType > >::const_iterator const_iterator
~FunctionListBase()=default
FunctionListBase()=default
const_iterator end() const
int add(FunctionType function)
const_iterator cbegin() const
const_iterator begin() const
size_t size() const
const_iterator cend() const
fl::vector< pair< int, FunctionType > > mFunctions
typename fl::vector< pair< int, FunctionType > >::iterator iterator
Pair< Key, Value > pair
Definition pair.h:13
HeapVector< T, Allocator > vector
Definition vector.h:1074
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
corkscrew_args args
Definition old.h:142