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.
41 /// A timer has a function to call, a context in which to call it, a
42 /// list of arguments and an interval specifying how often the function must be
45 /// It is *not* a "smart" timer, which is
46 /// it will *not* automatically execute at given intervals. Rather, it
47 /// will be movie_root responsibility to execute the timer-associated
48 /// function at regular intervals. As a facility, the Timer class provides
49 /// an execution operator, proxying the execution to the associated function
50 /// with properly set-up context.
57 /// Construct a Timer, enabling it.
60 /// The function to call from execution operator.
61 /// Will be stored in an intrusive_ptr.
64 /// The number of milliseconds between expires.
67 /// The object to be used as 'this' pointer when calling the
68 /// associated function. Will be stored in an intrusive_ptr.
69 /// It is allowed to be NULL as long as fn_call is allowed
70 /// a NULL as 'this_ptr' (we might want to change this).
73 /// The list of arguments to pass to the function being invoked.
76 /// If true the interval will run only once. False if omitted.
77 Timer(as_function
& method
, unsigned long ms
, as_object
* this_ptr
,
78 const fn_call::Args
& args
, bool runOnce
= false);
80 /// Construct the Timer to call a late-evaluated object method, enabling it.
83 /// The object to be used as 'this' pointer when calling the
84 /// associated function. Will be stored in an intrusive_ptr.
85 /// It is allowed to be NULL as long as fn_call is allowed
86 /// a NULL as 'this_ptr' (we might want to change this).
89 /// The method name to call from execution operator.
92 /// The number of milliseconds between expires.
95 /// The list of arguments to pass to the function being invoked.
98 /// If true the interval will run only once. False if omitted.
99 Timer(as_object
* obj
, const ObjectURI
& methodName
, unsigned long ms
,
100 const fn_call::Args
& args
, bool runOnce
= false);
102 /// Clear the timer, ready for reuse
104 /// When a Timer is cleared, the expired() function
105 /// will always return false.
107 /// Use setInterval() to reset it.
109 void clearInterval();
111 /// Get expiration state
113 /// Current time, in milliseconds.
116 /// Output parameter, will be set to the amount of milliseconds
117 /// elapsed since actual expiration, if expired.
119 /// @return true if the timer expired, false otherwise.
121 bool expired(unsigned long now
, unsigned long& elapsed
);
123 /// Return true if interval has been cleared.
125 /// Note that the timer is constructed as cleared and you
126 /// need to call setInterval() to make it not-cleared.
127 bool cleared() const {
128 return _start
== std::numeric_limits
<unsigned long>::max();
131 /// Execute associated function and reset state
133 /// After execution either the timer is cleared
134 /// (if runOnce) or start time is incremented
137 /// NOTE: if the timer is cleared this call
138 /// results in a no-op.
140 void executeAndReset();
142 /// Mark all reachable resources (for GC)
144 /// Resources reachable from Timer are:
146 /// - Arguments list (_args)
147 /// - Associated function (_function)
148 /// - Target object (_object)
150 void markReachableResources() const;
154 /// Execute associated function properly setting up context
157 /// Execute associated function properly setting up context
158 void operator() () { execute(); }
160 /// Return number of milliseconds between expirations
161 unsigned long getInterval() const { return _interval
; }
163 /// Return number of milliseconds after VM start this timer was last reset
164 unsigned long getStart() const { return _start
; }
168 /// Called by every function setting the interval.
172 /// Number of milliseconds between expirations
173 unsigned int _interval
;
175 /// Number of milliseconds since epoch at Timer start
177 /// This will be numeric_limits<unsigned long>::max()
178 /// if the timer is not active (or cleared)
180 unsigned long _start
;
182 /// The associated function (if statically-bound) stored in
183 /// an intrusive pointer
184 as_function
* _function
;
186 ObjectURI _methodName
;
188 /// Context for the function call. Will be used as 'this' pointer.
191 /// List of arguments
193 /// This must be copied before passing to a function call.
194 const fn_call::Args _args
;
196 /// True if the timer should execute only once (for setTimeout)