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_ASYNCHRONOUS_CALL_HXX
25cdf0e10cSrcweir #define SD_ASYNCHRONOUS_CALL_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <vcl/timer.hxx>
28cdf0e10cSrcweir #include <memory>
29cdf0e10cSrcweir #include <boost/function.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace sd { namespace tools {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /** Store a function object and execute it asynchronous.
35cdf0e10cSrcweir 
36cdf0e10cSrcweir     The features of this class are:
37cdf0e10cSrcweir     a) It provides a wrapper around a VCL Timer so that generic function
38cdf0e10cSrcweir     objects can be used.
39cdf0e10cSrcweir     b) When more than one function objects are posted to be executed later
40cdf0e10cSrcweir     then the pending ones are erased and only the last one will actually be
41cdf0e10cSrcweir     executed.
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     Use this class like this:
44cdf0e10cSrcweir     aInstanceOfAsynchronousCall.Post(
45cdf0e10cSrcweir         ::boost::bind(
46cdf0e10cSrcweir             ::std::mem_fun(&DrawViewShell::SwitchPage),
47cdf0e10cSrcweir             pDrawViewShell,
48cdf0e10cSrcweir             11));
49cdf0e10cSrcweir */
50cdf0e10cSrcweir class AsynchronousCall
51cdf0e10cSrcweir {
52cdf0e10cSrcweir public:
53cdf0e10cSrcweir     /** Create a new asynchronous call.  Each object of this class processes
54cdf0e10cSrcweir         one (semantical) type of call.
55cdf0e10cSrcweir     */
56cdf0e10cSrcweir     AsynchronousCall (void);
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     ~AsynchronousCall (void);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /** Post a function object that is to be executed asynchronously.  When
61cdf0e10cSrcweir         this method is called while the current function object has not bee
62cdf0e10cSrcweir         executed then the later is destroyed and only the given function
63cdf0e10cSrcweir         object will be executed.
64cdf0e10cSrcweir         @param rFunction
65cdf0e10cSrcweir             The function object that may be called asynchronously in the
66cdf0e10cSrcweir             near future.
67cdf0e10cSrcweir         @param nTimeoutInMilliseconds
68cdf0e10cSrcweir             The timeout in milliseconds until the function object is
69cdf0e10cSrcweir             executed.
70cdf0e10cSrcweir     */
71cdf0e10cSrcweir     typedef ::boost::function0<void> AsynchronousFunction;
72cdf0e10cSrcweir     void Post (
73cdf0e10cSrcweir         const AsynchronousFunction& rFunction,
74cdf0e10cSrcweir         sal_uInt32 nTimeoutInMilliseconds=10);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir private:
77cdf0e10cSrcweir     Timer maTimer;
78cdf0e10cSrcweir     /** The function object that will be executed when the TimerCallback
79cdf0e10cSrcweir         function is called the next time.  This pointer may be NULL.
80cdf0e10cSrcweir     */
81cdf0e10cSrcweir     ::std::auto_ptr<AsynchronousFunction> mpFunction;
82cdf0e10cSrcweir     DECL_LINK(TimerCallback,Timer*);
83cdf0e10cSrcweir };
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir } } // end of namespace ::sd::tools
87cdf0e10cSrcweir 
88cdf0e10cSrcweir #endif
89