1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SD_TIMER_BASED_TASK_EXECUTION_HXX
29 #define SD_TIMER_BASED_TASK_EXECUTION_HXX
30 
31 #include <vcl/timer.hxx>
32 
33 #include <boost/shared_ptr.hpp>
34 
35 namespace sd { namespace tools {
36 
37 class AsynchronousTask;
38 
39 /** Execute an AsynchronousTask timer based, i.e. every
40     nMillisecondsBetweenSteps milliseconds as much steps are executed as fit
41     into a nMaxTimePerStep millisecond intervall.
42 
43     When a task is executed completely, i.e. HasNextStep() returns <FALSE/>,
44     the TimerBasedTaskExecution destroys itself.  This, of course, works
45     only if the creating instance does not hold a shared_ptr to  that object.
46 */
47 class TimerBasedTaskExecution
48 {
49 public:
50     /** Create a new object of this class.
51         @param rpTask
52             The AsynchronousTask that is to be executed.
53         @param nMillisecondsBetweenSteps
54             Wait at least this long between the execution of steps.  Note
55             that more than one step may be executed in succession.
56         @param nMaxTimePerStep
57             The maximal time for executing steps without yielding control.
58     */
59     static ::boost::shared_ptr<TimerBasedTaskExecution> Create (
60         const ::boost::shared_ptr<AsynchronousTask>& rpTask,
61         sal_uInt32 nMillisecondsBetweenSteps,
62         sal_uInt32 nMaxTimePerStep);
63 
64     /** Stop the execution of the task and release the shared pointer to
65         itself so that it will eventually be destroyed.
66     */
67     void Release (void);
68 
69     /** Convenience method that calls Release() on the given task.  It
70         checks the given weak_ptr for being expired and catches bad_weak_ptr
71         exceptions.
72     */
73     static void ReleaseTask (const ::boost::weak_ptr<TimerBasedTaskExecution>& rpTask);
74 
75 private:
76     ::boost::shared_ptr<AsynchronousTask> mpTask;
77     Timer maTimer;
78     /** This shared_ptr to this is used to destroy a TimerBasedTaskExecution
79         object when its task has been executed completely.
80     */
81     ::boost::shared_ptr<TimerBasedTaskExecution> mpSelf;
82     sal_uInt32 mnMaxTimePerStep;
83 
84     TimerBasedTaskExecution (
85         const ::boost::shared_ptr<AsynchronousTask>& rpTask,
86         sal_uInt32 nMillisecondsBetweenSteps,
87         sal_uInt32 nMaxTimePerStep);
88     ~TimerBasedTaskExecution (void);
89     void SetSelf (const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf);
90 
91     class Deleter;
92     friend class Deleter;
93 
94     DECL_LINK(TimerCallback,Timer*);
95 };
96 
97 } } // end of namespace ::sd::tools
98 
99 #endif
100