merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / tools / TimerBasedTaskExecution.cxx
blob66dc8aac45361a8620e442000e6e7511d9345b1d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TimerBasedTaskExecution.cxx,v $
10 * $Revision: 1.5 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "tools/TimerBasedTaskExecution.hxx"
35 #include "tools/AsynchronousTask.hxx"
36 #include <tools/time.hxx>
37 #include <osl/diagnose.h>
38 #include <boost/weak_ptr.hpp>
40 #undef VERBOSE
42 namespace sd { namespace tools {
44 /** Used by the shared_ptr instead of the private destructor.
46 class TimerBasedTaskExecution::Deleter
48 public:
49 void operator() (TimerBasedTaskExecution* pObject)
51 delete pObject;
58 ::boost::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
59 const ::boost::shared_ptr<AsynchronousTask>& rpTask,
60 sal_uInt32 nMillisecondsBetweenSteps,
61 sal_uInt32 nMaxTimePerStep)
63 ::boost::shared_ptr<TimerBasedTaskExecution> pExecution(
64 new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep),
65 Deleter());
66 // Let the new object have a shared_ptr to itself, so that it can
67 // release itself when the AsynchronousTask has been executed
68 // completely.
69 pExecution->SetSelf(pExecution);
70 return pExecution;
76 void TimerBasedTaskExecution::Release (void)
78 maTimer.Stop();
79 mpSelf.reset();
85 //static
86 void TimerBasedTaskExecution::ReleaseTask (
87 const ::boost::weak_ptr<TimerBasedTaskExecution>& rpExecution)
89 if ( ! rpExecution.expired())
91 try
93 ::boost::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
94 pExecution->Release();
96 catch (::boost::bad_weak_ptr)
98 // When a bad_weak_ptr has been thrown then the object pointed
99 // to by rpTask has been released right after we checked that it
100 // still existed. Too bad, but that means, that we have nothing
101 // more do.
109 TimerBasedTaskExecution::TimerBasedTaskExecution (
110 const ::boost::shared_ptr<AsynchronousTask>& rpTask,
111 sal_uInt32 nMillisecondsBetweenSteps,
112 sal_uInt32 nMaxTimePerStep)
113 : mpTask(rpTask),
114 maTimer(),
115 mpSelf(),
116 mnMaxTimePerStep(nMaxTimePerStep)
118 Link aLink(LINK(this,TimerBasedTaskExecution,TimerCallback));
119 maTimer.SetTimeoutHdl(aLink);
120 maTimer.SetTimeout(nMillisecondsBetweenSteps);
121 maTimer.Start();
127 TimerBasedTaskExecution::~TimerBasedTaskExecution (void)
129 maTimer.Stop();
135 void TimerBasedTaskExecution::SetSelf (
136 const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf)
138 if (mpTask.get() != NULL)
139 mpSelf = rpSelf;
145 IMPL_LINK(TimerBasedTaskExecution,TimerCallback, Timer*,EMPTYARG)
147 if (mpTask.get() != NULL)
149 if (mpTask->HasNextStep())
151 // Execute as many steps as fit into the time span of length
152 // mnMaxTimePerStep. Note that the last step may take longer
153 // than allowed.
154 sal_uInt32 nStartTime (Time().GetMSFromTime());
155 #ifdef VERBOSE
156 OSL_TRACE("starting TimerBasedTaskExecution at %d", nStartTime);
157 #endif
160 mpTask->RunNextStep();
161 sal_uInt32 nDuration (Time().GetMSFromTime()-nStartTime);
162 #ifdef VERBOSE
163 OSL_TRACE("executed step in %d", nDuration);
164 #endif
165 if (nDuration > mnMaxTimePerStep)
166 break;
168 while (mpTask->HasNextStep());
169 #ifdef VERBOSE
170 OSL_TRACE("TimerBasedTaskExecution sleeping");
171 #endif
172 maTimer.Start();
174 else
175 mpSelf.reset();
178 return 0;
182 } } // end of namespace ::sd::tools