1*c45d927aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c45d927aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c45d927aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c45d927aSAndrew Rist  * distributed with this work for additional information
6*c45d927aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c45d927aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c45d927aSAndrew Rist  * "License"); you may not use this file except in compliance
9*c45d927aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c45d927aSAndrew Rist  *
11*c45d927aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c45d927aSAndrew Rist  *
13*c45d927aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c45d927aSAndrew Rist  * software distributed under the License is distributed on an
15*c45d927aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c45d927aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c45d927aSAndrew Rist  * specific language governing permissions and limitations
18*c45d927aSAndrew Rist  * under the License.
19*c45d927aSAndrew Rist  *
20*c45d927aSAndrew Rist  *************************************************************/
21*c45d927aSAndrew Rist 
22*c45d927aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef SD_TIMER_BASED_TASK_EXECUTION_HXX
25cdf0e10cSrcweir #define SD_TIMER_BASED_TASK_EXECUTION_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vcl/timer.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace sd { namespace tools {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir class AsynchronousTask;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir /** Execute an AsynchronousTask timer based, i.e. every
36cdf0e10cSrcweir     nMillisecondsBetweenSteps milliseconds as much steps are executed as fit
37cdf0e10cSrcweir     into a nMaxTimePerStep millisecond intervall.
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     When a task is executed completely, i.e. HasNextStep() returns <FALSE/>,
40cdf0e10cSrcweir     the TimerBasedTaskExecution destroys itself.  This, of course, works
41cdf0e10cSrcweir     only if the creating instance does not hold a shared_ptr to  that object.
42cdf0e10cSrcweir */
43cdf0e10cSrcweir class TimerBasedTaskExecution
44cdf0e10cSrcweir {
45cdf0e10cSrcweir public:
46cdf0e10cSrcweir     /** Create a new object of this class.
47cdf0e10cSrcweir         @param rpTask
48cdf0e10cSrcweir             The AsynchronousTask that is to be executed.
49cdf0e10cSrcweir         @param nMillisecondsBetweenSteps
50cdf0e10cSrcweir             Wait at least this long between the execution of steps.  Note
51cdf0e10cSrcweir             that more than one step may be executed in succession.
52cdf0e10cSrcweir         @param nMaxTimePerStep
53cdf0e10cSrcweir             The maximal time for executing steps without yielding control.
54cdf0e10cSrcweir     */
55cdf0e10cSrcweir     static ::boost::shared_ptr<TimerBasedTaskExecution> Create (
56cdf0e10cSrcweir         const ::boost::shared_ptr<AsynchronousTask>& rpTask,
57cdf0e10cSrcweir         sal_uInt32 nMillisecondsBetweenSteps,
58cdf0e10cSrcweir         sal_uInt32 nMaxTimePerStep);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /** Stop the execution of the task and release the shared pointer to
61cdf0e10cSrcweir         itself so that it will eventually be destroyed.
62cdf0e10cSrcweir     */
63cdf0e10cSrcweir     void Release (void);
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /** Convenience method that calls Release() on the given task.  It
66cdf0e10cSrcweir         checks the given weak_ptr for being expired and catches bad_weak_ptr
67cdf0e10cSrcweir         exceptions.
68cdf0e10cSrcweir     */
69cdf0e10cSrcweir     static void ReleaseTask (const ::boost::weak_ptr<TimerBasedTaskExecution>& rpTask);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir private:
72cdf0e10cSrcweir     ::boost::shared_ptr<AsynchronousTask> mpTask;
73cdf0e10cSrcweir     Timer maTimer;
74cdf0e10cSrcweir     /** This shared_ptr to this is used to destroy a TimerBasedTaskExecution
75cdf0e10cSrcweir         object when its task has been executed completely.
76cdf0e10cSrcweir     */
77cdf0e10cSrcweir     ::boost::shared_ptr<TimerBasedTaskExecution> mpSelf;
78cdf0e10cSrcweir     sal_uInt32 mnMaxTimePerStep;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     TimerBasedTaskExecution (
81cdf0e10cSrcweir         const ::boost::shared_ptr<AsynchronousTask>& rpTask,
82cdf0e10cSrcweir         sal_uInt32 nMillisecondsBetweenSteps,
83cdf0e10cSrcweir         sal_uInt32 nMaxTimePerStep);
84cdf0e10cSrcweir     ~TimerBasedTaskExecution (void);
85cdf0e10cSrcweir     void SetSelf (const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf);
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     class Deleter;
88cdf0e10cSrcweir     friend class Deleter;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     DECL_LINK(TimerCallback,Timer*);
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
93cdf0e10cSrcweir } } // end of namespace ::sd::tools
94cdf0e10cSrcweir 
95cdf0e10cSrcweir #endif
96