1 // VirtualClock.h -- virtual clock for gnash core lib
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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.
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
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.
37 /// Return number of milliseconds elapsed since start.
39 /// Subclass this to provide time to the core lib.
42 /// 32bit unsigned int has an upper limit of 4294967295
43 /// which means about 49 days before overlflow.
45 virtual unsigned long int elapsed() const=0;
48 virtual void restart()=0;
50 virtual ~VirtualClock() {}
53 /// A VirtualClock wrapper adding pause/resume capabilities
54 class InterruptableVirtualClock
: public VirtualClock
59 /// Construct an InterruptableVirtualClock from a VirtualClock source
61 /// The interruptable virtual clock starts in 'stop' mode,
62 /// use resume() to start.
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.
69 InterruptableVirtualClock(VirtualClock
& src
)
73 _offset(_src
.elapsed()),
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
;
89 _offset
= _src
.elapsed();
94 if ( _paused
) return; // nothing to do
100 if ( ! _paused
) return; // nothing to do
103 unsigned long now
= _src
.elapsed();
104 _offset
= ( now
- _elapsed
);
105 assert( now
-_offset
== _elapsed
); // check if we did the right thing
112 mutable unsigned long int _elapsed
;
114 unsigned long int _offset
;
122 #endif // GNASH_VIRTUAL_CLOCK_H