DUDS
Distributed Update of Data from Something
MeasurementSignalSink.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #include <duds/hardware/Instrument.hpp>
11 
12 namespace duds { namespace hardware {
13 
25 template <class SVT, class SQT, class TVT, class TQT>
27 public:
33  typedef boost::signals2::signal<
34  void (const std::shared_ptr<Instrument> &,
35  const std::shared_ptr<const Measurement> &)
37 protected:
42  boost::signals2::connection newCon;
43  boost::signals2::connection oldCon;
44  };
48  typedef std::map<Instrument*, MeasurementConnections> ConnectionMap;
49  typedef ConnectionMap ConnectionMapIterator;
56  ConnectionMap conns;
62  virtual void handleNewMeasure(
63  const std::shared_ptr<Instrument> &i,
64  const std::shared_ptr<const Measurement> &m
65  ) = 0;
71  virtual void handleOldMeasure(
72  const std::shared_ptr<Instrument> &i,
73  const std::shared_ptr<const Measurement> &m
74  ) = 0;
75 public:
80  disconnectAll();
81  }
88  for (ConnectionMapIterator iter = conns.begin(); iter != conns.end();) {
89  if (!iter->newCon.connected() && !iter->oldCon.connected()) {
90  iter = conns.erase(iter);
91  } else {
92  ++iter;
93  }
94  }
95  }
106  boost::signals2::connection newMeasurementSource(
107  const InstrumentSptr &inst,
108  boost::signals2::connect_position at = boost::signals2::at_back
109  ) {
110  // look for already existing connections to the instrument
111  ConnectionMapIterator iter = conns[inst.get()];
112  // is there a connection to this signal already?
113  if (iter->newCon.connected()) {
114  // already done; do not repeat
115  return iter->newCon;
116  }
117  // make a new connection
118  return iter->newCon = inst->newMeasurementConnect(
119  std::bind(
121  this,
122  std::placeholders::_1,
123  std::placeholders::_2
124  ),
125  at
126  );
127  }
128  boost::signals2::connection newMeasurementSource(
129  const typename MesurementSignal::group_type &group,
130  const Instrument &inst,
131  boost::signals2::connect_position at = boost::signals2::at_back
132  ) {
133  // look for already existing connections to the instrument
134  ConnectionMapIterator iter = conns[inst.get()];
135  // is there a connection to this signal already?
136  if (iter->newCon.connected()) {
137  // already done; do not repeat
138  return iter->newCon;
139  }
140  // make a new connection
141  return iter->newCon = inst->newMeasurementConnect(
142  group, std::bind(&handleNewMeasure,
143  this,
144  std::placeholders::_1,
145  std::placeholders::_2
146  ),
147  at
148  );
149  }
156  bool disconnectNewMeasurement(const InstrumentSptr &inst) {
157  // look for the instrument
158  ConnectionMapIterator iter = conns.find(inst.get());
159  if (iter != conns.end()) {
160  // disconnect it
161  iter->newCon.disconnect();
162  // are both signals disconnected?
163  if (!iter->oldCon.connected()) {
164  // remove the record for this instrument
165  conns.erase(iter);
166  return true;
167  }
168  }
169  return false;
170  }
171  boost::signals2::connection oldMeasurementSource(
172  const InstrumentSptr &inst,
173  boost::signals2::connect_position at = boost::signals2::at_back
174  ) {
175  // look for already existing connections to the instrument
176  ConnectionMapIterator iter = conns[inst.get()];
177  // is there a connection to this signal already?
178  if (iter->oldCon.connected()) {
179  // already done; do not repeat
180  return iter->oldCon;
181  }
182  // make a new connection
183  return iter->oldCon = inst->newMeasurementConnect(
184  std::bind(&handleOldMeasure, this, _1, _2), at
185  );
186  }
187  boost::signals2::connection oldMeasurementSource(
188  const typename MesurementSignal::group_type &group,
189  const Instrument &inst,
190  boost::signals2::connect_position at = boost::signals2::at_back
191  ) {
192  // look for already existing connections to the instrument
193  ConnectionMapIterator iter = conns[inst.get()];
194  // is there a connection to this signal already?
195  if (iter->oldCon.connected()) {
196  // already done; do not repeat
197  return iter->oldCon;
198  }
199  // make a new connection
200  return iter->oldCon = inst->newMeasurementConnect(
201  group, std::bind(&handleOldMeasure, this, _1, _2), at
202  );
203  }
210  bool disconnectOldMeasurement(const InstrumentSptr &inst) {
211  // look for the instrument
212  ConnectionMapIterator iter = conns.find(inst.get());
213  if (iter != conns.end()) {
214  // disconnect it
215  iter->oldCon.disconnect();
216  // are both signals disconnected?
217  if (!iter->newCon.connected()) {
218  // remove the record for this instrument
219  conns.erase(iter);
220  return true;
221  }
222  }
223  return false;
224  }
232  bool disconnectAll(const InstrumentSptr &inst) {
233  // look for the instrument
234  ConnectionMapIterator iter = conns.find(inst.get());
235  if (iter != conns.end()) {
236  // is either signal connected?
237  if (iter->newCon.connected() || iter->oldCon.connected()) {
238  // disconnect
239  iter->newCon.disconnect();
240  iter->oldCon.disconnect();
241  // remove the record for this instrument
242  conns.erase(iter);
243  return true;
244  }
245  } return false;
246  }
250  void disconnectAll() {
251  ConnectionMapIterator iter;
252  for (iter = conns.begin(); iter != conns.end(); iter = conns.erase(iter)) {
253  iter->newCon.disconnect();
254  iter->oldCon.disconnect();
255  }
256  }
257 };
258 
259 } }
virtual void handleOldMeasure(const std::shared_ptr< Instrument > &i, const std::shared_ptr< const Measurement > &m)=0
Handles an incoming old measurement signal.
void disconnectAll()
Disconnects the group from all signals.
bool disconnectOldMeasurement(const InstrumentSptr &inst)
Disconnects from the old measurement signal of the given instrument.
bool disconnectAll(const InstrumentSptr &inst)
Disconnects the group from both the new and old measurement signals from the given instrument...
duds::data::GenericMeasurement< SVT, SQT, TVT, TQT > Measurement
std::map< Instrument *, MeasurementConnections > ConnectionMap
Reduce typing and limit line length; no other reason.
A base class for receiving measurement signals from multiple Instruments.
bool disconnectNewMeasurement(const InstrumentSptr &inst)
Disconnects from the new measurement signal of the given instrument.
boost::signals2::connection oldMeasurementSource(const typename MesurementSignal::group_type &group, const Instrument &inst, boost::signals2::connect_position at=boost::signals2::at_back)
ConnectionMap conns
Stores connections keyed by Instrument pointer.
virtual ~GenericMeasurementSignalSink()
Disconnects from all signals.
void purgeDisconnections()
Removes any disconnected connection objects held by this object.
virtual void handleNewMeasure(const std::shared_ptr< Instrument > &i, const std::shared_ptr< const Measurement > &m)=0
Handles an incoming new measurement signal.
boost::signals2::signal< void(const std::shared_ptr< Instrument > &, const std::shared_ptr< const Measurement > &) > MesurementSignal
The type used for event listeners that are told of measurments.
boost::signals2::connection oldMeasurementSource(const InstrumentSptr &inst, boost::signals2::connect_position at=boost::signals2::at_back)
boost::signals2::connection newMeasurementSource(const typename MesurementSignal::group_type &group, const Instrument &inst, boost::signals2::connect_position at=boost::signals2::at_back)
Stores a sample of something along with a timestamp stored as a sample from a clock.
Definition: Measurement.hpp:24
GenericInstrument< SVT, SQT, TVT, TQT > Instrument
boost::signals2::connection newMeasurementSource(const InstrumentSptr &inst, boost::signals2::connect_position at=boost::signals2::at_back)
Connect this object to the new measurement signal of the given instrument, or return the existing con...