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