2
// Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 3 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "as_value.h" // for struct variable composition
24
#include "as_object.h" // for inheritance
25
#include "as_function.h" // for visibility of destructor by intrusive_ptr
26
#include "smart_ptr.h" // GNASH_USE_GC
32
// Forward declarations
40
/// An interval timer.
42
/// This is constructed when _global.setInterval() is called.
43
/// Instances of this class will be stored in the movie_root singleton.
45
/// A timer has a function to call, a context in which to call it, a
46
/// list of arguments and an interval specifying how often the function must be
49
/// It is *not* a "smart" timer, which is
50
/// it will *not* automatically execute at given intervals. Rather, it
51
/// will be movie_root responsibility to execute the timer-associated
52
/// function at regular intervals. As a facility, the Timer class provides
53
/// an execution operator, proxying the execution to the associated function
54
/// with properly set-up context.
62
/// Construct a disabled (cleared) timer.
67
/// Setup the Timer, enabling it.
70
/// The function to call from execution operator.
71
/// Will be stored in an intrusive_ptr.
74
/// The number of milliseconds between expires.
77
/// The object to be used as 'this' pointer when calling the
78
/// associated function. Will be stored in an intrusive_ptr.
79
/// It is allowed to be NULL as long as fn_call is allowed
80
/// a NULL as 'this_ptr' (we might want to change this).
83
/// If true the interval will run only once. False if omitted.
85
void setInterval(as_function& method, unsigned long ms, boost::intrusive_ptr<as_object> this_ptr, bool runOnce=false);
87
/// Setup the Timer, enabling it.
90
/// The function to call from execution operator.
91
/// Will be stored in an intrusive_ptr.
94
/// The number of milliseconds between expires.
97
/// The object to be used as 'this' pointer when calling the
98
/// associated function. Will be stored in an intrusive_ptr.
99
/// It is allowed to be NULL as long as fn_call is allowed
100
/// a NULL as 'this_ptr' (we might want to change this).
103
/// The list of arguments to pass to the function being invoked.
106
/// If true the interval will run only once. False if omitted.
108
void setInterval(as_function& method, unsigned long ms, boost::intrusive_ptr<as_object> this_ptr,
109
std::vector<as_value>& args, bool runOnce=false);
111
/// Setup the Timer to call a late-evaluated object method, enabling it.
114
/// The object to be used as 'this' pointer when calling the
115
/// associated function. Will be stored in an intrusive_ptr.
116
/// It is allowed to be NULL as long as fn_call is allowed
117
/// a NULL as 'this_ptr' (we might want to change this).
119
/// @param methodName
120
/// The method name to call from execution operator.
123
/// The number of milliseconds between expires.
126
/// The list of arguments to pass to the function being invoked.
129
/// If true the interval will run only once. False if omitted.
131
void setInterval(boost::intrusive_ptr<as_object> obj, const std::string& methodName, unsigned long ms,
132
std::vector<as_value>& args, bool runOnce=false);
134
/// Clear the timer, ready for reuse
136
/// When a Timer is cleared, the expired() function
137
/// will always return false.
139
/// Use setInterval() to reset it.
141
void clearInterval();
143
/// Get expiration state
146
/// Current time, in milliseconds.
149
/// Output parameter, will be set to the amount of milliseconds
150
/// elapsed since actual expiration, if expired.
152
/// @return true if the timer expired, false otherwise.
154
bool expired(unsigned long now, unsigned long& elapsed);
156
/// Return true if interval has been cleared.
158
/// Note that the timer is constructed as cleared and you
159
/// need to call setInterval() to make it not-cleared.
162
return _start == std::numeric_limits<unsigned long>::max();
165
/// Execute associated function and reset state
167
/// After execution either the timer is cleared
168
/// (if runOnce) or start time is incremented
171
/// NOTE: if the timer is cleared this call
172
/// results in a no-op.
174
void executeAndReset();
176
/// Arguments list type
177
typedef std::vector<as_value> ArgsContainer;
180
/// Mark all reachable resources (for GC)
182
/// Resources reachable from Timer are:
184
/// - Arguments list (_args)
185
/// - Associated function (_function)
186
/// - Target object (_object)
188
void markReachableResources() const;
189
#endif // GNASH_USE_GC
193
/// Execute associated function properly setting up context
196
/// Execute associated function properly setting up context
197
void operator() () { execute(); }
199
/// Return number of milliseconds between expirations
200
unsigned long getInterval() const { return _interval; }
202
/// Return number of milliseconds after VM start this timer was last reset
203
unsigned long getStart() const { return _start; }
208
/// Called by every function setting the interval.
212
/// Number of milliseconds between expirations
213
unsigned int _interval;
215
/// Number of milliseconds since epoch at Timer start
217
/// This will be numeric_limits<unsigned long>::max()
218
/// if the timer is not active (or cleared)
220
unsigned long _start;
222
/// The associated function (if statically-bound) stored in an intrusive pointer
223
boost::intrusive_ptr<as_function> _function;
225
/// The associated method name, stored in an intrusive pointer
226
std::string _methodName;
228
/// Context for the function call. Will be used as 'this' pointer.
229
boost::intrusive_ptr<as_object> _object;
231
/// List of arguments
234
/// True if the timer should execute only once (for setTimeout)
238
as_value timer_setinterval(const fn_call& fn);
239
as_value timer_settimeout(const fn_call& fn);
240
as_value timer_clearinterval(const fn_call& fn);
242
} // end of namespace gnash