DeSiGNAR  0.5a
Data Structures General Library
now.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGNOW_H
26 # define DSGNOW_H
27 
28 # include <types.H>
29 
30 namespace Designar
31 {
56  class Now
57  {
58  public:
60  enum class Precision
61  {
62  HOURS,
63  MINUTES,
64  SECONDS,
68  };
69 
70  static int to_int(Precision p)
71  {
72  return static_cast<int>(p);
73  }
74 
75  private:
76  time_point_t tp;
77 
78  Precision precision;
79 
80  static const double precision_values[];
81 
82  public:
85  {
86  return clock_t::now();
87  }
88 
97  static double compute_time_diff(const time_point_t & rtp,
98  const time_point_t & ltp,
99  const Precision & precision)
100  {
101  duration_t et = std::chrono::duration_cast<std::chrono::nanoseconds>
102  (rtp - ltp);
103  return et.count() * precision_values[to_int(precision)];
104  }
105 
118  Now(bool start_now = false)
119  : tp(), precision(Precision::MILLISECONDS)
120  {
121  if (start_now)
122  start();
123  }
124 
137  Now(const Precision & _precision, bool start_now = false)
138  : tp(), precision(_precision)
139  {
140  if (start_now)
141  start();
142  }
143 
148  const Precision & get_precision() const
149  {
150  return precision;
151  }
152 
157  void set_precision(const Precision & _precision)
158  {
159  precision = _precision;
160  }
161 
171  {
172  tp = this->current_time_point();
173  return tp;
174  }
175 
184  double elapsed()
185  {
186  time_point_t ltp = tp;
187  tp = this->current_time_point();
188  return compute_time_diff(tp, ltp, precision);
189  }
190 
195  double delta()
196  {
197  return elapsed();
198  }
199 
211  static double elapsed(const time_point_t & tp,
212  const Precision & precision = Precision::MILLISECONDS)
213  {
214  return compute_time_diff(current_time_point(), tp, precision);
215  }
216 
223  static double delta(const time_point_t & tp,
224  const Precision & precision = Precision::MILLISECONDS)
225  {
226  return elapsed(tp, precision);
227  }
228  };
229 
230 } // End namespace Designar
231 
232 # endif // DSGNOW_H
Now(bool start_now=false)
Builds a new object with default values.
Definition: now.H:118
static double elapsed(const time_point_t &tp, const Precision &precision=Precision::MILLISECONDS)
Definition: now.H:211
clock_t::duration duration_t
Definition: types.H:56
double delta()
Definition: now.H:195
static double compute_time_diff(const time_point_t &rtp, const time_point_t &ltp, const Precision &precision)
Definition: now.H:97
Definition: now.H:56
static int to_int(Precision p)
Definition: now.H:70
Precision
Precision for timing.
Definition: now.H:60
Now(const Precision &_precision, bool start_now=false)
Builds a new object with parametric precision time.
Definition: now.H:137
const Precision & get_precision() const
Definition: now.H:148
double elapsed()
Definition: now.H:184
clock_t::time_point time_point_t
Definition: types.H:55
Definition: array.H:32
static double delta(const time_point_t &tp, const Precision &precision=Precision::MILLISECONDS)
Definition: now.H:223
time_point_t start()
Definition: now.H:170
static time_point_t current_time_point()
Gets the current time point.
Definition: now.H:84
void set_precision(const Precision &_precision)
Definition: now.H:157