2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
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
27 // Forward declarations
35 /// An interval timer.
37 /// This is constructed when _global.setInterval() is called.
39 /// A timer has a function to call, a context in which to call it, a
40 /// list of arguments and an interval specifying how often the function must be
43 /// It is *not* a "smart" timer, which is
44 /// it will *not* automatically execute at given intervals. Rather, it
45 /// will be movie_root responsibility to execute the timer-associated
46 /// function at regular intervals. As a facility, the Timer class provides
47 /// an execution operator, proxying the execution to the associated function
48 /// with properly set-up context.
55 /// Construct a Timer, enabling it.
58 /// The function to call from execution operator.
59 /// Will be stored in an intrusive_ptr.
62 /// The number of milliseconds between expires.
65 /// The object to be used as 'this' pointer when calling the
66 /// associated function. Will be stored in an intrusive_ptr.
67 /// It is allowed to be NULL as long as fn_call is allowed
68 /// a NULL as 'this_ptr' (we might want to change this).
71 /// The list of arguments to pass to the function being invoked.
74 /// If true the interval will run only once. False if omitted.
75 Timer(as_function
& method
, unsigned long ms
, as_object
* this_ptr
,
76 fn_call::Args args
, bool runOnce
= false);
78 /// Construct the Timer to call a late-evaluated object method, enabling it.
81 /// The object to be used as 'this' pointer when calling the
82 /// associated function. Will be stored in an intrusive_ptr.
83 /// It is allowed to be NULL as long as fn_call is allowed
84 /// a NULL as 'this_ptr' (we might want to change this).
87 /// The method name to call from execution operator.
90 /// The number of milliseconds between expires.
93 /// The list of arguments to pass to the function being invoked.
96 /// If true the interval will run only once. False if omitted.
97 Timer(as_object
* obj
, ObjectURI methodName
, unsigned long ms
,
98 fn_call::Args args
, bool runOnce
= false);
100 /// Clear the timer, ready for reuse
102 /// When a Timer is cleared, the expired() function
103 /// will always return false.
105 /// Use setInterval() to reset it.
107 void clearInterval();
109 /// Get expiration state
111 /// Current time, in milliseconds.
114 /// Output parameter, will be set to the amount of milliseconds
115 /// elapsed since actual expiration, if expired.
117 /// @return true if the timer expired, false otherwise.
119 bool expired(unsigned long now
, unsigned long& elapsed
);
121 /// Return true if interval has been cleared.
123 /// Note that the timer is constructed as cleared and you
124 /// need to call setInterval() to make it not-cleared.
125 bool cleared() const {
126 return _start
== std::numeric_limits
<unsigned long>::max();
129 /// Execute associated function and reset state
131 /// After execution either the timer is cleared
132 /// (if runOnce) or start time is incremented
135 /// NOTE: if the timer is cleared this call
136 /// results in a no-op.
138 void executeAndReset();
140 /// Mark all reachable resources (for GC)
142 /// Resources reachable from Timer are:
144 /// - Arguments list (_args)
145 /// - Associated function (_function)
146 /// - Target object (_object)
148 void markReachableResources() const;
152 /// Execute associated function properly setting up context
155 /// Execute associated function properly setting up context
156 void operator() () { execute(); }
158 /// Return number of milliseconds between expirations
159 unsigned long getInterval() const { return _interval
; }
161 /// Return number of milliseconds after VM start this timer was last reset
162 unsigned long getStart() const { return _start
; }
166 /// Called by every function setting the interval.
170 /// Number of milliseconds between expirations
171 unsigned int _interval
;
173 /// Number of milliseconds since epoch at Timer start
175 /// This will be numeric_limits<unsigned long>::max()
176 /// if the timer is not active (or cleared)
178 unsigned long _start
;
180 /// The associated function (if statically-bound) stored in
181 /// an intrusive pointer
182 as_function
* _function
;
184 ObjectURI _methodName
;
186 /// Context for the function call. Will be used as 'this' pointer.
189 /// List of arguments
191 /// This must be copied before passing to a function call.
192 const fn_call::Args _args
;
194 /// True if the timer should execute only once (for setTimeout)