DUDS
Distributed Update of Data from Something
Device.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  */
11 #include <atomic>
12 #include <thread>
13 
14 namespace duds { namespace hardware { namespace devices {
15 
16 namespace clocks {
17  template<class SVT, class SQT, class TVT, class TQT>
18  class GenericClock;
19 }
20 
33 template<class SVT, class SQT, class TVT, class TQT>
34 class GenericDevice : public Something {
39  std::atomic<unsigned int> destructWait = 0;
46  std::atomic<unsigned int> &count;
47  public:
48  DestructCounter(std::atomic<unsigned int> &c) : count(c) {
49  ++count;
50  }
52  --count;
53  }
54  };
55 public:
63  typedef std::shared_ptr<Sensor> SensorSptr;
67  typedef std::shared_ptr<const Sensor> ConstSensorSptr;
71  typedef std::weak_ptr<Sensor> SensorWptr;
79  typedef std::shared_ptr<Measurement> MeasurementSptr;
83  typedef std::shared_ptr<const Measurement> ConstMeasurementSptr;
92  typedef std::shared_ptr<Clock> ClockSptr;
93 protected:
99  std::vector<SensorSptr> sens;
105  void setMeasurement(const ConstMeasurementSptr &store, int sIdx = 0) const {
106  sens[sIdx]->meas = store;
107  }
113  void setMeasurement(ConstMeasurementSptr &&store, int sIdx = 0) const {
114  sens[sIdx]->meas = std::move(store);
115  }
120  GenericDevice() = default;
126  GenericDevice(const boost::uuids::uuid &id) : Something(id) { }
127 public:
134  // prevent sensors from getting their Device
135  for (auto s : sens) {
136  s->dev = nullptr;
137  }
138  // wait for any stragglers
139  while (destructWait > 0) {
140  std::this_thread::yield();
141  }
142  }
147  std::shared_ptr< GenericDevice< SVT, SQT, TVT, TQT > > sharedPtr() {
148  // use this counter object to ensure this function returns before
149  // this object's destructor concludes
150  DestructCounter(destructWait);
151  // should be empty if the destructor is running on another thread
152  return std::static_pointer_cast< GenericDevice< SVT, SQT, TVT, TQT > >(
153  Something::weak_from_this().lock()
154  );
155  }
162  virtual void sample() = 0;
169  virtual void sample(const ClockSptr &clock) = 0;
173  unsigned int numSensors() const {
174  return sens.size();
175  }
182  const SensorSptr &sensor(unsigned int sIdx = 0) const {
184  return sens.at(sIdx);
185  }
186  //Unit sensorUnits(int sIdx) const;
193  ConstMeasurementSptr currentMeasurement(unsigned int sIdx = 0) const {
195  return sens.at(sIdx)->measurement();
196  }
197 };
198 
199 typedef GenericDevice<
201  double,
203  float
205 
206 typedef std::shared_ptr<Device> DeviceSptr;
207 typedef std::weak_ptr<Device> DeviceWptr;
208 
209 
210 } } }
ConstMeasurementSptr currentMeasurement(unsigned int sIdx=0) const
Returns the measurement object for the sensor at the given device specific index. ...
Definition: Device.hpp:193
boost::variant< std::string, duds::general::LanguageTaggedString, std::array< std::int32_t, 4 >, std::array< std::int64_t, 2 >, duds::data::int128_w, std::array< float, 4 >, std::array< double, 2 >, double, duds::data::Quantity, boost::recursive_wrapper< duds::data::QuantityNddArray >, duds::time::interstellar::Femtoseconds, duds::time::interstellar::Nanoseconds, duds::time::interstellar::FemtoTime, duds::time::interstellar::NanoTime, boost::uuids::uuid, boost::recursive_wrapper< duds::data::GenericValueTable >, std::shared_ptr< std::vector< char > >> GenericValue
A general value of a type can be serialized for transmission over a network and can be used with a re...
~GenericDevice()
Modifies member Sensor objects so they no longer have a pointer back to this object, and waits for calls those objects may be making to sharedPtr() to complete.
Definition: Device.hpp:133
GenericDevice(const boost::uuids::uuid &id)
Derived classes should create Sensor objects and place them in sens either in the constructor or an i...
Definition: Device.hpp:126
std::shared_ptr< GenericDevice< SVT, SQT, TVT, TQT > > sharedPtr()
Returns a shared pointer to this device object; will be empty if this object&#39;s destructor is running...
Definition: Device.hpp:147
GenericSensor< SVT, SQT, TVT, TQT > Sensor
The sensor type used by this device.
Definition: Device.hpp:59
std::weak_ptr< Device > DeviceWptr
Definition: Device.hpp:207
clocks::GenericClock< SVT, SQT, TVT, TQT > Clock
The clock driver type used by the instruments of this device for time stamps.
Definition: Device.hpp:88
Something specific; a base class for identifying things.
Definition: Something.hpp:26
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
std::vector< SensorSptr > sens
Contains Sensor objects to represent all the kinds of data this device can collect.
Definition: Device.hpp:99
void setMeasurement(ConstMeasurementSptr &&store, int sIdx=0) const
Updates the current measurement of a member sensor.
Definition: Device.hpp:113
duds::data::GenericMeasurement< SVT, SQT, TVT, TQT > Measurement
The measurement type provided by the instruments of this device.
Definition: Device.hpp:75
DestructCounter(std::atomic< unsigned int > &c)
Definition: Device.hpp:48
Represents a sensor on a specific Device; allows access to measurements without needing to use a Devi...
Definition: Sensor.hpp:39
Used to assure the destructor function does not return before Sensor objects quit using member functi...
Definition: Device.hpp:45
Represents something with one or more sensors that are sampled through the same hardware.
Definition: Device.hpp:34
std::shared_ptr< Clock > ClockSptr
A shared pointer type to the clock type used by this device.
Definition: Device.hpp:92
const SensorSptr & sensor(unsigned int sIdx=0) const
Returns the sensor object at the given device specific index.
Definition: Device.hpp:182
std::shared_ptr< Sensor > SensorSptr
A shared pointer type to the sensor type used by this device.
Definition: Device.hpp:63
GenericDevice< duds::data::GenericValue, double, duds::time::interstellar::NanoTime, float > Device
Definition: Device.hpp:204
std::weak_ptr< Sensor > SensorWptr
A weak pointer type to the sensor type used by this device.
Definition: Device.hpp:71
std::shared_ptr< Measurement > MeasurementSptr
A shared pointer type to the measurement type used by this device.
Definition: Device.hpp:79
The foundation to a clock driver.
Definition: Clock.hpp:78
Stores a sample of something along with a timestamp stored as a sample from a clock.
Definition: Measurement.hpp:24
std::shared_ptr< Device > DeviceSptr
Definition: Device.hpp:206
unsigned int numSensors() const
Returns the number of sensors provided by this device.
Definition: Device.hpp:173
std::shared_ptr< const Measurement > ConstMeasurementSptr
A shared pointer type to the const measurement type used by this device.
Definition: Device.hpp:83
NanoClock::time_point NanoTime
A point in time in Interstellar Time stored in Nanoseconds.
void setMeasurement(const ConstMeasurementSptr &store, int sIdx=0) const
Updates the current measurement of a member sensor.
Definition: Device.hpp:105
std::shared_ptr< const Sensor > ConstSensorSptr
A shared pointer type to the const sensor type used by this device.
Definition: Device.hpp:67