DUDS
Distributed Update of Data from Something
Clock.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 #ifndef CLOCK_HPP
11 #define CLOCK_HPP
12 
15 #include <boost/uuid/name_generator.hpp>
16 
17 namespace duds { namespace hardware { namespace devices {
18 
24 namespace clocks {
25 
26 // ___________________________________________________________________________
27 // Are these items needed?
28 
37 
48 
53 
54 // ___________________________________________________________________________
55 
56 
64 struct ClockError : DeviceError { };
65 
77 template<class SVT, class SQT, class TVT, class TQT>
78 class GenericClock :
79 public duds::hardware::devices::GenericDevice<SVT, SQT, TVT, TQT> {
80 public:
81  // copied from base class; cannot use from derived classes without scope
84  typedef std::shared_ptr<GenericClock < SVT, SQT, TVT, TQT > > ClockSptr;
85 protected:
96  template<class Ratio, class Src, class Dest>
97  static void convert(Dest &dest, const Src &src) {
98  dest = (Dest)src * ((Dest)(Ratio::num) / (Dest)(Ratio::den));
99  }
110  template<class Ratio, class IST, class Src>
111  static void convertIST(IST &dest, const Src &src) {
112  // src * Ratio / IST::period
113  typedef std::ratio_divide<Ratio, typename IST::period> r;
114  dest = IST(typename IST::duration((src * r::num) / r::den));
115  }
116  #if !defined(HAVE_INT128) | defined(DOXYGEN)
117 
129  template<class Ratio, class IST>
130  static void convertIST(IST &dest, const duds::data::int128_t &src) {
131  // src * Ratio / IST::period
132  typedef std::ratio_divide<Ratio, typename IST::period> r;
133  dest = IST(typename IST::duration(
134  ((src * r::num) / r::den).template convert_to<typename IST::rep>()
135  ));
136  }
137  #endif
138 
145  template<class Ratio, class Src>
146  static void convert(
148  const Src &src)
149  {
150  convertIST<Ratio, duds::time::interstellar::FemtoTime>(dest, src);
151  }
159  template<class Ratio, class Src>
160  static void convert(duds::time::interstellar::NanoTime &dest, const Src &src) {
161  convertIST<Ratio, duds::time::interstellar::NanoTime>(dest, src);
162  }
171  template<class Ratio, class Src>
172  static void convert(duds::data::GenericValue &dest, const Src &src) {
173  // same as Femtoseconds
174  typedef std::ratio_divide<Ratio, std::femto> r;
175  duds::time::interstellar::Femtoseconds::rep bigsrc = src;
177  (src * r::num) / r::den
178  );
179  }
180  GenericClock() = delete;
185  GenericClock(const boost::uuids::uuid &id) :
186  duds::hardware::devices::GenericDevice<SVT, SQT, TVT, TQT>(id) {
187  boost::uuids::name_generator_latest g(id);
189  sens.push_back(Sensor::make(this, g("clock"), 0));
190  }
191 public:
192  //using Something::sharedPtr; // doesn't help; no effect
193  operator ClockSptr () const {
194  return Something::sharedPtr< GenericClock< SVT, SQT, TVT, TQT > >();
195  }
201  virtual void sampleTime(typename Measurement::TimeSample &time) = 0;
216  virtual bool unambiguous() const noexcept = 0;
223  typename Measurement::TimeSample ts;
224  sampleTime(ts);
225  return ts;
226  }
227  #ifdef DOXYGEN
228  // Additional documentation for clock driver requirements. The functions
229  // are defined in GenericDevice.
235  virtual void sample() = 0;
249  virtual void sample(const ClockSptr &clock) = 0;
250  #endif
251 };
252 
256 typedef GenericClock<
258  double,
260  float
262 
263 typedef std::shared_ptr<Clock> ClockSptr;
264 
265 } } } }
266 
267 #endif // #ifndef CLOCK_HPP
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...
static void convert(duds::time::interstellar::NanoTime &dest, const Src &src)
Template specialization to convert time in one format to NanoTime.
Definition: Clock.hpp:160
boost::multiprecision::int128_t int128_t
The type used for 128-bit integers.
Definition: Int128.hpp:66
static void convert(duds::time::interstellar::FemtoTime &dest, const Src &src)
Template specialization to convert time in one format to FemtoTime.
Definition: Clock.hpp:146
The base type for errors from clocks.
Definition: Clock.hpp:64
A template for a sample from an insturment.
Definition: Sample.hpp:81
GenericClock(const boost::uuids::uuid &id)
Definition: Clock.hpp:185
static std::shared_ptr< GenericSensor< SVT, SQT, TVT, TQT > > make(Device *pdev)
Make a sensor without setting the UUID.
Definition: Sensor.hpp:110
static void convert(Dest &dest, const Src &src)
General template to convert time in one format to another.
Definition: Clock.hpp:97
std::shared_ptr< Clock > ClockSptr
Definition: Clock.hpp:263
Measurement::TimeSample sampleTime()
Samples the time from the clock device without storing the result in the clock&#39;s sensor object...
Definition: Clock.hpp:222
duds::data::GenericSample< duds::time::interstellar::FemtoTime, double > FemtoTimeSample
A time sample fit for applications requiring very long-term time samples, or time samples with high r...
Definition: Clock.hpp:47
Represents something with one or more sensors that are sampled through the same hardware.
Definition: Device.hpp:34
NanoTimeSample TimeSample
The regular time sample is currently NanoTimeSample for practicality.
Definition: Clock.hpp:52
An extention to the C++ std::chrono::time_point template to make time points easier to work with...
static void convertIST(IST &dest, const duds::data::int128_t &src)
More specialized template to convert time stored in a 128-bit integer to one of the types defined in ...
Definition: Clock.hpp:130
std::shared_ptr< GenericClock< SVT, SQT, TVT, TQT > > ClockSptr
Definition: Clock.hpp:84
static void convert(duds::data::GenericValue &dest, const Src &src)
Template specialization to convert time in one format to FemtoTime in a GenericValue.
Definition: Clock.hpp:172
std::chrono::duration< int128_t, std::femto > Femtoseconds
Stores a duration in femtoseconds.
duds::data::GenericSample< duds::time::interstellar::NanoTime, float > NanoTimeSample
A compact time sample that works well for most purposes.
Definition: Clock.hpp:36
The foundation to a clock driver.
Definition: Clock.hpp:78
The base type for errors from devices.
Stores a sample of something along with a timestamp stored as a sample from a clock.
Definition: Measurement.hpp:24
static void convertIST(IST &dest, const Src &src)
Template to convert time in one format to one of the types defined in duds::time::interstellar.
Definition: Clock.hpp:111
duds::data::GenericMeasurement< SVT, SQT, TVT, TQT > Measurement
The measurement type provided by the instruments of this device.
Definition: Clock.hpp:83
GenericClock< duds::data::GenericValue, double, duds::time::interstellar::NanoTime, float > Clock
General use clock driver type.
Definition: Clock.hpp:261
NanoClock::time_point NanoTime
A point in time in Interstellar Time stored in Nanoseconds.