SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CSystemClock.hpp
1 /* This file is part of sUtil, a random collection of utilities.
2 
3 sUtil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sUtil is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sUtil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CSystemClock.hpp
23  *
24  * Created on: Jul 17, 2011
25  *
26  * Copyright (C) 2011, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CSYSTEMCLOCK_HPP_
30 #define CSYSTEMCLOCK_HPP_
31 
32 #include <sutil/CSingleton.hpp>
33 
34 #include <unistd.h>
35 #include <sys/time.h>
36 
37 namespace sutil
38 {
40  typedef double suClock;
41 
46  {
47  public:
50  {
51  gettimeofday(&t_start_,NULL);
52  sim_time_ = 0.0;
53  }
54 
56  friend class CSystemClock;
57 
58  private:
60  suClock sim_time_;
61 
63  timeval t_start_;
64 
66  SSystemClock(const SSystemClock&);
67 
70  };
71 
81  class CSystemClock : private CSingleton<SSystemClock>
82  {
83  public:
86  static bool start()
87  {
89  { return true; }
90  else
91  { return false; }
92  }
95  static suClock getSysTime()
96  { return computeTDiff(CSingleton<SSystemClock>::getData()->t_start_); }
97 
100  static suClock getSimTime()
101  { return CSingleton<SSystemClock>::getData()->sim_time_; }
102 
103  static void tick(suClock arg_dt)
104  { CSingleton<SSystemClock>::getData()->sim_time_+=arg_dt; }
105 
109  static void progressToTime(const timeval &arg_t_start,
110  const suClock arg_time_increment=1.0,
111  const bool arg_increment_sim_clock=true)
112  {//Sleeps up to when n seconds have elapsed from the passed timeval
113  suClock time_diff;
114  //How much time has passed since the timestamp
115  time_diff = computeTDiff(arg_t_start);
116 
117  if(time_diff<arg_time_increment)//Sleep for remaining time
118  { sleep(arg_time_increment-time_diff); }
119 
120  if(arg_increment_sim_clock)//Increment the simulation time
121  { CSingleton<SSystemClock>::getData()->sim_time_+=arg_time_increment; }
122  }
123 
124  private:
126  static suClock computeTDiff(const timeval &arg_t_start)
127  {
128  //To compute the elapsed time since the clock was initialized.
129  timeval t_tick,t_diff,t_start(arg_t_start);
130 
131  //Get the current system time
132  gettimeofday(&t_tick,NULL);
133  if (t_tick.tv_usec < t_start.tv_usec)
134  {
135  long int nsec = (t_start.tv_usec - t_tick.tv_usec) / 1000000 + 1;
136  t_start.tv_usec -= 1000000 * nsec;
137  t_start.tv_sec += nsec;
138  }
139  if (t_tick.tv_usec - t_start.tv_usec > 1000000)
140  {
141  long int nsec = (t_tick.tv_usec - t_start.tv_usec) / 1000000;
142  t_start.tv_usec += 1000000 * nsec;
143  t_start.tv_sec -= nsec;
144  }
145 
146  // Compute the remaining time. tv_usec is positive.
147  t_diff.tv_sec = t_tick.tv_sec - t_start.tv_sec;
148  t_diff.tv_usec = t_tick.tv_usec - t_start.tv_usec;
149 
150  return static_cast<suClock>(t_diff.tv_sec)+(static_cast<suClock>(t_diff.tv_usec)/1000000.00);
151  }
152 
154  CSystemClock();
155 
157  CSystemClock(const CSystemClock&);
158 
161  };
162 }
163 
164 #endif /*CSYSTEMCLOCK_HPP_*/
static suClock computeTDiff(const timeval &arg_t_start)
Definition: CSystemClock.hpp:126
Definition: CSystemClock.hpp:45
static void progressToTime(const timeval &arg_t_start, const suClock arg_time_increment=1.0, const bool arg_increment_sim_clock=true)
Definition: CSystemClock.hpp:109
static suClock getSimTime()
Definition: CSystemClock.hpp:100
static bool start()
Definition: CSystemClock.hpp:86
Definition: CSingleton.hpp:47
timeval t_start_
Definition: CSystemClock.hpp:63
SSystemClock & operator=(const SSystemClock &)
CSystemClock & operator=(const CSystemClock &)
static SDataStruct * getData()
Definition: CSingleton.hpp:87
SSystemClock()
Definition: CSystemClock.hpp:49
suClock sim_time_
Definition: CSystemClock.hpp:60
static suClock getSysTime()
Definition: CSystemClock.hpp:95
Definition: CSystemClock.hpp:81