~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

« back to all changes in this revision

Viewing changes to libcore/timers.h

  • Committer: Bazaar Package Importer
  • Author(s): Sindhudweep Narayan Sarkar
  • Date: 2009-10-07 00:06:10 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20091007000610-mj9rwqe774gizn1j
Tags: 0.8.6-0ubuntu1
new upstream release 0.8.6 (LP: #435897)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
//   Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3
 
// 
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.
8
 
// 
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.
13
 
// 
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
17
 
 
18
 
#ifndef HAVE_TIMERS_H
19
 
#define HAVE_TIMERS_H
20
 
 
21
 
#include "dsodefs.h"
22
 
 
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
27
 
 
28
 
#include <string>
29
 
#include <vector> 
30
 
#include <limits>
31
 
 
32
 
// Forward declarations
33
 
namespace gnash {
34
 
        class fn_call;
35
 
        class as_function;
36
 
}
37
 
 
38
 
namespace gnash {
39
 
  
40
 
/// An interval timer.
41
 
//
42
 
/// This is constructed when _global.setInterval() is called.
43
 
/// Instances of this class will be stored in the movie_root singleton.
44
 
///
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
47
 
/// called.
48
 
///
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.
55
 
///
56
 
///
57
 
class DSOEXPORT Timer
58
 
{
59
 
 
60
 
public:
61
 
 
62
 
      /// Construct a disabled (cleared) timer.
63
 
      Timer();
64
 
 
65
 
      ~Timer();
66
 
 
67
 
      /// Setup the Timer, enabling it.
68
 
      //
69
 
      /// @param method
70
 
      ///       The function to call from execution operator.
71
 
      ///       Will be stored in an intrusive_ptr.
72
 
      ///
73
 
      /// @param ms
74
 
      ///       The number of milliseconds between expires.
75
 
      ///
76
 
      /// @param this_ptr
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).
81
 
      ///
82
 
      /// @param runOnce
83
 
      ///       If true the interval will run only once. False if omitted.
84
 
      ///
85
 
      void setInterval(as_function& method, unsigned long ms, boost::intrusive_ptr<as_object> this_ptr, bool runOnce=false);
86
 
 
87
 
      /// Setup the Timer, enabling it.
88
 
      //
89
 
      /// @param method
90
 
      ///       The function to call from execution operator.
91
 
      ///       Will be stored in an intrusive_ptr.
92
 
      ///
93
 
      /// @param ms
94
 
      ///       The number of milliseconds between expires.
95
 
      ///
96
 
      /// @param this_ptr
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).
101
 
      ///
102
 
      /// @param args
103
 
      ///       The list of arguments to pass to the function being invoked.
104
 
      ///
105
 
      /// @param runOnce
106
 
      ///       If true the interval will run only once. False if omitted.
107
 
      ///
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);
110
 
 
111
 
      /// Setup the Timer to call a late-evaluated object method, enabling it.
112
 
      //
113
 
      /// @param this_ptr
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).
118
 
      ///
119
 
      /// @param methodName
120
 
      ///       The method name to call from execution operator.
121
 
      ///
122
 
      /// @param ms
123
 
      ///       The number of milliseconds between expires.
124
 
      ///
125
 
      /// @param args
126
 
      ///       The list of arguments to pass to the function being invoked.
127
 
      ///
128
 
      /// @param runOnce
129
 
      ///       If true the interval will run only once. False if omitted.
130
 
      ///
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);
133
 
 
134
 
      /// Clear the timer, ready for reuse
135
 
      //
136
 
      /// When a Timer is cleared, the expired() function
137
 
      /// will always return false.
138
 
      ///
139
 
      /// Use setInterval() to reset it.
140
 
      ///
141
 
      void clearInterval();
142
 
 
143
 
      /// Get expiration state
144
 
      //
145
 
      /// @param now
146
 
      ///    Current time, in milliseconds.
147
 
      ///
148
 
      /// @param elapsed
149
 
      ///    Output parameter, will be set to the amount of milliseconds
150
 
      ///    elapsed since actual expiration, if expired.
151
 
      ///
152
 
      /// @return true if the timer expired, false otherwise.
153
 
      ///
154
 
      bool expired(unsigned long now, unsigned long& elapsed); 
155
 
 
156
 
      /// Return true if interval has been cleared.
157
 
      //
158
 
      /// Note that the timer is constructed as cleared and you
159
 
      /// need to call setInterval() to make it not-cleared.
160
 
      bool cleared() const
161
 
      {
162
 
            return _start == std::numeric_limits<unsigned long>::max();
163
 
      }
164
 
 
165
 
      /// Execute associated function and reset state
166
 
      //
167
 
      /// After execution either the timer is cleared
168
 
      /// (if runOnce) or start time is incremented
169
 
      /// by the interval.
170
 
      ///
171
 
      /// NOTE: if the timer is cleared this call
172
 
      ///       results in a no-op.
173
 
      ///
174
 
      void executeAndReset();
175
 
 
176
 
      /// Arguments list type
177
 
      typedef std::vector<as_value> ArgsContainer;
178
 
 
179
 
#ifdef GNASH_USE_GC
180
 
        /// Mark all reachable resources (for GC)
181
 
        //
182
 
        /// Resources reachable from Timer are:
183
 
        ///
184
 
        ///     - Arguments list (_args)
185
 
        ///     - Associated function (_function)
186
 
        ///     - Target object (_object)
187
 
        ///
188
 
        void markReachableResources() const;
189
 
#endif // GNASH_USE_GC
190
 
 
191
 
private:
192
 
 
193
 
      /// Execute associated function properly setting up context
194
 
      void execute();
195
 
 
196
 
      /// Execute associated function properly setting up context
197
 
      void operator() () { execute(); }
198
 
      
199
 
      /// Return number of milliseconds between expirations 
200
 
      unsigned long getInterval() const { return _interval; }
201
 
 
202
 
      /// Return number of milliseconds after VM start this timer was last reset
203
 
      unsigned long getStart() const { return _start; }
204
 
 
205
 
 
206
 
      /// Set timer start
207
 
      //
208
 
      /// Called by every function setting the interval.
209
 
      ///
210
 
      void start();
211
 
 
212
 
      /// Number of milliseconds between expirations 
213
 
      unsigned int _interval;
214
 
 
215
 
      /// Number of milliseconds since epoch at Timer start 
216
 
      //
217
 
      /// This will be numeric_limits<unsigned long>::max()
218
 
      /// if the timer is not active (or cleared)
219
 
      ///
220
 
      unsigned long _start;
221
 
 
222
 
      /// The associated function (if statically-bound) stored in an intrusive pointer
223
 
      boost::intrusive_ptr<as_function> _function;
224
 
 
225
 
      /// The associated method name, stored in an intrusive pointer
226
 
      std::string _methodName;
227
 
 
228
 
      /// Context for the function call. Will be used as 'this' pointer.
229
 
      boost::intrusive_ptr<as_object> _object;
230
 
 
231
 
      /// List of arguments
232
 
      ArgsContainer _args;
233
 
 
234
 
      /// True if the timer should execute only once (for setTimeout)
235
 
      bool _runOnce;
236
 
};
237
 
  
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);
241
 
  
242
 
} // end of namespace gnash
243
 
 
244
 
  // HAVE_TIMERS_H
245
 
#endif