SensESP 3.0.1
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#include <memory>
6
7#include "observable.h"
8#include "valueconsumer.h"
9
10namespace sensesp {
11
12// The Transform class is defined in transforms/transform.h
13template <typename C, typename P>
14class Transform;
15
24template <typename T>
25class ValueProducer : virtual public Observable {
26 public:
28 ValueProducer(const T& initial_value) : output_(initial_value) {}
29
33 virtual const T& get() const { return output_; }
34
46 template <typename VConsumer>
47 typename std::enable_if<
48 std::is_base_of<ValueConsumer<typename VConsumer::input_type>, VConsumer>::value &&
49 std::is_convertible<T, typename VConsumer::input_type>::value,
50 std::shared_ptr<VConsumer>
51 >::type
52 connect_to(std::shared_ptr<VConsumer> consumer) {
53 using CInput = typename VConsumer::input_type;
54 // Capture consumer_producer as weak_ptr to avoid strong reference cycles
55 std::weak_ptr<VConsumer> weak_consumer = consumer;
56 this->attach([this, weak_consumer]() {
57 if (auto consumer = weak_consumer.lock()) {
58 consumer->set(static_cast<CInput>(this->get()));
59 }
60 });
61 return consumer;
62 }
63
64 template <typename VConsumer>
65 typename std::enable_if<
66 std::is_base_of<ValueConsumer<typename VConsumer::input_type>, VConsumer>::value &&
67 std::is_convertible<T, typename VConsumer::input_type>::value,
68 VConsumer*
69 >::type
70 connect_to(VConsumer* consumer) {
71 using CInput = typename VConsumer::input_type;
72 this->attach([this, consumer]() {
73 consumer->set(static_cast<CInput>(this->get()));
74 });
75 return consumer;
76 }
77
78 template <typename VConsumer>
79 typename std::enable_if<
80 std::is_base_of<ValueConsumer<typename VConsumer::input_type>, VConsumer>::value &&
81 std::is_convertible<T, typename VConsumer::input_type>::value,
82 VConsumer*
83 >::type
84 connect_to(VConsumer& consumer) {
85 return connect_to(&consumer);
86 }
87
88 /*
89 * Set a new output value and notify consumers about it
90 */
91 void emit(const T& new_value) {
92 this->output_ = new_value;
94 }
95
96 protected:
102};
103
108
109} // namespace sensesp
110
111#endif // SENSESP_SYSTEM_VALUE_PRODUCER_H_
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)
Definition observable.h:29
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, VConsumer * >::type connect_to(VConsumer &consumer)
virtual const T & get() const
void emit(const T &new_value)
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, VConsumer * >::type connect_to(VConsumer *consumer)
ValueProducer(const T &initial_value)
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< T, typenameVConsumer::input_type >::value, std::shared_ptr< VConsumer > >::type connect_to(std::shared_ptr< VConsumer > consumer)
Connect a producer to a transform with a different input type.
ValueProducer< bool > BoolProducer
ValueProducer< float > FloatProducer
ValueProducer< int > IntProducer
ValueProducer< String > StringProducer