roo_transceivers
API Documentation for roo_transceivers
Loading...
Searching...
No Matches
binding.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5#include "roo_logging.h"
9
10namespace roo_transceivers {
11
12/// Persistent binding of a sensor key to a locator.
14 public:
16 : loc_(), store_(store), key_(key), synced_(false) {}
17
18 /// Returns the bound locator (empty if unbound).
20 sync();
21 return loc_;
22 }
23
24 /// Returns true when a locator is bound.
25 bool isBound() const {
26 sync();
27 return loc_.isDefined();
28 }
29
30 /// Binds to the specified locator (or unbinds when undefined).
31 void bind(const SensorLocator& loc) {
32 if (loc_ == loc) return;
33 loc_ = loc;
34 if (!loc_.isDefined()) {
35 store_.clearSensorBinding(key_);
36 } else {
37 store_.setSensorBinding(key_, loc_);
38 }
39 synced_ = true;
40 }
41
42 /// Clears the binding.
43 void unbind() {
44 loc_ = SensorLocator();
45 store_.clearSensorBinding(key_);
46 synced_ = true;
47 }
48
49 private:
50 friend roo_logging::Stream& operator<<(roo_logging::Stream& os,
51 const SensorBinding& binding);
52
53 void sync() const {
54 if (!synced_) {
55 loc_ = store_.getSensorBinding(key_);
56 synced_ = true;
57 }
58 }
59 mutable SensorLocator loc_;
60
61 BindingStore& store_;
63
64 mutable bool synced_;
65};
66
68 public:
69 BoundSensor(Universe& universe, const SensorBinding* binding)
70 : universe_(universe), binding_(binding) {}
71
72 /// Reads the bound sensor or returns an initial measurement if unbound.
73 Measurement read() const {
74 SensorLocator loc = binding_->get();
75 if (loc.isDefined()) {
76 return universe_.read(loc);
77 }
78 return Measurement();
79 }
80
81 private:
82 friend roo_logging::Stream& operator<<(roo_logging::Stream& os,
83 const BoundSensor& sensor);
84
85 Universe& universe_;
86 const SensorBinding* binding_;
87};
88
90 public:
92 : loc_(), store_(store), key_(key), synced_(false) {}
93
94 /// Returns the bound locator (empty if unbound).
96 sync();
97 return loc_;
98 }
99
100 /// Returns true when a locator is bound.
101 bool isBound() const {
102 sync();
103 return loc_.isDefined();
104 }
105
106 /// Binds to the specified locator (or unbinds when undefined).
107 void bind(const ActuatorLocator& loc) {
108 if (loc_ == loc) return;
109 loc_ = loc;
110 if (!loc_.isDefined()) {
111 store_.clearActuatorBinding(key_);
112 } else {
113 store_.setActuatorBinding(key_, loc_);
114 }
115 synced_ = true;
116 }
117
118 /// Clears the binding.
119 void unbind() {
120 loc_ = ActuatorLocator();
121 store_.clearActuatorBinding(key_);
122 synced_ = true;
123 }
124
125 private:
126 friend roo_logging::Stream& operator<<(roo_logging::Stream& os,
127 const ActuatorBinding& binding);
128
129 void sync() const {
130 if (!synced_) {
131 loc_ = store_.getActuatorBinding(key_);
132 synced_ = true;
133 }
134 }
135 mutable ActuatorLocator loc_;
136
137 BindingStore& store_;
139
140 mutable bool synced_;
141};
142
144 public:
145 BoundActuator(Universe& universe, const ActuatorBinding* binding)
146 : universe_(universe), binding_(binding) {}
147
148 /// Writes to the bound actuator, if any.
149 bool write(float value) const {
150 ActuatorLocator loc = binding_->get();
151 if (loc.isDefined()) {
152 return universe_.write(loc, value);
153 }
154 return false;
155 }
156
157 private:
158 friend roo_logging::Stream& operator<<(roo_logging::Stream& os,
159 const BoundActuator& actuator);
160
161 Universe& universe_;
162 const ActuatorBinding* binding_;
163};
164
165/// Adapter for actuators that can be read, using actuator id as sensor id.
167 public:
169 : universe_(universe), binding_(binding) {}
170
171 /// Reads from the bound actuator as a sensor.
173 ActuatorLocator loc = binding_->get();
174 if (loc.isDefined()) {
175 SensorLocator sensor_loc(loc.device_locator(), loc.actuator_id());
176 return universe_.read(sensor_loc);
177 }
178 return Measurement();
179 }
180
181 /// Writes to the bound actuator.
182 bool write(float value) const {
183 ActuatorLocator loc = binding_->get();
184 if (loc.isDefined()) {
185 return universe_.write(loc, value);
186 }
187 return false;
188 }
189
190 private:
191 friend roo_logging::Stream& operator<<(roo_logging::Stream& os,
192 const BoundSensingActuator& actuator);
193
194 Universe& universe_;
195 const ActuatorBinding* binding_;
196};
197
198} // namespace roo_transceivers
ActuatorBinding(BindingStore &store, BindingStore::ActuatorKey key)
Definition binding.h:91
friend roo_logging::Stream & operator<<(roo_logging::Stream &os, const ActuatorBinding &binding)
Definition binding.cpp:22
void bind(const ActuatorLocator &loc)
Binds to the specified locator (or unbinds when undefined).
Definition binding.h:107
bool isBound() const
Returns true when a locator is bound.
Definition binding.h:101
ActuatorLocator get() const
Returns the bound locator (empty if unbound).
Definition binding.h:95
void unbind()
Clears the binding.
Definition binding.h:119
Identifies actuator within a transceiver device.
Definition id.h:104
const DeviceLocator & device_locator() const
Returns the device locator.
Definition id.h:115
bool isDefined() const
Returns true if the locator is defined.
Definition id.h:127
const ActuatorId & actuator_id() const
Returns the actuator id.
Definition id.h:124
virtual SensorLocator getSensorBinding(SensorKey key)=0
virtual void setActuatorBinding(ActuatorKey key, const ActuatorLocator &locator)=0
virtual void setSensorBinding(SensorKey key, const SensorLocator &locator)=0
virtual void clearActuatorBinding(ActuatorKey key)=0
virtual void clearSensorBinding(SensorKey key)=0
virtual ActuatorLocator getActuatorBinding(ActuatorKey key)=0
BoundActuator(Universe &universe, const ActuatorBinding *binding)
Definition binding.h:145
friend roo_logging::Stream & operator<<(roo_logging::Stream &os, const BoundActuator &actuator)
Definition binding.cpp:33
bool write(float value) const
Writes to the bound actuator, if any.
Definition binding.h:149
Adapter for actuators that can be read, using actuator id as sensor id.
Definition binding.h:166
BoundSensingActuator(Universe &universe, const ActuatorBinding *binding)
Definition binding.h:168
bool write(float value) const
Writes to the bound actuator.
Definition binding.h:182
friend roo_logging::Stream & operator<<(roo_logging::Stream &os, const BoundSensingActuator &actuator)
Definition binding.cpp:39
Measurement read() const
Reads from the bound actuator as a sensor.
Definition binding.h:172
BoundSensor(Universe &universe, const SensorBinding *binding)
Definition binding.h:69
Measurement read() const
Reads the bound sensor or returns an initial measurement if unbound.
Definition binding.h:73
friend roo_logging::Stream & operator<<(roo_logging::Stream &os, const BoundSensor &sensor)
Definition binding.cpp:16
Measurement of a quantity at a specific time.
Definition measurement.h:11
Persistent binding of a sensor key to a locator.
Definition binding.h:13
bool isBound() const
Returns true when a locator is bound.
Definition binding.h:25
friend roo_logging::Stream & operator<<(roo_logging::Stream &os, const SensorBinding &binding)
Definition binding.cpp:5
void bind(const SensorLocator &loc)
Binds to the specified locator (or unbinds when undefined).
Definition binding.h:31
SensorBinding(BindingStore &store, BindingStore::SensorKey key)
Definition binding.h:15
void unbind()
Clears the binding.
Definition binding.h:43
SensorLocator get() const
Returns the bound locator (empty if unbound).
Definition binding.h:19
Identifies sensor within a transceiver device.
Definition id.h:57
bool isDefined() const
Returns true if the locator is defined.
Definition id.h:79
An abstract collection of transceiver devices.
Definition universe.h:19
virtual bool write(const ActuatorLocator &locator, float value)=0
Writes to the actuator identified by locator.
virtual Measurement read(const SensorLocator &locator) const =0
Returns the latest known reading of the sensor identified by locator.