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