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