Gnash  0.8.10
VirtualClock.h
Go to the documentation of this file.
00001 // VirtualClock.h -- virtual clock for gnash core lib
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   Free Software Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020 #ifndef GNASH_VIRTUAL_CLOCK_H
00021 #define GNASH_VIRTUAL_CLOCK_H
00022 
00023 #include <cassert> // for InterruptableVirtualClock
00024 
00025 namespace gnash
00026 {
00027 
00029 //
00033 class VirtualClock
00034 {
00035 public:
00036 
00038     //
00040     //
00045     virtual unsigned long int elapsed() const=0;
00046 
00048     virtual void restart()=0;
00049 
00050     virtual ~VirtualClock() {}
00051 };
00052 
00054 class InterruptableVirtualClock : public VirtualClock
00055 {
00056 
00057 public:
00058 
00060         //
00069         InterruptableVirtualClock(VirtualClock& src)
00070                 :
00071                 _src(src),
00072                 _elapsed(0),
00073                 _offset(_src.elapsed()),
00074                 _paused(true)
00075         {
00076         }
00077 
00079         unsigned long int elapsed() const
00080         {
00081                 if ( ! _paused ) // query source if not stopped
00082                         _elapsed = _src.elapsed()-_offset;
00083                 return _elapsed;
00084         }
00085 
00086         void restart()
00087         {
00088                 _elapsed = 0;
00089                 _offset = _src.elapsed();
00090         }
00091 
00092         void pause()
00093         {
00094                 if ( _paused ) return; // nothing to do
00095                 _paused = true;
00096         }
00097 
00098         void resume()
00099         {
00100                 if ( ! _paused ) return; // nothing to do
00101                 _paused = false;
00102 
00103                 unsigned long now = _src.elapsed();
00104                 _offset = ( now - _elapsed );
00105                 assert( now-_offset == _elapsed ); // check if we did the right thing
00106         }
00107 
00108 private:
00109 
00110         VirtualClock& _src;
00111 
00112         mutable unsigned long int _elapsed;
00113 
00114         unsigned long int _offset;
00115 
00116         bool _paused;
00117 };
00118 
00119 
00120 } // namespace gnash
00121 
00122 #endif // GNASH_VIRTUAL_CLOCK_H
00123