SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
join.h
Go to the documentation of this file.
1#ifndef SENSESP_SRC_SENSESP_TRANSFORMS_JOIN_H_
2#define SENSESP_SRC_SENSESP_TRANSFORMS_JOIN_H_
3
4#include <elapsedMillis.h>
5#include <tuple>
6
9#include "transform.h"
10
11namespace sensesp {
12
14// Base class for Join. This is needed until Arduino ESP32 Core supports C++14
15// and template pack indices: https://stackoverflow.com/a/55807564/2999754
16
17template <int N>
18class JoinBase {
19 public:
20 JoinBase(long max_age = 0) : max_age_{max_age} {
21 for (int i = 0; i < N; i++) {
22 age_[i] = max_age;
23 }
24 }
25
26 protected:
27 elapsedMillis age_[N];
29
30 virtual void emit_tuple() = 0;
31
32 void check_emit() {
33 for (int i = 0; i < N; i++) {
34 if (max_age_ != 0 && age_[i] > max_age_) {
35 return;
36 }
37 }
38 emit_tuple();
39 }
40};
41
50template <typename T1, typename T2>
51class Join : public JoinBase<2>, public ValueProducer<std::tuple<T1, T2>> {
52 public:
53 Join(long max_age = 0) : JoinBase<2>(max_age) {}
54
55 std::tuple<LambdaConsumer<T1>, LambdaConsumer<T2>> consumers = {
56 LambdaConsumer<T1>([this](T1 value) {
57 std::get<0>(values) = value;
58 age_[0] = 0;
59 check_emit();
60 }),
61 LambdaConsumer<T2>([this](T2 value) {
62 std::get<1>(values) = value;
63 age_[1] = 0;
64 check_emit();
65 })};
66
67 protected:
68 std::tuple<T1, T2> values;
69
70 void emit_tuple() override { this->emit(values); }
71};
72
80template <typename T1, typename T2, typename T3>
81class Join3 : public JoinBase<3>, public ValueProducer<std::tuple<T1, T2, T3>> {
82 public:
83 Join3(long max_age = 0) : JoinBase<3>(max_age) {}
84
85 std::tuple<LambdaConsumer<T1>, LambdaConsumer<T2>, LambdaConsumer<T3>>
86 consumers = {LambdaConsumer<T1>([this](T1 value) {
87 std::get<0>(values) = value;
88 age_[0] = 0;
89 check_emit();
90 }),
91 LambdaConsumer<T2>([this](T2 value) {
92 std::get<1>(values) = value;
93 age_[1] = 0;
94 check_emit();
95 }),
96 LambdaConsumer<T3>([this](T3 value) {
97 std::get<2>(values) = value;
98 age_[2] = 0;
99 check_emit();
100 })};
101
102 protected:
103 std::tuple<T1, T2, T3> values;
104
105 void emit_tuple() override { this->emit(values); }
106};
107
115template <typename T1, typename T2, typename T3, typename T4>
116class Join4 : public JoinBase<4>,
117 public ValueProducer<std::tuple<T1, T2, T3, T4>> {
118 public:
119 Join4(long max_age = 0) : JoinBase<4>(max_age) {}
120
121 std::tuple<LambdaConsumer<T1>, LambdaConsumer<T2>, LambdaConsumer<T3>,
123 consumers = {LambdaConsumer<T1>([this](T1 value) {
124 std::get<0>(values) = value;
125 age_[0] = 0;
126 check_emit();
127 }),
128 LambdaConsumer<T2>([this](T2 value) {
129 std::get<1>(values) = value;
130 age_[1] = 0;
131 check_emit();
132 }),
133 LambdaConsumer<T3>([this](T3 value) {
134 std::get<2>(values) = value;
135 age_[2] = 0;
136 check_emit();
137 }),
138 LambdaConsumer<T4>([this](T4 value) {
139 std::get<3>(values) = value;
140 age_[3] = 0;
141 check_emit();
142 })};
143
144 protected:
145 std::tuple<T1, T2, T3, T4> values;
146
147 void emit_tuple() override { this->emit(values); }
148};
149
157template <typename T1, typename T2, typename T3, typename T4, typename T5>
158class Join5 : public JoinBase<5>,
159 public ValueProducer<std::tuple<T1, T2, T3, T4, T5>> {
160 public:
161 Join5(long max_age = 0) : JoinBase<5>(max_age) {}
162
163 std::tuple<LambdaConsumer<T1>, LambdaConsumer<T2>, LambdaConsumer<T3>,
165 consumers = {LambdaConsumer<T1>([this](T1 value) {
166 std::get<0>(values) = value;
167 age_[0] = 0;
168 check_emit();
169 }),
170 LambdaConsumer<T2>([this](T2 value) {
171 std::get<1>(values) = value;
172 age_[1] = 0;
173 check_emit();
174 }),
175 LambdaConsumer<T3>([this](T3 value) {
176 std::get<2>(values) = value;
177 age_[2] = 0;
178 check_emit();
179 }),
180 LambdaConsumer<T4>([this](T4 value) {
181 std::get<3>(values) = value;
182 age_[3] = 0;
183 check_emit();
184 }),
185 LambdaConsumer<T5>([this](T5 value) {
186 std::get<4>(values) = value;
187 age_[4] = 0;
188 check_emit();
189 })};
190
191 protected:
192 std::tuple<T1, T2, T3, T4, T5> values;
193
194 void emit_tuple() override { this->emit(values); }
195};
196
197} // namespace sensesp
198
199#endif
Merge three producer values into a tuple.
Definition join.h:81
std::tuple< LambdaConsumer< T1 >, LambdaConsumer< T2 >, LambdaConsumer< T3 > > consumers
Definition join.h:86
Join3(long max_age=0)
Definition join.h:83
void emit_tuple() override
Definition join.h:105
std::tuple< T1, T2, T3 > values
Definition join.h:103
Merge four producer values into a tuple.
Definition join.h:117
std::tuple< T1, T2, T3, T4 > values
Definition join.h:145
std::tuple< LambdaConsumer< T1 >, LambdaConsumer< T2 >, LambdaConsumer< T3 >, LambdaConsumer< T4 > > consumers
Definition join.h:123
void emit_tuple() override
Definition join.h:147
Join4(long max_age=0)
Definition join.h:119
Merge five producer values into a tuple.
Definition join.h:159
std::tuple< LambdaConsumer< T1 >, LambdaConsumer< T2 >, LambdaConsumer< T3 >, LambdaConsumer< T4 >, LambdaConsumer< T5 > > consumers
Definition join.h:165
std::tuple< T1, T2, T3, T4, T5 > values
Definition join.h:192
Join5(long max_age=0)
Definition join.h:161
void emit_tuple() override
Definition join.h:194
elapsedMillis age_[N]
Definition join.h:27
void check_emit()
Definition join.h:32
JoinBase(long max_age=0)
Definition join.h:20
virtual void emit_tuple()=0
long max_age_
Definition join.h:28
Join two producer values into a tuple.
Definition join.h:51
Join(long max_age=0)
Definition join.h:53
void emit_tuple() override
Definition join.h:70
std::tuple< LambdaConsumer< T1 >, LambdaConsumer< T2 > > consumers
Definition join.h:55
std::tuple< T1, T2 > values
Definition join.h:68
Provides an easy way of calling a function based on the output of any ValueProducer.
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const std::tuple< T1, T2 > &new_value)