big merge from master, fix rpm creation, drop fetching swfdec
[gnash.git] / libcore / VirtualClock.h
blob7effd10f079e1888b4182f64dc2da4626babadc3
1 // VirtualClock.h -- virtual clock for gnash core lib
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 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
20 #ifndef GNASH_VIRTUAL_CLOCK_H
21 #define GNASH_VIRTUAL_CLOCK_H
23 #include <cassert> // for InterruptableVirtualClock
25 namespace gnash
28 /// A class used to virtualize time flow
30 /// This class will be used to fetch current time
31 /// everytime it is needed by the core lib.
32 ///
33 class VirtualClock
35 public:
37 /// Return number of milliseconds elapsed since start.
39 /// Subclass this to provide time to the core lib.
41 /// NOTE:
42 /// 32bit unsigned int has an upper limit of 4294967295
43 /// which means about 49 days before overlflow.
44 ///
45 virtual unsigned long int elapsed() const=0;
47 /// Restart the clock
48 virtual void restart()=0;
50 virtual ~VirtualClock() {}
53 /// A VirtualClock wrapper adding pause/resume capabilities
54 class InterruptableVirtualClock : public VirtualClock
57 public:
59 /// Construct an InterruptableVirtualClock from a VirtualClock source
61 /// The interruptable virtual clock starts in 'stop' mode,
62 /// use resume() to start.
63 ///
64 /// @param src
65 /// A VirtualClock to use as source, ownership is retained by caller
66 /// which should guarantee to keep the source alive for the whole
67 /// lifetime of this instance.
68 ///
69 InterruptableVirtualClock(VirtualClock& src)
71 _src(src),
72 _elapsed(0),
73 _offset(_src.elapsed()),
74 _paused(true)
78 /// Return elapsed time, taking interruptions in consideration
79 unsigned long int elapsed() const
81 if ( ! _paused ) // query source if not stopped
82 _elapsed = _src.elapsed()-_offset;
83 return _elapsed;
86 void restart()
88 _elapsed = 0;
89 _offset = _src.elapsed();
92 void pause()
94 if ( _paused ) return; // nothing to do
95 _paused = true;
98 void resume()
100 if ( ! _paused ) return; // nothing to do
101 _paused = false;
103 unsigned long now = _src.elapsed();
104 _offset = ( now - _elapsed );
105 assert( now-_offset == _elapsed ); // check if we did the right thing
108 private:
110 VirtualClock& _src;
112 mutable unsigned long int _elapsed;
114 unsigned long int _offset;
116 bool _paused;
120 } // namespace gnash
122 #endif // GNASH_VIRTUAL_CLOCK_H