2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "smart_ptr.h"
29 // Forward declarations
37 /// An interval timer.
39 /// This is constructed when _global.setInterval() is called.
40 /// Instances of this class will be stored in the movie_root singleton.
42 /// A timer has a function to call, a context in which to call it, a
43 /// list of arguments and an interval specifying how often the function must be
46 /// It is *not* a "smart" timer, which is
47 /// it will *not* automatically execute at given intervals. Rather, it
48 /// will be movie_root responsibility to execute the timer-associated
49 /// function at regular intervals. As a facility, the Timer class provides
50 /// an execution operator, proxying the execution to the associated function
51 /// with properly set-up context.
58 /// Construct a Timer, enabling it.
61 /// The function to call from execution operator.
62 /// Will be stored in an intrusive_ptr.
65 /// The number of milliseconds between expires.
68 /// The object to be used as 'this' pointer when calling the
69 /// associated function. Will be stored in an intrusive_ptr.
70 /// It is allowed to be NULL as long as fn_call is allowed
71 /// a NULL as 'this_ptr' (we might want to change this).
74 /// The list of arguments to pass to the function being invoked.
77 /// If true the interval will run only once. False if omitted.
78 Timer(as_function
& method
, unsigned long ms
, as_object
* this_ptr
,
79 const fn_call::Args
& args
, bool runOnce
= false);
81 /// Construct the Timer to call a late-evaluated object method, enabling it.
84 /// The object to be used as 'this' pointer when calling the
85 /// associated function. Will be stored in an intrusive_ptr.
86 /// It is allowed to be NULL as long as fn_call is allowed
87 /// a NULL as 'this_ptr' (we might want to change this).
90 /// The method name to call from execution operator.
93 /// The number of milliseconds between expires.
96 /// The list of arguments to pass to the function being invoked.
99 /// If true the interval will run only once. False if omitted.
100 Timer(as_object
* obj
, string_table::key methodName
, unsigned long ms
,
101 const fn_call::Args
& args
, bool runOnce
= false);
103 /// Clear the timer, ready for reuse
105 /// When a Timer is cleared, the expired() function
106 /// will always return false.
108 /// Use setInterval() to reset it.
110 void clearInterval();
112 /// Get expiration state
114 /// Current time, in milliseconds.
117 /// Output parameter, will be set to the amount of milliseconds
118 /// elapsed since actual expiration, if expired.
120 /// @return true if the timer expired, false otherwise.
122 bool expired(unsigned long now
, unsigned long& elapsed
);
124 /// Return true if interval has been cleared.
126 /// Note that the timer is constructed as cleared and you
127 /// need to call setInterval() to make it not-cleared.
128 bool cleared() const {
129 return _start
== std::numeric_limits
<unsigned long>::max();
132 /// Execute associated function and reset state
134 /// After execution either the timer is cleared
135 /// (if runOnce) or start time is incremented
138 /// NOTE: if the timer is cleared this call
139 /// results in a no-op.
141 void executeAndReset();
144 /// Mark all reachable resources (for GC)
146 /// Resources reachable from Timer are:
148 /// - Arguments list (_args)
149 /// - Associated function (_function)
150 /// - Target object (_object)
152 void markReachableResources() const;
153 #endif // GNASH_USE_GC
157 /// Execute associated function properly setting up context
160 /// Execute associated function properly setting up context
161 void operator() () { execute(); }
163 /// Return number of milliseconds between expirations
164 unsigned long getInterval() const { return _interval
; }
166 /// Return number of milliseconds after VM start this timer was last reset
167 unsigned long getStart() const { return _start
; }
171 /// Called by every function setting the interval.
175 /// Number of milliseconds between expirations
176 unsigned int _interval
;
178 /// Number of milliseconds since epoch at Timer start
180 /// This will be numeric_limits<unsigned long>::max()
181 /// if the timer is not active (or cleared)
183 unsigned long _start
;
185 /// The associated function (if statically-bound) stored in
186 /// an intrusive pointer
187 as_function
* _function
;
189 string_table::key _methodName
;
191 /// Context for the function call. Will be used as 'this' pointer.
194 /// List of arguments
196 /// This must be copied before passing to a function call.
197 const fn_call::Args _args
;
199 /// True if the timer should execute only once (for setTimeout)