Gnash  0.8.11dev
VirtualClock.h
Go to the documentation of this file.
1 // VirtualClock.h -- virtual clock for gnash core lib
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
20 #ifndef GNASH_VIRTUAL_CLOCK_H
21 #define GNASH_VIRTUAL_CLOCK_H
22 
23 #include <cassert> // for InterruptableVirtualClock
24 
25 namespace gnash
26 {
27 
29 //
34 {
35 public:
36 
38  //
40  //
45  virtual unsigned long int elapsed() const=0;
46 
48  virtual void restart()=0;
49 
50  virtual ~VirtualClock() {}
51 };
52 
55 {
56 
57 public:
58 
60  //
70  :
71  _src(src),
72  _elapsed(0),
73  _offset(_src.elapsed()),
74  _paused(true)
75  {
76  }
77 
79  unsigned long int elapsed() const
80  {
81  if ( ! _paused ) // query source if not stopped
82  _elapsed = _src.elapsed()-_offset;
83  return _elapsed;
84  }
85 
86  void restart()
87  {
88  _elapsed = 0;
89  _offset = _src.elapsed();
90  }
91 
92  void pause()
93  {
94  if ( _paused ) return; // nothing to do
95  _paused = true;
96  }
97 
98  void resume()
99  {
100  if ( ! _paused ) return; // nothing to do
101  _paused = false;
102 
103  unsigned long now = _src.elapsed();
104  _offset = ( now - _elapsed );
105  assert( now-_offset == _elapsed ); // check if we did the right thing
106  }
107 
108 private:
109 
110  VirtualClock& _src;
111 
112  mutable unsigned long int _elapsed;
113 
114  unsigned long int _offset;
115 
116  bool _paused;
117 };
118 
119 
120 } // namespace gnash
121 
122 #endif // GNASH_VIRTUAL_CLOCK_H
123 
void resume()
Definition: VirtualClock.h:98
A class used to virtualize time flow.
Definition: VirtualClock.h:33
unsigned long int elapsed() const
Return elapsed time, taking interruptions in consideration.
Definition: VirtualClock.h:79
void restart()
Restart the clock.
Definition: VirtualClock.h:86
virtual ~VirtualClock()
Definition: VirtualClock.h:50
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
InterruptableVirtualClock(VirtualClock &src)
Construct an InterruptableVirtualClock from a VirtualClock source.
Definition: VirtualClock.h:69
A VirtualClock wrapper adding pause/resume capabilities.
Definition: VirtualClock.h:54
void pause()
Definition: VirtualClock.h:92
virtual unsigned long int elapsed() const =0
Return number of milliseconds elapsed since start.
virtual void restart()=0
Restart the clock.