zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Chronometer.hxx
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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  * zenilib 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ZENI_CHRONOMETER_HXX
19 #define ZENI_CHRONOMETER_HXX
20 
21 // HXXed below
22 #include <Zeni/Timer_HQ.h>
23 
24 #include <Zeni/Chronometer.h>
25 #include <cassert>
26 
27 namespace Zeni {
28 
29  template <class TIME>
31  : m_seconds_counted(0.0f),
32  m_running(false),
33  m_scaling_factor(typename TIME::Second_Type(1))
34  {
35  get_chronometers().insert(this);
36  }
37 
38  template <class TIME>
40  get_chronometers().erase(this);
41  get_paused().erase(this);
42  }
43 
44  template <class TIME>
46  : m_seconds_counted(rhs.m_seconds_counted),
47  m_start_time(rhs.m_start_time),
48  m_end_time(rhs.m_end_time),
49  m_running(rhs.m_running),
50  m_scaling_factor(rhs.m_scaling_factor)
51  {
52  get_chronometers().insert(this);
53 
54  if(g_are_paused && m_running)
55  get_paused().insert(this);
56  }
57 
58  template <class TIME>
60  if(g_are_paused) {
61  if(m_running && !rhs.m_running)
62  get_paused().erase(this);
63  else if(rhs.m_running && !m_running)
64  get_paused().insert(this);
65  }
66 
67  m_seconds_counted = rhs.m_seconds_counted;
68  m_start_time = rhs.m_start_time;
69  m_end_time = rhs.m_end_time;
70  m_running = rhs.m_running;
71  m_scaling_factor = rhs.m_scaling_factor;
72  return *this;
73  }
74 
75  template <class TIME>
76  const bool & Chronometer<TIME>::is_running() const {
77  return m_running;
78  }
79 
80  template <class TIME>
82  if(!m_running) {
83  if(g_are_paused) {
84  get_paused().insert(this);
85  }
86  else {
87  m_running = true;
88 
89  m_start_time.update();
90  }
91  }
92 
93  return m_start_time;
94  }
95 
96  template <class TIME>
98  if(m_running) {
99  if(g_are_paused) {
100  get_paused().erase(this);
101  }
102  else {
103  m_end_time.update();
104 
105  m_running = false;
106  m_seconds_counted += m_end_time.get_seconds_since(m_start_time) * m_scaling_factor;
107  }
108  }
109 
110  return m_end_time;
111  }
112 
113  template <class TIME>
114  typename TIME::Second_Type Chronometer<TIME>::seconds() const {
115  return m_seconds_counted + (m_running ?
116  m_start_time.get_seconds_passed() * m_scaling_factor :
117  0.0f);
118  }
119 
120  template <class TIME>
121  void Chronometer<TIME>::set(const typename TIME::Second_Type &time) {
122  if(m_running)
123  m_start_time.update();
124 
125  m_seconds_counted = time;
126  }
127 
128  template <class TIME>
130  m_start_time.update();
131  m_seconds_counted = 0.0f;
132  }
133 
134  template <class TIME>
135  const typename TIME::Second_Type & Chronometer<TIME>::scaling_factor() const {
136  return m_scaling_factor;
137  }
138 
139  template <class TIME>
140  void Chronometer<TIME>::scale(const typename TIME::Second_Type &scaling_factor) {
141  if(m_running) {
142  m_end_time.update();
143  m_seconds_counted += m_end_time.get_seconds_since(m_start_time) * m_scaling_factor;
144  m_start_time = m_end_time;
145  }
146 
147  m_scaling_factor = scaling_factor;
148  }
149 
150  template <class TIME>
152  return g_are_paused;
153  }
154 
155  template <class TIME>
157  std::set<Chronometer<TIME> *> &chronometers = get_chronometers();
158  std::set<Chronometer<TIME> *> &paused = get_paused();
159 
160  for(typename std::set<Chronometer<TIME> *>::iterator it = chronometers.begin();
161  it != chronometers.end();
162  ++it)
163  {
164  if((*it)->is_running()) {
165  (*it)->stop();
166  paused.insert(*it);
167  }
168  }
169 
170  g_are_paused = true;
171  }
172 
173  template <class TIME>
175  g_are_paused = false;
176 
177  std::set<Chronometer<TIME> *> &paused = get_paused();
178 
179  for(typename std::set<Chronometer<TIME> *>::iterator it = paused.begin();
180  it != paused.end();
181  ++it)
182  {
183  (*it)->start();
184  }
185 
186  paused.clear();
187  }
188 
189  template <class TIME>
190  std::set<Chronometer<TIME> *> & Chronometer<TIME>::get_chronometers() {
191  static std::set<Chronometer<TIME> *> * chronometers = new std::set<Chronometer<TIME> *>;
192  return *chronometers;
193  }
194 
195  template <class TIME>
196  std::set<Chronometer<TIME> *> & Chronometer<TIME>::get_paused() {
197  static std::set<Chronometer<TIME> *> * paused = new std::set<Chronometer<TIME> *>;
198  return *paused;
199  }
200 
201  template <class TIME>
202  bool Chronometer<TIME>::g_are_paused = false;
203 
204 }
205 
206 #include <Zeni/Timer_HQ.hxx>
207 
208 #endif
Chronometer & operator=(const Chronometer< TIME > &rhs)
Definition: Chronometer.hxx:59
GLclampf f
Definition: glew.h:3390
void scale(const typename TIME::Second_Type &scaling_factor=typename TIME::Second_Type(1))
Scale the amount of time that passes by some amount.
void set(const typename TIME::Second_Type &time)
Set the number of seconds counted by the Chronometer.
const TIME::Second_Type & scaling_factor() const
Get the scaling factor.
static void pause_all()
Pause all Chronometer&lt;TIME&gt; objects.
const bool & is_running() const
Get whether the Chronometer is currently counting or stopped.
Definition: Chronometer.hxx:76
const TIME & stop()
Stop the Chronometer and get the current TIME.
Definition: Chronometer.hxx:97
Definition: inflate.h:23
A stoppable running timer.
Definition: Chronometer.h:40
static bool are_paused()
Check to see if all Chronometer&lt;TIME&gt; objects are paused.
void reset()
Reset the Chronometer. This does NOT stop the Chronometer.
TIME::Second_Type seconds() const
Get the number of seconds counted by the Chronometer.
const TIME & start()
Start the Chronometer and get the current TIME. This does NOT reset the Chronometer.
Definition: Chronometer.hxx:81
static void unpause_all()
Unpause all Chronometer&lt;TIME&gt; objects.
#define false
Definition: ftrandom.c:50