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