reduce verbosity
[gnash.git] / libcore / Timers.h
blob09d8764f37a2fbf127e47cd37d18b532e48d8435
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
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.
9 //
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.
14 //
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
19 #ifndef HAVE_TIMERS_H
20 #define HAVE_TIMERS_H
22 #include "dsodefs.h"
23 #include "smart_ptr.h"
24 #include "fn_call.h"
26 #include <string>
27 #include <limits>
29 // Forward declarations
30 namespace gnash {
31 class as_function;
32 class as_object;
35 namespace gnash {
37 /// An interval timer.
39 /// This is constructed when _global.setInterval() is called.
40 ///
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
43 /// called.
44 ///
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.
51 class DSOEXPORT Timer
53 public:
55 ~Timer();
57 /// Construct a Timer, enabling it.
59 /// @param method
60 /// The function to call from execution operator.
61 /// Will be stored in an intrusive_ptr.
62 ///
63 /// @param ms
64 /// The number of milliseconds between expires.
65 ///
66 /// @param this_ptr
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).
71 ///
72 /// @param args
73 /// The list of arguments to pass to the function being invoked.
74 ///
75 /// @param runOnce
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.
82 /// @param this_ptr
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).
87 ///
88 /// @param methodName
89 /// The method name to call from execution operator.
90 ///
91 /// @param ms
92 /// The number of milliseconds between expires.
93 ///
94 /// @param args
95 /// The list of arguments to pass to the function being invoked.
96 ///
97 /// @param runOnce
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
112 // /// @param now
113 /// Current time, in milliseconds.
115 /// @param elapsed
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
135 /// by the interval.
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;
152 private:
154 /// Execute associated function properly setting up context
155 void execute();
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; }
166 /// Set timer start
168 /// Called by every function setting the interval.
170 void start();
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.
189 as_object* _object;
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)
197 bool _runOnce;
200 } // namespace gnash
202 #endif