SensESP 3.0.0-beta.3
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
valueproducer.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_VALUE_PRODUCER_H_
2#define SENSESP_SYSTEM_VALUE_PRODUCER_H_
3
4#include <ArduinoJson.h>
5
6#include "observable.h"
7#include "valueconsumer.h"
8
9namespace sensesp {
10
11// The Transform class is defined in transforms/transform.h
12template <typename C, typename P>
13class Transform;
14
23template <typename T>
24class ValueProducer : virtual public Observable {
25 public:
27 ValueProducer(const T& initial_value) : output(initial_value) {}
28
32 virtual const T& get() const { return output; }
33
38 void connect_to(ValueConsumer<T>* consumer) {
39 this->attach([this, consumer]() { consumer->set(this->get()); });
40 }
41 void connect_to(ValueConsumer<T>& consumer) {
42 this->attach([this, consumer]() { consumer.set(this->get()); });
43 }
44
54 template <typename CT>
55 void connect_to(ValueConsumer<CT>* consumer) {
56 this->attach([this, consumer]() { consumer->set(CT(this->get())); });
57 }
58 template <typename CT>
59 void connect_to(ValueConsumer<CT>& consumer) {
60 this->attach([this, consumer]() { consumer.set(CT(this->get())); });
61 }
62
69 template <typename T2>
71 this->attach([this, consumer_producer]() {
72 consumer_producer->set(T(this->get()));
73 });
74 return consumer_producer;
75 }
76 template <typename T2>
78 this->attach(
79 [this, consumer_producer]() { consumer_producer.set(T(this->get())); });
80 return &consumer_producer;
81 }
82
94 template <typename TT, typename T2>
96 this->attach([this, consumer_producer]() {
97 consumer_producer->set(TT(this->get()));
98 });
99 return consumer_producer;
100 }
101 template <typename TT, typename T2>
103 this->attach([this, consumer_producer]() {
104 consumer_producer->set(TT(this->get()));
105 });
106 return &consumer_producer;
107 }
108
109 /*
110 * Set a new output value and notify consumers about it
111 */
112 void emit(const T& new_value) {
113 this->output = new_value;
115 }
116
117 protected:
123};
124
129
130} // namespace sensesp
131
132#endif
A base class which allow observers to attach callbacks to themselves. The callbacks will be called wh...
Definition observable.h:16
void attach(std::function< void()> observer)
The main Transform class. A transform is identified primarily by the type of value that is produces (...
Definition transform.h:53
A base class for piece of code (like a transform) that accepts data for input. ValueConsumers can acc...
virtual void set(const T &new_value)
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void connect_to(ValueConsumer< T > *consumer)
void connect_to(ValueConsumer< CT > &consumer)
Transform< T, T2 > * connect_to(Transform< T, T2 > &consumer_producer)
Transform< T, T2 > * connect_to(Transform< T, T2 > *consumer_producer)
virtual const T & get() const
void emit(const T &new_value)
void connect_to(ValueConsumer< T > &consumer)
Transform< TT, T2 > * connect_to(Transform< TT, T2 > &consumer_producer)
ValueProducer(const T &initial_value)
void connect_to(ValueConsumer< CT > *consumer)
Connect a producer to a consumer of a different type.
Transform< TT, T2 > * connect_to(Transform< TT, T2 > *consumer_producer)
Connect a producer to a transform with a different input type.
ValueProducer< bool > BoolProducer
ValueProducer< float > FloatProducer
ValueProducer< int > IntProducer
ValueProducer< String > StringProducer