Piotr Krysik | df97869 | 2017-09-27 21:58:24 +0200 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2010-2012 Ettus Research LLC |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | // |
| 17 | |
| 18 | #ifndef INCLUDED_TYPES_TIME_SPEC_HPP |
| 19 | #define INCLUDED_TYPES_TIME_SPEC_HPP |
| 20 | |
Piotr Krysik | d32ba5b | 2017-11-03 09:58:13 +0100 | [diff] [blame] | 21 | #include <grgsm/api.h> |
Piotr Krysik | df97869 | 2017-09-27 21:58:24 +0200 | [diff] [blame] | 22 | #include <boost/operators.hpp> |
| 23 | #include <ctime> |
| 24 | |
| 25 | namespace gr { |
| 26 | namespace gsm { |
| 27 | |
| 28 | /*! |
| 29 | * A time_spec_t holds a seconds and a fractional seconds time value. |
| 30 | * Depending upon usage, the time_spec_t can represent absolute times, |
| 31 | * relative times, or time differences (between absolute times). |
| 32 | * |
| 33 | * The time_spec_t provides clock-domain independent time storage, |
| 34 | * but can convert fractional seconds to/from clock-domain specific units. |
| 35 | * |
| 36 | * The fractional seconds are stored as double precision floating point. |
| 37 | * This gives the fractional seconds enough precision to unambiguously |
| 38 | * specify a clock-tick/sample-count up to rates of several petahertz. |
| 39 | */ |
Piotr Krysik | d32ba5b | 2017-11-03 09:58:13 +0100 | [diff] [blame] | 40 | class GRGSM_API time_spec_t : boost::additive<time_spec_t>, boost::totally_ordered<time_spec_t>{ |
Piotr Krysik | df97869 | 2017-09-27 21:58:24 +0200 | [diff] [blame] | 41 | public: |
| 42 | |
| 43 | /*! |
Piotr Krysik | df97869 | 2017-09-27 21:58:24 +0200 | [diff] [blame] | 44 | * Copy constructor |
| 45 | */ |
| 46 | time_spec_t(const time_spec_t & spec); |
| 47 | |
| 48 | /*! |
| 49 | * Create a time_spec_t from a real-valued seconds count. |
| 50 | * \param secs the real-valued seconds count (default = 0) |
| 51 | */ |
| 52 | time_spec_t(double secs = 0); |
| 53 | |
| 54 | /*! |
| 55 | * Create a time_spec_t from whole and fractional seconds. |
| 56 | * \param full_secs the whole/integer seconds count |
| 57 | * \param frac_secs the fractional seconds count (default = 0) |
| 58 | */ |
| 59 | time_spec_t(time_t full_secs, double frac_secs = 0); |
| 60 | |
| 61 | /*! |
| 62 | * Create a time_spec_t from whole seconds and fractional ticks. |
| 63 | * Translation from clock-domain specific units. |
| 64 | * \param full_secs the whole/integer seconds count |
| 65 | * \param tick_count the fractional seconds tick count |
| 66 | * \param tick_rate the number of ticks per second |
| 67 | */ |
| 68 | time_spec_t(time_t full_secs, long tick_count, double tick_rate); |
| 69 | |
| 70 | /*! |
| 71 | * Create a time_spec_t from a 64-bit tick count. |
| 72 | * Translation from clock-domain specific units. |
| 73 | * \param ticks an integer count of ticks |
| 74 | * \param tick_rate the number of ticks per second |
| 75 | */ |
| 76 | static time_spec_t from_ticks(long long ticks, double tick_rate); |
| 77 | |
| 78 | /*! |
| 79 | * Convert the fractional seconds to clock ticks. |
| 80 | * Translation into clock-domain specific units. |
| 81 | * \param tick_rate the number of ticks per second |
| 82 | * \return the fractional seconds tick count |
| 83 | */ |
| 84 | long get_tick_count(double tick_rate) const; |
| 85 | |
| 86 | /*! |
| 87 | * Convert the time spec into a 64-bit tick count. |
| 88 | * Translation into clock-domain specific units. |
| 89 | * \param tick_rate the number of ticks per second |
| 90 | * \return an integer number of ticks |
| 91 | */ |
| 92 | long long to_ticks(const double tick_rate) const; |
| 93 | |
| 94 | /*! |
| 95 | * Get the time as a real-valued seconds count. |
| 96 | * Note: If this time_spec_t represents an absolute time, |
| 97 | * the precision of the fractional seconds may be lost. |
| 98 | * \return the real-valued seconds |
| 99 | */ |
| 100 | double get_real_secs(void) const; |
| 101 | |
| 102 | /*! |
| 103 | * Get the whole/integer part of the time in seconds. |
| 104 | * \return the whole/integer seconds |
| 105 | */ |
| 106 | time_t get_full_secs(void) const; |
| 107 | |
| 108 | /*! |
| 109 | * Get the fractional part of the time in seconds. |
| 110 | * \return the fractional seconds |
| 111 | */ |
| 112 | double get_frac_secs(void) const; |
| 113 | |
| 114 | //! Implement addable interface |
| 115 | time_spec_t &operator+=(const time_spec_t &); |
| 116 | |
| 117 | //! Implement subtractable interface |
| 118 | time_spec_t &operator-=(const time_spec_t &); |
| 119 | |
| 120 | //private time storage details |
| 121 | private: time_t _full_secs; double _frac_secs; |
| 122 | }; |
| 123 | |
| 124 | //! Implement equality_comparable interface |
| 125 | bool operator==(const time_spec_t &, const time_spec_t &); |
| 126 | |
| 127 | //! Implement less_than_comparable interface |
| 128 | bool operator<(const time_spec_t &, const time_spec_t &); |
| 129 | |
| 130 | inline time_t time_spec_t::get_full_secs(void) const{ |
| 131 | return this->_full_secs; |
| 132 | } |
| 133 | |
| 134 | inline double time_spec_t::get_frac_secs(void) const{ |
| 135 | return this->_frac_secs; |
| 136 | } |
| 137 | |
| 138 | } //namespace transceiver |
| 139 | } //namespace gr |
| 140 | |
| 141 | #endif /* INCLUDED_TYPES_TIME_SPEC_HPP */ |